-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
95 lines (76 loc) · 3.51 KB
/
test.js
File metadata and controls
95 lines (76 loc) · 3.51 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
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
var natural = require('natural');
//stemming
stemmer = natural.PorterStemmer;
//stemmer = natural.LancasterStemmer; can be used as well
var stem = stemmer.stem('walked');
var stem1 = stemmer.stem('checked');
console.log(stem,stem1)
//tokenize and stem
stemmer.attach();
console.log('i stemmed words'.tokenizeAndStem());
console.log('i stemmed words'.tokenizeAndStem(true));//excluding stop words
/*-----------------------------------------------------------------------------------------*/
//Phonetics
phonetic = natural.Metaphone;
var wordA = 'phonetics';
var wordB = 'fonetix';
if(phonetic.compare(wordA, wordB))
console.log('they sound alike!');
//another method
phonetic.attach();
if(wordA.soundsLike(wordB))
console.log('they sound alike!');
//to get the code
console.log('phonetics'.phonetics());
console.log('phonetics rock'.tokenizeAndPhoneticize());
/*-----------------------------------------------------------------------------------------*/
//inflection
//convert nouns between singular and plural forms and turn
//intergers to string counters
nounInflector = new natural.NounInflector();
var plural = nounInflector.pluralize('radius');
console.log(plural);
var singular = nounInflector.singularize('beers');
console.log(singular);
//for shortcut we can use attach method
nounInflector.attach();
console.log('radius'.pluralizeNoun());
console.log('beers'.singularizeNoun());
//count inflector
countInflector = natural.CountInflector;
console.log(countInflector.nth(1));
console.log(countInflector.nth(2));
console.log(countInflector.nth(3));
console.log(countInflector.nth(4));
/*-----------------------------------------------------------------------------------------*/
//classification
classifier = new natural.BayesClassifier();
classifier.addDocument("my unit-tests failed.", 'software');
classifier.addDocument("tried the program, but it was buggy.", 'software');
classifier.addDocument("the drive has a 2TB capacity.", 'hardware');
classifier.addDocument("i need a new power supply.", 'hardware');
classifier.train();
console.log(classifier.classify('did the tests pass ??'))
//we can save the train model
classifier.save('classifier.json',function(err,classifier){
//classifier ll save
});
//load the saved classifier
classifier = new natural.BayesClassifier();
natural.BayesClassifier.load('classifier.json', null, function(err, classifier) {
console.log(classifier.classify('did the tests pass?'));
});
/*-------------------------------------------------------------------------------------------*/
//N-Grams :
var NGrams = natural.NGrams;
console.log(NGrams.trigrams('some words here'));
console.log(NGrams.trigrams(['some','grams','here']));
console.log(NGrams.bigrams('some words here'));
console.log(NGrams.bigrams(['some','grams','here']));
/*-----------------------------------------------------------------------------------------------*/
//string distance : "natural" supplies the Dice's coefficient, Levenshtein distance,
//and Jaro-Winkler distance algorithms for determining string similarity.
//These algorithms are concerned with orthographic (spelling) similarity, not necessarily phonetics.
console.log(natural.JaroWinklerDistance('execution', 'intention')); //higher the value --> higher the similarity
console.log(natural.LevenshteinDistance('execution', 'intention')); //lower the value --> higher the similarity
console.log(natural.DiceCoefficient('execution', 'intention')); //higher the value --> higher the similarity