-
Notifications
You must be signed in to change notification settings - Fork 214
/
knwl.js
executable file
·173 lines (151 loc) · 5.24 KB
/
knwl.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
function Knwl(language) {
this.language = 'unknown';
if (language !== undefined)
this.language = language;
this.tasks = {};
this.tasks.escapeRegExp = function(str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
};
this.tasks.removeCharacters = function(charArray, str) {
for (var ii = 0; ii < charArray.length; ii++)
str = str.replace(new RegExp(knwl.tasks.escapeRegExp(charArray[ii]), 'g'), '');
return str;
};
this.tasks.search = function(terms, words) {
var results = [];
for (var i = 0; i < words.length; i++) {
for (var e = 0; e < terms.length; e++) {
var curHol = terms[e];
var pos = i;
if (words[pos] === curHol[0]) {
for (var x = 0; x < curHol.length; x++) {
if (words[pos] === curHol[x]) {
if (x === curHol.length - 1) {
results.push(curHol);
}
}
pos++;
}
}
}
}
return results;
};
this.tasks.preview = function(position) { // used to get the entire sentence or a portion of it (depending on size), in a human-readable format, from a position
var words = knwl.words.linkWordsCasesensitive;
var sentence = '';
var startPos = position;
var endPos = position;
for (var ii = position; ii > -1; ii--) {
startPos = ii;
if (words[ii][words[ii].length - 1] !== undefined) {
if (words[ii][words[ii].length - 1].search(/[?!.]/g) !== -1) {
if (position - startPos > 0)
startPos = ii + 1;
break;
} else if (position - startPos > 10) {
break;
}
}
}
for (var ii = position; ii < words.length; ii++) {
endPos = ii;
if (words[ii][words[ii].length - 1] !== undefined) {
if (words[ii][words[ii].length - 1].search(/[?!.]/g) !== -1) {
break;
} else if (endPos - position > 10) {
break;
}
}
}
sentence += words[startPos];
for (var ii = startPos + 1; ii <= endPos; ii++) {
sentence += ' ' + words[ii];
}
return sentence;
};
this.words = {
words: [],
linkWords: [],
linkWordsCasesensitive: []
};
this.words.get = function(typeStr) { //retrieve words from database
if (knwl.words[typeStr] !== undefined) {
return knwl.words[typeStr].concat([]);
}
};
this.words.getSentence = function(pos, typeStr) { //used to get the entire sentence a position occurs in, in a specific format
var fullWords = knwl.words.get('linkWordsCasesensitive');
var typeWords = knwl.words.get(typeStr);
var startPos = pos;
var begin = 0;
var sentence = [];
for (var ii = startPos; ii > -1; ii--) {
if (fullWords[ii][fullWords[ii].length - 1].search(/[?!.]/g) !== -1) {
if (startPos - begin > 0)
begin = ii + 1;
break;
}
}
var end = 0;
for (var ii = startPos; ii < fullWords.length; ii++) {
end = ii;
if (fullWords[ii][fullWords[ii].length - 1].search(/[?!.]/g) !== -1) {
break;
}
}
for (var ii = begin; ii <= end; ii++) {
sentence.push(typeWords[ii]);
}
console.log(sentence);
};
this.init = function(str) {
var lowercase = str.toLowerCase();
var linkWords = lowercase.split(/[ \n]+/);
var linkWordsCaseSensitive = str.split(/[ \n]+/);
lowercase = lowercase.split(/[\n ]+/);
for (var ii = 0; ii < lowercase.length; ii++)
lowercase[ii] = lowercase[ii].replace(/[ ,?!]/g, '').replace(/["]/g, "'");
var words = [];
for (var ii = 0; ii < lowercase.length; ii++)
words[ii] = lowercase[ii].split(/[.,!?]+/)[0];
knwl.words.linkWordsCasesensitive = linkWordsCaseSensitive;
knwl.words.linkWords = linkWords;
knwl.words.words = words;
return knwl.words;
};
this.get = function(parser) {
if (this.plugins[parser] !== undefined) {
try {
var args = arguments;
var data = knwl.plugins[parser].calls(args);
return data;
} catch (error) {
console.error('Knwl.js Error', 'Error running parser plugin "' + parser + '"', error);
return false;
}
} else {
console.error('Knwl.js Error', 'Parser plugin "' + parser + '" not found.');
return false;
}
};
this.plugins = {};
this.register = function (name, Plugin) {
knwl.plugins[name] = new Plugin(knwl);
if (knwl.plugins[name].languages !== undefined && knwl.language !== 'unknown') {
if (knwl.plugins[name].languages[knwl.language] === undefined || knwl.plugins[name].languages[knwl.language] === false) {
return {'Knwl.js Error': 'Parser plugin does not seem to support the specified language.'};
}
}
return knwl;
};
var knwl = this;
// load default plugins
this.register('dates', require('./default_plugins/dates'));
this.register('times', require('./default_plugins/times'));
this.register('emails', require('./default_plugins/emails'));
this.register('links', require('./default_plugins/links'));
this.register('phones', require('./default_plugins/phones'));
this.register('places', require('./default_plugins/places'));
};
module.exports = Knwl;