nltk.jaccard_distance 函数几乎总是输出 1.0
nltk.jaccard_distance function almost always outputs 1.0
我正在尝试获取从书籍中提取的两个关键字字符串之间的 jaccard 距离。由于某种原因,nltk.jaccard_distance() 函数几乎总是输出 1.0
以下是我如何预处理关键字:
def preprocess(text):
# make sure to use the right encoding
text = text.encode("utf-8")
# remove digits and punctuation
text = re.sub('[^A-Za-z]+', ' ', text)
# remove duplicate words
# note that these aren't sentences, they are strings of keywords
text = set(text.split())
text = ' '.join(text)
# tokenize
text = nltk.word_tokenize(text)
# create sets of n-grams
text = set(nltk.ngrams(text, n=3))
return text
这里是我做比较的地方:
def getJaccardSimilarity(keyword_list_1, keyword_list_2):
keywordstokens_2 = preprocess(keyword_list_2)
keywordstokens_1 = preprocess(keyword_list_1)
if len(keywordstokens_1) > 0 and len(keywordstokens_2) > 0:
return nltk.jaccard_distance(keywordstokens_1, keywordstokens_2)
else:
return 0
当我查看结果时,相似度几乎总是 1.0,我认为这意味着两本书之间的 n-gram 是相同的。这是我刚刚打印出来的一些示例数据:
KEYWORDS_1:
set([('laser', 'structur', 'high'), ('high', 'electron', 'halo'), ('atom', 'nuclei', 'helium'), ('nuclei', 'helium', 'neutron'), ('halo', 'atom', 'nuclei'), ('precis', 'laser', 'structur'), ('structur', 'high', 'electron'), ('electron', 'halo', 'atom')])
KEYWORDS_2:
set([('quantum', 'line', 'experi'), ('bench', 'magnet', 'survey'), ('trap', 'tabl', 'quantum'), ('tabl', 'quantum', 'line'), ('use', 'optic', 'trace'), ('line', 'experi', 'cold'), ('trace', 'straight', 'becaus'), ('survey', 'trap', 'tabl'), ('magnet', 'survey', 'trap'), ('straight', 'becaus', 'bench'), ('experi', 'cold', 'requir'), ('optic', 'trace', 'straight'), ('becaus', 'bench', 'magnet')])
SIMILARITY:
1.0
我不太确定我错过了什么。感谢任何帮助。
你计算的是杰卡德距离,而不是相似度。因此,恰恰相反:距离为 0 表示您的集合相同,而距离为 1.0 表示它们的交集为空。
或者,换句话说:similarity(x, y) = 1 - distance(x, y)
我正在尝试获取从书籍中提取的两个关键字字符串之间的 jaccard 距离。由于某种原因,nltk.jaccard_distance() 函数几乎总是输出 1.0
以下是我如何预处理关键字:
def preprocess(text):
# make sure to use the right encoding
text = text.encode("utf-8")
# remove digits and punctuation
text = re.sub('[^A-Za-z]+', ' ', text)
# remove duplicate words
# note that these aren't sentences, they are strings of keywords
text = set(text.split())
text = ' '.join(text)
# tokenize
text = nltk.word_tokenize(text)
# create sets of n-grams
text = set(nltk.ngrams(text, n=3))
return text
这里是我做比较的地方:
def getJaccardSimilarity(keyword_list_1, keyword_list_2):
keywordstokens_2 = preprocess(keyword_list_2)
keywordstokens_1 = preprocess(keyword_list_1)
if len(keywordstokens_1) > 0 and len(keywordstokens_2) > 0:
return nltk.jaccard_distance(keywordstokens_1, keywordstokens_2)
else:
return 0
当我查看结果时,相似度几乎总是 1.0,我认为这意味着两本书之间的 n-gram 是相同的。这是我刚刚打印出来的一些示例数据:
KEYWORDS_1:
set([('laser', 'structur', 'high'), ('high', 'electron', 'halo'), ('atom', 'nuclei', 'helium'), ('nuclei', 'helium', 'neutron'), ('halo', 'atom', 'nuclei'), ('precis', 'laser', 'structur'), ('structur', 'high', 'electron'), ('electron', 'halo', 'atom')])
KEYWORDS_2:
set([('quantum', 'line', 'experi'), ('bench', 'magnet', 'survey'), ('trap', 'tabl', 'quantum'), ('tabl', 'quantum', 'line'), ('use', 'optic', 'trace'), ('line', 'experi', 'cold'), ('trace', 'straight', 'becaus'), ('survey', 'trap', 'tabl'), ('magnet', 'survey', 'trap'), ('straight', 'becaus', 'bench'), ('experi', 'cold', 'requir'), ('optic', 'trace', 'straight'), ('becaus', 'bench', 'magnet')])
SIMILARITY:
1.0
我不太确定我错过了什么。感谢任何帮助。
你计算的是杰卡德距离,而不是相似度。因此,恰恰相反:距离为 0 表示您的集合相同,而距离为 1.0 表示它们的交集为空。
或者,换句话说:similarity(x, y) = 1 - distance(x, y)