-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakeText2.js
More file actions
70 lines (48 loc) · 1.27 KB
/
Copy pathmakeText2.js
File metadata and controls
70 lines (48 loc) · 1.27 KB
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
/** Command-line tool to generate Markov text. */
const fs = require("fs");
const bigram = require("./bigram");
const axios = require("axios");
const process = require("process");
console.log('ggg', bigram)
/** Make Markov machine from text and generate text from it. */
function generateText(text) {
let mm = new bigram.MarkovBigram(text);
console.log(mm.makeText());
}
/** read file and generate text from it. */
function makeText(path) {
fs.readFile(path, "utf8", function cb(err, data) {
if (err) {
console.error(`Cannot read file: ${path}: ${err}`);
process.exit(1);
} else {
generateText(data);
}
});
}
/** read URL and make text from it. */
async function makeURLText(url) {
let resp;
try {
resp = await axios.get(url);
} catch (err) {
console.error(`Cannot read URL: ${url}: ${err}`);
process.exit(1);
}
generateText(resp.data)
}
/** interpret cmdline to decide what to do. */
//skip first two
let [method, path] = process.argv.slice(2);
if (method === "file") {
makeText(path);
}
else if (method === "url") {
makeURLText(path);
}
else {
console.error(`Unknown method: ${method}`);
process.exit(1);
}
// node makeText.js file eggs.txt
// node makeText.js url http://www.gutenberg.org/files/11/11-0.txt