forked from mckaywrigley/paul-graham-gpt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrape.ts
202 lines (161 loc) · 4.98 KB
/
scrape.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import { PGChunk, PGEssay, PGJSON } from "@/types";
import axios from "axios";
import * as cheerio from "cheerio";
import fs from "fs";
import { encode } from "gpt-3-encoder";
const BASE_URL = "http://www.paulgraham.com/";
const CHUNK_SIZE = 200;
const getLinks = async () => {
const html = await axios.get(`${BASE_URL}articles.html`);
const $ = cheerio.load(html.data);
const tables = $("table");
const linksArr: { url: string; title: string }[] = [];
tables.each((i, table) => {
if (i === 2) {
const links = $(table).find("a");
links.each((i, link) => {
const url = $(link).attr("href");
const title = $(link).text();
if (url && url.endsWith(".html")) {
const linkObj = {
url,
title
};
linksArr.push(linkObj);
}
});
}
});
return linksArr;
};
const getEssay = async (linkObj: { url: string; title: string }) => {
const { title, url } = linkObj;
let essay: PGEssay = {
title: "",
url: "",
date: "",
thanks: "",
content: "",
length: 0,
tokens: 0,
chunks: []
};
const fullLink = BASE_URL + url;
const html = await axios.get(fullLink);
const $ = cheerio.load(html.data);
const tables = $("table");
tables.each((i, table) => {
if (i === 1) {
const text = $(table).text();
let cleanedText = text.replace(/\s+/g, " ");
cleanedText = cleanedText.replace(/\.([a-zA-Z])/g, ". $1");
const date = cleanedText.match(/([A-Z][a-z]+ [0-9]{4})/);
let dateStr = "";
let textWithoutDate = "";
if (date) {
dateStr = date[0];
textWithoutDate = cleanedText.replace(date[0], "");
}
let essayText = textWithoutDate.replace(/\n/g, " ");
let thanksTo = "";
const split = essayText.split(". ").filter((s) => s);
const lastSentence = split[split.length - 1];
if (lastSentence && lastSentence.includes("Thanks to")) {
const thanksToSplit = lastSentence.split("Thanks to");
if (thanksToSplit[1].trim()[thanksToSplit[1].trim().length - 1] === ".") {
thanksTo = "Thanks to " + thanksToSplit[1].trim();
} else {
thanksTo = "Thanks to " + thanksToSplit[1].trim() + ".";
}
essayText = essayText.replace(thanksTo, "");
}
const trimmedContent = essayText.trim();
essay = {
title,
url: fullLink,
date: dateStr,
thanks: thanksTo.trim(),
content: trimmedContent,
length: trimmedContent.length,
tokens: encode(trimmedContent).length,
chunks: []
};
}
});
return essay;
};
const chunkEssay = async (essay: PGEssay) => {
const { title, url, date, thanks, content, ...chunklessSection } = essay;
let essayTextChunks = [];
if (encode(content).length > CHUNK_SIZE) {
const split = content.split(". ");
let chunkText = "";
for (let i = 0; i < split.length; i++) {
const sentence = split[i];
const sentenceTokenLength = encode(sentence);
const chunkTextTokenLength = encode(chunkText).length;
if (chunkTextTokenLength + sentenceTokenLength.length > CHUNK_SIZE) {
essayTextChunks.push(chunkText);
chunkText = "";
}
if (sentence[sentence.length - 1].match(/[a-z0-9]/i)) {
chunkText += sentence + ". ";
} else {
chunkText += sentence + " ";
}
}
essayTextChunks.push(chunkText.trim());
} else {
essayTextChunks.push(content.trim());
}
const essayChunks = essayTextChunks.map((text) => {
const trimmedText = text.trim();
const chunk: PGChunk = {
essay_title: title,
essay_url: url,
essay_date: date,
essay_thanks: thanks,
content: trimmedText,
content_length: trimmedText.length,
content_tokens: encode(trimmedText).length,
embedding: []
};
return chunk;
});
if (essayChunks.length > 1) {
for (let i = 0; i < essayChunks.length; i++) {
const chunk = essayChunks[i];
const prevChunk = essayChunks[i - 1];
if (chunk.content_tokens < 100 && prevChunk) {
prevChunk.content += " " + chunk.content;
prevChunk.content_length += chunk.content_length;
prevChunk.content_tokens += chunk.content_tokens;
essayChunks.splice(i, 1);
i--;
}
}
}
const chunkedSection: PGEssay = {
...essay,
chunks: essayChunks
};
return chunkedSection;
};
(async () => {
const links = await getLinks();
let essays = [];
for (let i = 0; i < links.length; i++) {
const essay = await getEssay(links[i]);
const chunkedEssay = await chunkEssay(essay);
essays.push(chunkedEssay);
}
const json: PGJSON = {
current_date: "2023-03-01",
author: "Paul Graham",
url: "http://www.paulgraham.com/articles.html",
length: essays.reduce((acc, essay) => acc + essay.length, 0),
tokens: essays.reduce((acc, essay) => acc + essay.tokens, 0),
essays
};
fs.writeFileSync("scripts/pg.json", JSON.stringify(json));
})();