-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
195 lines (182 loc) · 6.13 KB
/
index.js
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
import { argv } from 'node:process'
import * as path from 'path'
import { readdir, readFile } from 'node:fs/promises'
import { createWriteStream } from 'node:fs'
const pages = path.join(argv.slice(2)[0], 'pages')
let pageTitle = ''
let itemId = ''
console.log(pages)
class Counter extends Map{
get(key) {
return super.get(key)
}
count(key){
// guard against null key
if (key == null || key.length == 0) {
return
}
return super.set(key, (super.get(key) || 0) + 1)
}
}
const extractItemText = (text) => {
text.replace(/\[{2}|\[(?:[\S]+)|\]{1,2}/g,' ') // remove braces from any wiki links
.replace(/\n/g, ' ')
.replace(/<style.*?<\/style>/g, ' ') // remove any STYLE content
.replace(/<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>/g, ' ') // remove HTML tags - fails if mismatched quotes!
.replace(/<(?:[^>])+>/g, ' ') // remove any HTML tags - fails if embedded tags - in alt text!
.replace(/\[([^\]]*?)\][\[\(].*?[\]\)]/g, ` ${2} `) // remove markdown links
.replace(/https?.*?(?=\p{White_Space}|$)/gu, ' ') // remove any URLs
.replace(/[\p{P}\p{Emoji}\p{Symbol}}]+/gu, ' ') // remove all Punctuation, Emoji and Symbols
.replace(/[\p{White_Space}\n\t]+/gu, ' ')
.split(/\s+/) // split - same a default tokenizer in minisearch
//.filter((word) => word.match(/^[\p{Alphabetic}]*$/u)) // words must only contain letters
.forEach((word) => {
words.count(word.toLowerCase())
if (word.length > 50) {
console.log('long word', pageTitle, itemId, word)
}
})
}
const extractStoryText = (story) => {
story.forEach((item) => {
if (Object.hasOwn(item, 'type')) {
items.count(item.type)
itemId = item.id
if (Object.hasOwn(item, 'text')) {
switch (item.type) {
case 'paragraph':
case 'markdown':
case 'html':
case 'reference':
case 'image':
case 'pagefold':
case 'math':
case 'mathjax':
extractItemText(item.text)
break
case 'audio':
case 'video':
case 'frame':
// remove lines starting with UPPERCASE word or a URL
extractItemText(item.text.split(/\r\n?|\n/)
.map((line) => {
const firstWord = line.split(/\p{White_Space}/u)[0]
if (firstWord.startsWith('http') ||
firstWord.toUpperCase() === firstWord ||
firstWord.startsWith('//')) {
// line is markup
return ''
} else {
return line
}
}).join(' '))
break
default:
// other item types are not indexed.
}
}
} else {
items.count('type missing')
}
})
}
const words = new Counter()
const items = new Counter()
readdir(pages, { withFileTypes: true })
.then((directroyListing) => {
new Promise((resolve, reject) => {
directroyListing.forEach((entry, index, array) => {
if (entry.isFile()) {
const filePath = path.join(entry.path, entry.name)
readFile(filePath, { encoding: 'utf-8'})
.then((raw) => {
return JSON.parse(raw)
})
.then((page) => {
try {
if (Object.hasOwn(page, 'story')) {
const story = page.story
pageTitle = page.title
extractStoryText(story)
}
} catch (error) {
console.log('problem with page', filePath, error, page)
}
})
.finally(() => {
if (index === array.length -1) resolve()
})
}
})
})
.then(() => {
console.log('Number of different item types:\t', items.size)
console.log('Number of different words:\t', words.size)
const itemStream = createWriteStream('items.txt')
const freqItems = [...items.entries()].sort((a,b) => {
if (a[1] < b[1]) {
return 1
} else if (a[1] > b[1]) {
return -1
}
// same frequency so by alpha
if (a[0] < b[0]) {
return -1
} else if (a[0] > b[0]) {
return 1
}
return 0
})
freqItems.forEach((item) => {
itemStream.write(item[1] + '\t' + item[0] + '\n')
})
itemStream.close()
// report on words found - alphabetical list
const wordStream = createWriteStream('words.txt')
const sortedWords = [...words.keys()].sort()
sortedWords.forEach((word) => {
wordStream.write(word + '\n')
})
wordStream.close()
// report on the length of words - longest first
const lenStream = createWriteStream('length.txt')
const lenWords = [...words.keys()].sort((a,b) => {
if (a.length < b.length) {
return 1
} else if (a.length > b.length) {
return -1
}
// same length so by alpha
if (a.at(0) < b.at(0)) {
return -1
} else if (a.at(0) > b.at(0)) {
return 1
}
return 0
})
lenWords.forEach((len) => {
lenStream.write(len.length + '\t' + len + '\n')
})
lenStream.close()
// report on word frequency
const freqStream = createWriteStream('frequency.txt')
const freqWords = [...words.entries()].sort((a,b) => {
if (a[1] < b[1]) {
return 1
} else if (a[1] > b[1]) {
return -1
}
// same frequency so by alpha
if (a[0] < b[0]) {
return -1
} else if (a[0] > b[0]) {
return 1
}
return 0
})
freqWords.forEach((freq) => {
freqStream.write(freq[1] + '\t' + freq[0] + '\n')
})
freqStream.close()
})
})