python - 使用模糊python删除重复的近似词匹配

标签 python pandas fuzzy

我想问一下如何使用python中的模糊或任何可行的方法删除重复的近似单词匹配。我有一个Excel,其中包含近似相似的名称,此时,我想删除包含高相似度的名称,只保留一个名称。

例如,这是输入(excel文件),总共有6行5列:

|-------------------|-----|-----|-----|-----|-----|  
| abby_john         | abc | abc | abc | abc | abc |
|-------------------|-----|-----|-----|-----|-----|  
| abby_johnny       | def | def | def | def | def |  
|-------------------|-----|-----|-----|-----|-----|  
| a_j               | ghi | ghi | ghi | ghi | ghi |  
|-------------------|-----|-----|-----|-----|-----|  
| abby_(john)       | abc | abc | abc | abc | abc |  
|-------------------|-----|-----|-----|-----|-----|  
| john_abby_doe     | def | def | def | def | def | 
|-------------------|-----|-----|-----|-----|-----|  
| aby_/_John_Doedy  | ghi | ghi | ghi | ghi | ghi |  
|-------------------|-----|-----|-----|-----|-----|  

虽然上面所有的名称看起来不同,但它们实际上是相同的,python 应该如何知道它们都是相同的并删除重复的名称并保留名称的 ANY ONE 并保留其整行?顺便说一句,输入文件是 Excel 文件格式 (.xlsx)。

期望的输出:

|-------------------|-----|-----|-----|-----|-----|  
| abby_john         | abc | abc | abc | abc | abc |
|-------------------|-----|-----|-----|-----|-----|  

由于下划线不是很重要,因此可以将其替换为“空格”,因此可以接受以下另一个输出: 另一个所需的输出:

|-------------------|-----|-----|-----|-----|-----|  
| abby_john         | abc | abc | abc | abc | abc |
|-------------------|-----|-----|-----|-----|-----|  

如果有人能帮助我,非常感谢,谢谢!

最佳答案

这是一类名为 semantic similarity 的问题.

获取数据:

from io import StringIO
s = StringIO("""abby_john         abc   abc   abc   abc 
abby_johnny       def   def   def   def 
a_j               ghi   ghi   ghi   ghi 
abby_(john)       abc   abc   abc   abc 
abby_john_doe     def   def   def   def 
aby_John_Doedy    ghi   ghi   ghi   ghi
abby john         ghi   ghi   ghi   ghi
john_abby_doe     def   def   def   def
aby_/_John_Doedy  ghi   ghi   ghi   ghi
doe jane          abc   abc   abc   abc
doe_jane          def   def   def   def""")

import pandas as pd
df = pd.read_fwf(s,header=None,sep='\s+')
lst_original = df[0].tolist() # the first column 

Vectorize (转为数字表示):

import numpy as np 
from gensim.models import Word2Vec

m = Word2Vec(lst_original,size=50,min_count=1,cbow_mean=1)  
def vectorizer(sent,m): 
    vec = [] 
    numw = 0 
    for w in sent: 
        try: 
            if numw == 0: 
                vec = m[w] 
            else: 
                vec = np.add(vec, m[w]) 
            numw += 1 
        except Exception as e: 
            print(e) 
    return np.asarray(vec) / numw 

l = []
for i in lst_original:
    l.append(vectorizer(i,m))

X = np.array(l)

KMeans clustering :

from sklearn.cluster import KMeans

clf = KMeans(n_clusters=2,init='k-means++',n_init=100,random_state=0)
labels = clf.fit_predict(X)

然后我们只得到集群交替的值:

previous_cluster = 0
for index, sentence in enumerate(lst_original):
    if index > 0:
        previous_cluster = labels[index - 1]
    cluster = labels[index]
    if previous_cluster != cluster:
        print(str(labels[index]) + ":" + str(sentence))

结果,如您所见,a_j 的处理方式与 abby_john 组中的其他成员不同:

1:a_j
0:abby_(john)
1:doe jane

关于python - 使用模糊python删除重复的近似词匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61858903/

相关文章:

python - 如何将我的函数导入到 python 文件并从中获取输入?

python - 如何在 TurboGears 2.2.2 的 RootController 中使用自定义 sys.excepthook

python - 追加两个dataframe并继续分配Id

Ruby, FuzzBert, 无法将 Proc 转换为 String (TypeError)

python - 在 OSX 上使用格式语言的德语数字分隔符?

Python - 将 dict 转储为 json 字符串

python - 平滑绘制数据框的所有列

python - py/pandas dataframe - 如何将每日收盘价更改为每月收盘价?

string-comparison - 如何检查两个非结构化街道地址字符串是否相同?