|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +import * as fsa from 'async-file'; |
| 4 | + |
| 5 | +import { |
| 6 | + readLines, |
| 7 | + handleIndexSourceErrors, |
| 8 | + readCached, |
| 9 | + figureOutTruncateAndSelector, |
| 10 | + cheerioLoad |
| 11 | +} from './common'; |
| 12 | + |
| 13 | +import {stopWords} from './stopwords'; |
| 14 | + |
| 15 | +const URL_LIST = 'archive/index.csv'; |
| 16 | +const OVERWRITE = true; |
| 17 | + |
| 18 | +function removePunctuation(input) { |
| 19 | + return input.replace(/[^\w\s]|_/g, ''); |
| 20 | +} |
| 21 | + |
| 22 | +async function extractWords(recv, source) { |
| 23 | + const loaded = cheerioLoad(recv); |
| 24 | + return loaded.then(shard => { |
| 25 | + const {_, truncate} = figureOutTruncateAndSelector(source); |
| 26 | + shard(truncate).remove(); |
| 27 | + const text = shard.text().split(' '); |
| 28 | + const words = Object.create(null); |
| 29 | + const foundOnce = new Set(); |
| 30 | + for (let i = 0; i < text.length; i++) { |
| 31 | + const w = removePunctuation(text[i]).toLowerCase(); |
| 32 | + if (/^[a-zA-ZÀ-ÖØ-öø-ÿ]+$/.test(w) && stopWords.has(w) === false) { |
| 33 | + if (foundOnce.has(w)) { |
| 34 | + if (Object.prototype.hasOwnProperty.call(words, w)) { |
| 35 | + words[w]++; |
| 36 | + } else { |
| 37 | + words[w] = 2; |
| 38 | + } |
| 39 | + } else { |
| 40 | + foundOnce.add(w); |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + return words; |
| 45 | + }); |
| 46 | +} |
| 47 | + |
| 48 | +async function read(source) { |
| 49 | + const path = `archive/${source.slug}`; |
| 50 | + const cache = `${path}/cache.html`; |
| 51 | + const targetFileName = `${path}/analyze.json`; |
| 52 | + const cacheExists = await fsa.exists(cache); |
| 53 | + const data = {}; |
| 54 | + if (cacheExists === true) { |
| 55 | + const cached = await readCached(cache); |
| 56 | + const words = await extractWords(cached, source); |
| 57 | + data.words = words; |
| 58 | + } |
| 59 | + |
| 60 | + return {file: targetFileName, data}; |
| 61 | +} |
| 62 | + |
| 63 | +function sort(subject) { |
| 64 | + let sortable = []; |
| 65 | + for (let key in subject) { |
| 66 | + sortable.push([key, subject[key]]); |
| 67 | + } |
| 68 | + // Sort from more occurences, to least |
| 69 | + sortable.sort((a, b) => { |
| 70 | + return -1 * (a[1] - b[1]); |
| 71 | + }); |
| 72 | + |
| 73 | + return sortable; // array in format [ [ key1, val1 ], [ key2, val2 ], ... ] |
| 74 | +} |
| 75 | + |
| 76 | +async function analyze(recv) { |
| 77 | + const words = recv.data.words; |
| 78 | + const keywords = Object.create(null); |
| 79 | + const sorted = sort(words); |
| 80 | + const max = 10; |
| 81 | + let iter = 0; |
| 82 | + for (let popular of sorted) { |
| 83 | + let used = popular[1]; // word has been used n times |
| 84 | + let word = popular[0]; |
| 85 | + if (iter <= max && used > 3) { |
| 86 | + keywords[word] = used; |
| 87 | + } |
| 88 | + iter++; |
| 89 | + } |
| 90 | + |
| 91 | + recv.data.keywords = keywords; |
| 92 | + |
| 93 | + return recv; |
| 94 | +} |
| 95 | + |
| 96 | +async function write({file, data = {}}, boolOverwrite = false) { |
| 97 | + const destExists = await fsa.exists(file); |
| 98 | + if (destExists === false || (destExists === true && boolOverwrite)) { |
| 99 | + await fsa.writeTextFile(file, JSON.stringify(data), 'utf8'); |
| 100 | + } |
| 101 | + |
| 102 | + return {file, data}; |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * Something is going somewhat as an anti-pattern here. |
| 107 | + * We want Promise.all(...) at each step, and it's not how |
| 108 | + * it is as of now. Needs rework here. TODO |
| 109 | + */ |
| 110 | +for (const url of readLines(URL_LIST)) { |
| 111 | + Promise.resolve(url) |
| 112 | + .then(u => read(u)) |
| 113 | + .then(descriptor => analyze(descriptor)) |
| 114 | + .then(descriptor => write(descriptor, OVERWRITE)) |
| 115 | + .catch(handleIndexSourceErrors); |
| 116 | +} |
0 commit comments