-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
160 lines (143 loc) · 4.48 KB
/
cli.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
// import { getEtymologies, textifyEtymologies } from './parseWiki';
const { Dictionary } = require('./dictionary');
const { Word } = require('./wikiTypes')
const { findLanguage } = require('./langs');
const yargs = require('yargs');
const argv = compileOptions();
function compileOptions(){
return yargs
.usage('Usage: $0 command args')
.option('language', {
alias: ['lang', 'l'],
default: 'English',
type: 'string',
desc: 'language of the etymology'
})
.command({
command: 'single <word>',
aliases: ['s'],
desc: 'etymology of a single word',
handler: argv => {
handleThen(singleEtymology(argv));
}
})
.command({
command: 'bag',
desc: 'etymology of a bag of words, using the dictionary cache',
handler: argv => {
handleThen(bagEtymology(argv))
}
})
.command({
command: 'stats',
desc: 'stats of a dictionary',
handler: argv => {
handleThen(dictStats(argv));
}
})
.command({
command: 'table',
desc: 'generate cognate table',
handler: argv => {
handleThen(cognateTable(argv));
}
})
.help('help')
.argv;
}
async function singleEtymology(argv){
const wd = argv.word;
const targetLang = argv.language;
const word = new Word(wd, targetLang);
const senses = await word.fetchWiktionary();
const etyms = word.toString();
console.log(etyms);
console.log('Main ones:');
console.log(senses.map((sense, i) => `Sense ${i}: ${sense.mainEtymology().toString()}`).join('\n'));
}
function cleanInput(input){
return input
.replace('\n', ' ')
.replace(/['":;\.,]/g,'')
.replace(/\s+/g, ' ')
.toLowerCase();
}
async function bagEtymology(argv){
const rawInput = await getStdin();
const input = cleanInput(rawInput);
const dict = new Dictionary(argv.language);
await dict.load();
const words = await Promise.all(input.split(' ').map(dict.fetchWord.bind(dict)));
words.forEach(word => {
console.log(` == ${word.word} == `);
console.log(word.toString());
})
await dict.save();
}
async function dictStats(argv){
const dict = new Dictionary(argv.language);
await dict.load();
const res = dict.countMainOrigins();
console.log('Inherited from:');
console.log(Object.keys(res.inherited).map(i => `${findLanguage(i)}: ${res.inherited[i]}`).join('\n'));
console.log('Borrowed from:');
console.log(Object.keys(res.borrowed).map(i => `${findLanguage(i)}: ${res.borrowed[i]}`).join('\n'));
console.log('Derived from:');
console.log(Object.keys(res.derived).map(i => `${findLanguage(i)}: ${res.derived[i]}`).join('\n'));
console.log('Etymologied from:');
console.log(Object.keys(res.etymology).map(i => `${findLanguage(i)}: ${res.etymology[i]}`).join('\n'));
}
async function cognateTable(argv){
const cognateTableConfig = {
"English": {
main: "en",
history: [
"enm",
"ang",
"gem-pro",
"ine-pro"
]
}
}
if(!cognateTableConfig[argv.language]){
console.log('Supported languages: ' + Object.keys(cognateTableConfig).join(', '));
return;
}
const dict = new Dictionary(argv.language);
await dict.load();
const table = dict.generateCognateTable(cognateTableConfig[argv.language])
process.stdout.write(`${findLanguage(cognateTableConfig[argv.language].main)}\t\t`)
console.log(cognateTableConfig[argv.language].history.map(l => `${findLanguage(l)}`).join('\t'));
console.log(cognateTableConfig[argv.language]);
table.forEach(row => {
if(row.length != cognateTableConfig[argv.language].history.length+1){
console.log('Warning: row does not have the expected length!');
}
process.stdout.write(`${row[0]}\t`);
console.log(row.slice(1).map(t => {
if(t.length == 0 || !t) return '\t';
return t.map(tt => `${tt.getForm()}`).join(',');
// return t.map(tt => `${tt.getForm()}${tt.getMeaning() ? ` '${tt.getMeaning()}'` : '' }`).join(',');
}).join('\t'));
})
}
function getStdin(){
return new Promise((resolve, reject) => {
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
process.stdin.on('data', inputStdin => {
inputString += inputStdin;
});
process.stdin.on('end', function() {
resolve(inputString);
});
});
}
function handleThen(promise){
promise.then(() => console.log('Done.')).catch(err => {
console.error('We might have run into an error!');
console.error(err.message);
console.trace(err);
});
}