python - 从列表中删除密切匹配/相似的短语
问题描述
我正在努力删除列表中的类似短语,但我遇到了一个小障碍。
我有句子和短语,短语都与句子有关。一个句子的所有短语都在一个列表中。
让短语列表为:p=[['This is great','is great','place for drinks','for drinks'],['Tonight is a good','good night','is a good','for movies']]
我希望我的输出是[['This is great','place for drinks'],['Tonight is a good','for movies']]
基本上,我想获得列表中所有最长的唯一短语。
我查看了fuzzywuzzy 库,但我无法找到一个好的解决方案。
这是我的代码:
def remove_dup(arr, threshold=80):
ret_arr =[]
for item in arr:
if item[1]<threshold:
ret_arr.append(item[0])
return ret_arr
def find_important(sents=sents, phrase=phrase):
import os, random
from fuzzywuzzy import process, fuzz
all_processed = [] #final array to be returned
for i in range(len(sents)):
new_arr = [] #reshaped phrases for a single sentence
for item in phrase[i]:
new_arr.append(item)
new_arr.sort(reverse=True, key=lambda x : len(x)) #sort with highest length
important = [] #array to store terms
important = process.extractBests(new_arr[0], new_arr) #to get levenshtein distance matches
to_proc = remove_dup(important) #remove_dup removes all relatively matching terms.
to_proc.append(important[0][0]) #the term with highest match is obviously the important term.
all_processed.append(to_proc) #add non duplicates to all_processed[]
return all_processed
有人可以指出我缺少什么,或者有什么更好的方法吗?提前致谢!
解决方案
我会使用每个短语和所有其他短语之间的差异。如果一个短语与所有其他短语相比至少有一个不同的词,那么它是唯一的,应该保留。
我还使它对精确匹配和添加空格变得健壮
sentences = [['This is great','is great','place for drinks','for drinks'],
['Tonight is a good','good night','is a good','for movies'],
['Axe far his favorite brand for deodorant body spray',' Axe far his favorite brand for deodorant spray','Axe is']]
new_sentences = []
s = " "
for phrases in sentences :
new_phrases = []
phrases = [phrase.split() for phrase in phrases]
for i in range(len(phrases)) :
phrase = phrases[i]
if all([len(set(phrase).difference(phrases[j])) > 0 or i == j for j in range(len(phrases))]) :
new_phrases.append(phrase)
new_phrases = [s.join(phrase) for phrase in new_phrases]
new_sentences.append(new_phrases)
print(new_sentences)
输出:
[['这很棒','喝酒的地方'],
['今晚好','晚安','电影'],
['Axe 是他最喜欢的除臭身体喷雾品牌','Axe is']]
推荐阅读
- ios - 如何修复此错误“找不到架构‘x86_64’的模块‘CzsBleSdk’;找到:arm64,arm”
- firebase - 无法从 Firebase 函数访问数据库
- sql - 计算排名并按照顺序应用
- node.js - NodeJS在函数中调用readline
- scala - 监控具体集合的运行时使用
- javascript - 无法移除视频来源属性
- java - kotlin.TypeCastException: null 不能转换为非 null 类型 android.graphics.Bitmap
- ios - 在 iOS 上运行离子应用程序时出现 NSURLConnection 错误 1100
- amazon-web-services - 如何使两个ec2实例在内部相互连接
- php - TWIG PersistentCollection 无法转换为字符串:从 OneToMany 的表上输出数据 - ManyToOne 实体