Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b23e1c3

Browse files
committed
计算编辑距离,去停用词
1 parent 9979984 commit b23e1c3

4 files changed

Lines changed: 31 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# 3.10
2+
* 计算编辑距离时去停用词
3+
4+
# 3.9
5+
* fix bug
6+
17
# 3.8
28
* 获得一个分词后句子的向量,向量以BoW方式组成
39

Requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
synonyms>=3.6
1+
synonyms>=3.10

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
setup(
1515
name='synonyms',
16-
version='3.8.0',
16+
version='3.10.0',
1717
description='Chinese Synonyms for Natural Language Processing and Understanding',
1818
long_description=LONGDOC,
1919
author='Hai Liang Wang, Hu Ying Xi',

synonyms/synonyms.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
'''
7979
tokenizer_dict = os.path.join(curdir, 'data', 'vocab.txt')
8080
if "SYNONYMS_WORDSEG_DICT" in ENVIRON:
81-
if os.exist(ENVIRON["SYNONYMS_WORDSEG_DICT"]):
81+
if os.path.exists(ENVIRON["SYNONYMS_WORDSEG_DICT"]):
8282
print("info: set wordseg dict with %s" % tokenizer_dict)
8383
tokenizer_dict = ENVIRON["SYNONYMS_WORDSEG_DICT"]
8484
else: print("warning: can not find dict at [%s]" % tokenizer_dict)
@@ -303,23 +303,43 @@ def nearby(word):
303303
_cache_nearby[w] = (words, scores)
304304
return words, scores
305305

306-
def compare(s1, s2, seg=True, ignore=False):
306+
def compare(s1, s2, seg=True, ignore=False, stopwords=False):
307307
'''
308308
compare similarity
309309
s1 : sentence1
310310
s2 : sentence2
311311
seg : True : The original sentences need jieba.cut
312312
Flase : The original sentences have been cut.
313+
ignore: True: ignore OOV words
314+
False: get vector randomly for OOV words
313315
'''
314316
if s1 == s2: return 1.0
317+
318+
s1_words = []
319+
s2_words = []
320+
315321
if seg:
316322
s1 = [x for x in jieba.cut(s1)]
317323
s2 = [x for x in jieba.cut(s2)]
318324
else:
319325
s1 = s1.split()
320326
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+
321341
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)
323343

324344
def display(word):
325345
print("'%s'近义词:" % word)

0 commit comments

Comments
 (0)