且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

确定Python句子中2个单词之间的接近度

更新时间:2023-11-28 18:43:04

您可以将句子拆分为单词列表,并使用listindex方法:

You can split your sentence to list of words and use index method of list:

sentence = "the foo and the bar is foo bar"
words = sentence.split()

def get_distance(w1, w2):
     if w1 in words and w2 in words:
          return abs(words.index(w2) - words.index(w1))

更新以统计所有单词出现的次数:

Update to count all word occurrences:

import itertools

def get_distance(w1, w2):
    if w1 in words and w2 in words:
        w1_indexes = [index for index, value in enumerate(words) if value == w1]    
        w2_indexes = [index for index, value in enumerate(words) if value == w2]    
        distances = [abs(item[0] - item[1]) for item in itertools.product(w1_indexes, w2_indexes)]
        return {'min': min(distances), 'avg': sum(distances)/float(len(distances))}