-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
133 lines (120 loc) · 3.04 KB
/
index.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
'use strict'
let helpers = require("./helpers")
helpers.load_or_unzip(function(data) {
//
//some helper methods
let fast_search = function(str, k) {
let founds = []
let l = data[k].length;
for (let i = 0; i < l; i++) {
for (let o = 0; o < data[k][i].words.length; o++) {
if (data[k][i].words[o] === str) {
founds.push(data[k][i])
break
}
}
}
return founds
}
let is_id = function(str) {
return str.match(/[a-z]\.(adjective|verb|noun|adverb)\.[0-9]/i) !== null
}
let id_lookup = function(id, k) {
let l = data[k].length;
for (let i = 0; i < l; i++) {
if (data[k][i].id === id) {
return [data[k][i]]
}
}
return null
}
let lookup = function(str, k) {
//given an id
if (is_id(str)) {
let type = str.match(/[a-z]\.(adjective|verb|noun|adverb)\.[0-9]/i)[1]
return id_lookup(str, type)
}
//given a pos
if (k) {
if (str) {
return fast_search(str, k)
}
return data[k]
}
//else, lookup in all types
let types = ["adverb", "adjective", "verb", "noun"]
let all = []
for (let i = 0; i < types.length; i++) {
all = all.concat(fast_search(str, types[i]))
}
return all
}
//
//begin API now
exports.lookup = lookup
exports.data = data
//main methods
exports.adverb = function(s) {
return lookup(s, "adverb")
}
exports.adjective = function(s) {
return lookup(s, "adjective")
}
exports.verb = function(s) {
return lookup(s, "verb")
}
exports.noun = function(s) {
return lookup(s, "noun")
}
exports.synonyms = function(s) {
return lookup(s, "adjective").map(function(syn) {
let loose = syn.similar.map(function(id) {
return lookup(id, "adjective")[0].words
})
return {
synset: syn.id,
close: syn.words.filter(function(w) {
return w !== s
}),
far: helpers.flatten(loose).filter(function(w) {
return w !== s
})
}
})
}
exports.antonyms = function(s) {
let ants = lookup(s, "adjective").map(function(syn) {
return syn.antonym
})
ants = helpers.unique(helpers.flatten(ants))
let all = ants.map(function(id) {
return lookup(id, "adjective")[0]
})
return all
}
exports.pos = function(s) {
return helpers.unique(lookup(s).map(function(syn) {
return syn.syntactic_category
}))
}
exports.words = function(cb) {
helpers.load_or_unzip((obj)=>{
let keys=Object.keys(obj)
let words={}
for (let i=0; i<keys.length; i++){
for (let o=0; o<obj[keys[i]].length; o++){
for (let w=0; w<obj[keys[i]][o].words.length; w++){
words[obj[keys[i]][o].words[w]]=true
}
}
}
cb(Object.keys(words).sort())
})
}
})
// console.log(exports.pos("perverse"))
// console.log(exports.antonyms("perverse"))
// exports.words((arr)=>{
// console.log(arr.filter((s)=> s.match(/cool/)))
// })
// exports.words((arr)=>{console.log(arr.slice(110,113))})