|
78 | 78 | ''' |
79 | 79 | tokenizer_dict = os.path.join(curdir, 'data', 'vocab.txt') |
80 | 80 | if "SYNONYMS_WORDSEG_DICT" in ENVIRON: |
81 | | - if os.exist(ENVIRON["SYNONYMS_WORDSEG_DICT"]): |
| 81 | + if os.path.exists(ENVIRON["SYNONYMS_WORDSEG_DICT"]): |
82 | 82 | print("info: set wordseg dict with %s" % tokenizer_dict) |
83 | 83 | tokenizer_dict = ENVIRON["SYNONYMS_WORDSEG_DICT"] |
84 | 84 | else: print("warning: can not find dict at [%s]" % tokenizer_dict) |
@@ -303,23 +303,43 @@ def nearby(word): |
303 | 303 | _cache_nearby[w] = (words, scores) |
304 | 304 | return words, scores |
305 | 305 |
|
306 | | -def compare(s1, s2, seg=True, ignore=False): |
| 306 | +def compare(s1, s2, seg=True, ignore=False, stopwords=False): |
307 | 307 | ''' |
308 | 308 | compare similarity |
309 | 309 | s1 : sentence1 |
310 | 310 | s2 : sentence2 |
311 | 311 | seg : True : The original sentences need jieba.cut |
312 | 312 | Flase : The original sentences have been cut. |
| 313 | + ignore: True: ignore OOV words |
| 314 | + False: get vector randomly for OOV words |
313 | 315 | ''' |
314 | 316 | if s1 == s2: return 1.0 |
| 317 | + |
| 318 | + s1_words = [] |
| 319 | + s2_words = [] |
| 320 | + |
315 | 321 | if seg: |
316 | 322 | s1 = [x for x in jieba.cut(s1)] |
317 | 323 | s2 = [x for x in jieba.cut(s2)] |
318 | 324 | else: |
319 | 325 | s1 = s1.split() |
320 | 326 | s2 = s2.split() |
| 327 | + |
| 328 | + # check stopwords |
| 329 | + if not stopwords: |
| 330 | + global _stopwords |
| 331 | + for x in s1: |
| 332 | + if not x in _stopwords: |
| 333 | + s1_words.append(x) |
| 334 | + for x in s2: |
| 335 | + if not x in _stopwords: |
| 336 | + s2_words.append(x) |
| 337 | + else: |
| 338 | + s1_words = s1 |
| 339 | + s2_words = s2 |
| 340 | + |
321 | 341 | assert len(s1) > 0 and len(s2) > 0, "The length of s1 and s2 should > 0." |
322 | | - return _similarity_distance(s1, s2, ignore) |
| 342 | + return _similarity_distance(s1_words, s2_words, ignore) |
323 | 343 |
|
324 | 344 | def display(word): |
325 | 345 | print("'%s'近义词:" % word) |
|
0 commit comments