返回 social-analyzer
string-analysis.js
根目录 / modules / string-analysis.js
1 import helper from './helper.js'
2 import {findWord} from 'most-common-words-by-language'
3 import WordsNinjaPack from 'wordsninja'
4 const WordsNinja = new WordsNinjaPack()
5
6 function most_common1 (all_words, temp_words) {
7 const temp_list = []
8 Object.keys(all_words).forEach(function (key) {
9 all_words[key].forEach(function (item) {
10 if (!temp_list.includes(item) && item.length > 1) {
11 temp_list.push(item)
12 const temp = findWord(item)
13 if (Object.keys(temp).length !== 0) {
14 const languages = Object.keys(temp).map(function (key) {
15 return [key, temp[key]]
16 })
17 languages.sort(function (first, second) {
18 return second[1] - first[1]
19 }).reverse()
20 temp_words.push({
21 word: item,
22 languages: languages.map(e => e.join(':')).join(' ')
23 })
24 }
25 }
26 })
27 })
28 }
29
30 async function most_common (all_words, temp_words) {
31 const temp_list = []
32 Object.keys(all_words).forEach(function (key) {
33 all_words[key].forEach(function (item) {
34 if (!temp_list.includes(item) && item.length > 1) {
35 temp_list.push(item)
36 const temp = findWord(item)
37 if (Object.keys(temp).length !== 0) {
38 const languages = Object.keys(temp).map(function (key) {
39 return [key, temp[key]]
40 })
41 languages.sort(function (first, second) {
42 return second[1] - first[1]
43 }).reverse()
44 temp_words.push({
45 word: item,
46 languages: languages.map(e => e[0]).join(', ')
47 })
48 }
49 }
50 })
51 })
52 }
53
54 function find_other (req, all_words) {
55 const words = WordsNinja.splitSentence(req.body.string)
56
57 words.forEach(function (word) {
58 let value = false
59 Object.keys(all_words).forEach(function (key) {
60 if (all_words[key].includes(word)) {
61 value = true
62 }
63 })
64
65 if (!value && !all_words.maybe.includes(word)) {
66 all_words.maybe.push(word)
67 }
68 })
69 }
70
71 function remove_word (str, sub_string) {
72 const part1 = str.substring(0, str.indexOf(sub_string))
73 const part2 = str.substring(str.indexOf(sub_string) + sub_string.length, str.length)
74 const temp = (part1 + part2).replace(/[ \[\]:"\\|,.<>\/?~`!@#$%^&*()_+\-={};"]/gi, '')
75 return temp
76 }
77
78 async function analyze_string (req, all_words) {
79 helper.log_to_file_queue(req.body.uuid, '[Starting] String analysis')
80 let temp_rr_names = []
81 const string_to_check = req.body.string
82 helper.parsed_json.prefix.forEach(function (item, index) {
83 if (string_to_check.indexOf(item) === 0 && !all_words.prefix.includes(item)) {
84 all_words.prefix.push(item)
85 const temp = remove_word(string_to_check, item)
86 if (temp !== null && temp !== '' && !all_words.unknown.includes(temp) && !all_words.maybe.includes(temp) && temp.length > 1) {
87 all_words.unknown.push(temp)
88 }
89 }
90 })
91 helper.parsed_json.m_names.forEach(function (item, index) {
92 if (string_to_check.indexOf(item) >= 0 && !all_words.name.includes(item)) {
93 all_words.name.push(item)
94 const temp = remove_word(string_to_check, item)
95 if (temp !== null && temp !== '' && !all_words.unknown.includes(temp) && !all_words.maybe.includes(temp) && temp.length > 1) {
96 all_words.unknown.push(temp)
97 }
98 }
99 })
100 helper.parsed_json.f_names.forEach(function (item, index) {
101 if (string_to_check.indexOf(item) >= 0 && !all_words.name.includes(item)) {
102 all_words.name.push(item)
103 const temp = remove_word(string_to_check, item)
104 if (temp !== null && temp !== '' && !all_words.unknown.includes(temp) && !all_words.maybe.includes(temp) && temp.length > 1) {
105 all_words.unknown.push(temp)
106 }
107 }
108 })
109
110 all_words.prefix.forEach(function (h_item, index) {
111 all_words.unknown.forEach(function (r_item, index) {
112 if (r_item.indexOf(h_item) === 0) {
113 const temp = remove_word(r_item, h_item)
114 if (temp !== null && temp !== '' && !temp_rr_names.includes(temp) && !all_words.maybe.includes(temp) && temp.length > 1) {
115 temp_rr_names.push(temp)
116 }
117 }
118 })
119 })
120
121 let temp_r_concat = all_words.unknown.concat(temp_rr_names.filter((item) => all_words.unknown.indexOf(item) < 0))
122
123 all_words.unknown = temp_r_concat
124 temp_rr_names = []
125
126 all_words.number.forEach(function (n_item, index) {
127 all_words.unknown.forEach(function (r_item, index) {
128 if (r_item.indexOf(n_item) >= 0) {
129 const temp = remove_word(r_item, n_item)
130 if (temp !== null && temp !== '' && !temp_rr_names.includes(temp) && !all_words.maybe.includes(temp) && temp.length > 1) {
131 temp_rr_names.push(temp)
132 }
133 }
134 })
135 })
136
137 temp_r_concat = all_words.unknown.concat(temp_rr_names.filter((item) => all_words.unknown.indexOf(item) < 0))
138 all_words.unknown = temp_r_concat
139 helper.log_to_file_queue(req.body.uuid, '[Done] String analysis')
140 }
141
142 async function split_comma (req, all_words) {
143 try {
144 req.body.string.split(',').forEach((item) => {
145 if (item.length > 1 && !all_words.unknown.includes(item) && !all_words.maybe.includes(item)) {
146 all_words.unknown.push(item.toLowerCase())
147 }
148 })
149 } catch (err) {}
150 }
151
152 async function split_upper_case (req, all_words) {
153 try {
154 req.body.string.match(/[A-Z][a-z]+/g).forEach((item) => {
155 if (item.length > 1 && !all_words.unknown.includes(item) && !all_words.maybe.includes(item)) {
156 all_words.unknown.push(item.toLowerCase())
157 }
158 })
159 } catch (err) {}
160 }
161
162 async function split_alphabet_case (req, all_words) {
163 try {
164 req.body.string.match(/[A-Za-z]+/g).forEach((item) => {
165 if (item.length > 1 && !all_words.unknown.includes(item) && !all_words.maybe.includes(item)) {
166 all_words.unknown.push(item.toLowerCase())
167 }
168 })
169 } catch (err) {}
170 }
171
172 async function find_symbols (req, all_words) {
173 try {
174 req.body.string.match(/[ \[\]:"\\|,.<>\/?~`!@#$%^&*()_+\-={};']/gi).forEach((item) => {
175 if (item !== ' ' && !all_words.symbol.includes(item)) {
176 all_words.symbol.push(item)
177 }
178 })
179 } catch (err) {}
180 }
181
182 async function find_numbers (req, all_words) {
183 try {
184 req.body.string.match(/(\d+)/g).forEach((item) => {
185 if (!all_words.number.includes(item)) {
186 all_words.number.push(item)
187 }
188 })
189 } catch (err) {}
190 }
191
192 async function convert_numbers (req) {
193 try {
194 const numbers_to_letters = {
195 4: 'a',
196 8: 'b',
197 3: 'e',
198 1: 'l',
199 0: 'o',
200 5: 's',
201 7: 't',
202 2: 'z'
203 }
204
205 let temp_value = ''
206 for (let i = 0; i < req.body.string.length; i++) {
207 const _temp = numbers_to_letters[req.body.string.charAt(i)]
208 if (_temp !== undefined) {
209 temp_value += numbers_to_letters[req.body.string.charAt(i)]
210 } else {
211 temp_value += req.body.string.charAt(i)
212 }
213 }
214 req.body.string = temp_value
215 } catch (err) {}
216 }
217
218 async function get_maybe_words (req, all_words) {
219 await WordsNinja.loadDictionary()
220 all_words.maybe = await WordsNinja.splitSentence(req.body.string).filter(function (elem, index, self) {
221 return index === self.indexOf(elem)
222 }).filter(word => word.length > 1)
223 }
224
225 async function guess_age_from_string(req) {
226 let results = []
227
228 try {
229 let age_4_numbers = /\d{4}|\d{2}/g
230 let current_year = new Date().getFullYear()
231 while ((match = age_4_numbers.exec(req.body.string)) != null) {
232 let temp_dict = {"found":"","year":"","age":""}
233 let found = 0
234 let year = 0
235 let age = 0
236 temp_dict.found = match[0]
237 found = parseInt(match[0])
238 if (found >= 50 && found <= 99){
239 year = found + 1900
240 age = current_year - year
241 if (age <= 75){
242 temp_dict.year = year.toString()
243 temp_dict.age = age.toString()
244 }
245 }
246 if (found >= 14 && found <= 49){
247 year = current_year - found
248 age = found
249 if (age <= 75){
250 temp_dict.year = year.toString()
251 temp_dict.age = age.toString()
252 }
253 }
254 if (found >= 1950){
255 year = found
256 age = current_year - year
257 if (age <= 75){
258 temp_dict.year = year.toString()
259 temp_dict.age = age.toString()
260 }
261 }
262
263 results.push(temp_dict)
264 }
265
266 } catch (err) {
267 }
268
269 return results
270 }
271
272 export default{
273 get_maybe_words,
274 find_symbols,
275 find_numbers,
276 convert_numbers,
277 split_comma,
278 split_upper_case,
279 split_alphabet_case,
280 most_common,
281 find_other,
282 analyze_string,
283 guess_age_from_string
284 }
285
285 lines JAVASCRIPT