-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.txt
73 lines (62 loc) · 3.09 KB
/
index.txt
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
/*
var fs = require('fs');
var distinct = array => array.filter((value,index,that) => that.indexOf(value) == index);
fs.readFile('dict.txt', 'utf8', (err,data) => {
var dictionary = data.split(/[:.,\s]+[-=]+|[=-]+|[.:]+|[{}]+|[\r]+|[\n]+|[\r\n]+|[\s\n]+|[\n\s]+|[()]+/)
.filter(x => x.trim()!=="")
.filter(x => !isFinite(x) );
dictionary = dictionary.map(x => x.toLowerCase().replace(/[,.'"?!]/g,''));
dictionary = distinct(dictionary);
//dictionary = dictionary.filter(term => ['.','',',',':',] .indexOf(term.trim()) == -1);
var x = 1;
});
*/
Array.prototype.contains = (term) => this.indexOf(term) != -1;
var dictionary = ['which','is','the','basis','theory','of'];
var str = "|wh| |is|th| b| o|as|is|eo| |ry| |f |ic| t|, | |he|h |";
var input2 = str.split('|').filter(x => x!='' && x != ' ');
var result = [];
var R = "";
var someDictWordStartsWithR = false;
do {
// i am removing the first element of input2 at the same time that i am it putting into R
// but i am doing this only if the word is not fully complete (which means that the some word of the dictionary starts with R)
if(!someDictWordStartsWithR) {
R = input2.splice(0,1)[0];
}
var i = 0;
while(i < input2.length && input2.length > 0) {
var possibleWord = R.concat(input2[i]).trim();
// first of all, verify if the actual R is a valid term in the dictionary
if(dictionary.indexOf(R.toLowerCase()) != -1) {
result.push(R);
someDictWordStartsWithR = false;
break;
} else if(dictionary.indexOf(possibleWord.toLowerCase()) != -1) {
//if(dictionary.contains()) {
// possible word is actually a word
// here i'm removing this term of the input2 array
input2.splice(i,1);
//add this word to the result
result.push(possibleWord);
someDictWordStartsWithR = false;
break;
} else if( dictionary.some(t => t.startsWith(possibleWord.toLowerCase())) ) {
// it means that some word of the dictionary starts with 'possibleWord'. I will continue trying to find the full word
R = possibleWord;
someDictWordStartsWithR = true;
input2.splice(i,1);
break;
} else {
someDictWordStartsWithR = false;
// if it's got here, it means no word starts with possibleWord nor possibleWord is a full word in the dictionary
// here i will add nothing the final result, as possibleWord is not valid.
// i will not delete anything from input2 because i already did it in the first place
}
// this code will run until the array is empty, which means that I tried to create all the existing words possible in the dictionary.
// the code is not complete, it will only try and match the valid words with the dictionary (it doesnt include special characters like ",.' etc)
i++;
}
} while(input2.length > 0);
if(dictionary.indexOf(R.toLowerCase()) != -1) { result.push(R); }
process.exit();