2

介绍

您好,我正在开展一个项目,该项目需要我将 pandas 文本列中的字典键替换为值 - 但可能存在拼写错误。具体来说,我在 pandas 文本列中匹配名称,并将它们替换为“名字”。例如,我会将“tommy”替换为“First Name”。

但是,我意识到字符串列中存在拼写错误的名称和文本的问题,这些字符串不会被我的字典替换。例如,“tommmmy”有额外的 m,不是我字典中的名字。

#Create df 
d = {'message' : pd.Series(['awesome', 'my name is tommmy , please help with...', 'hi tommy , we understand your quest...'])}
names = ["tommy", "zelda", "marcon"]

#create dict 
namesdict = {r'(^|\s){}($|\s)'.format(el): r'\1FirstName\2' for el in names}

#replace 
d['message'].replace(namesdict, regex = True)



  #output 
    Out: 
0                                       awesome
1    my name is tommmy , please help with...
2    hi FirstName , we understand your quest...
dtype: object

所以“tommmy”与 -> 中的“tommy”不匹配,我需要处理拼写错误。我考虑过在实际的字典键和值替换之前尝试执行此操作,例如扫描熊猫数据框并用适当的名称替换字符串列(“消息”)中的单词。我已经看到了一种类似的方法,它在像这样的特定字符串上使用索引

但是如何使用正确拼写列表匹配和替换 pandas df 中句子中的单词?我可以在 df.series replace 参数中执行此操作吗?我应该坚持使用正则表达式字符串替换吗?*

任何建议表示赞赏。

更新,尝试 Yannis 的回答

我正在尝试 Yannis 的答案,但我需要使用来自外部来源的列表,特别是美国的名字普查进行匹配。但它与我下载的字符串的全名不匹配。

d = {'message' : pd.Series(['awesome', 'my name is tommy , please help with...', 'hi tommy , we understand your quest...'])}

import requests 
r = requests.get('http://deron.meranda.us/data/census-derived-all-first.txt')

#US Census first names (5000 +) 
firstnamelist = re.findall(r'\n(.*?)\s', r.text, re.DOTALL)


#turn list to string, force lower case
fnstring = ', '.join('"{0}"'.format(w) for w in firstnamelist )
fnstring  = ','.join(firstnamelist)
fnstring  = (fnstring.lower())


##turn to list, prepare it so it matches the name preceded by either the beginning of the string or whitespace.  
names = [x.strip() for x in fnstring.split(',')]




#import jellyfish 
import difflib 


def best_match(tokens, names):
    for i,t in enumerate(tokens):
        closest = difflib.get_close_matches(t, names, n=1)
        if len(closest) > 0:
            return i, closest[0]
    return None

def fuzzy_replace(x, y):
    
    names = y # just a simple replacement list
    tokens = x.split()
    res = best_match(tokens, y)
    if res is not None:
        pos, replacement = res
        tokens[pos] = "FirstName"
        return u" ".join(tokens)
    return x

d["message"].apply(lambda x: fuzzy_replace(x, names))

结果是:

Out: 
0                                        FirstName
1    FirstName name is tommy , please help with...
2    FirstName tommy , we understand your quest...

但是,如果我使用这样的较小列表,它会起作用:

names = ["tommy", "caitlyn", "kat", "al", "hope"]
d["message"].apply(lambda x: fuzzy_replace(x, names))

是否有较长的名称列表导致了问题?

4

1 回答 1

1

编辑:

将我的解决方案更改为使用difflib. 核心思想是标记您的输入文本并将每个标记与名称列表进行匹配。如果best_match找到匹配项,则报告位置(和最佳匹配字符串),因此您可以将标记替换为“FirstName”或您想要的任何内容。请参阅下面的完整示例:

import pandas as pd
import difflib

df = pd.DataFrame(data=[(0,"my name is tommmy , please help with"), (1, "hi FirstName , we understand your quest")], columns=["A", "message"])

def best_match(tokens, names):
    for i,t in enumerate(tokens):
        closest = difflib.get_close_matches(t, names, n=1)
        if len(closest) > 0:
            return i, closest[0]
    return None

def fuzzy_replace(x):
    names = ["tommy", "john"] # just a simple replacement list
    tokens = x.split()
    res = best_match(tokens, names)
    if res is not None:
        pos, replacement = res
        tokens[pos] = "FirstName"
        return u" ".join(tokens)
    return x

df.message.apply(lambda x: fuzzy_replace(x))

你应该得到的输出如下

0    my name is FirstName , please help with
1    hi FirstName , we understand your quest
Name: message, dtype: object

编辑 2

经过讨论,我决定再试一次,使用 NLTK 进行词性标注,并仅NNP针对名称列表的标签(专有名词)运行模糊匹配。问题是有时标注器无法正确标注,例如“Hi”也可能被标记为专有名词。但是,如果名称列表是小写的,则与名称get_close_matches不匹配Hi,但与所有其他名称匹配。我建议df["message"]不要小写以增加 NLTK 正确标记名称的机会。也可以和 StanfordNER 一起玩,但没有什么能 100% 奏效。这是代码:

import pandas as pd
import difflib
from nltk import pos_tag, wordpunct_tokenize
import requests 
import re

r = requests.get('http://deron.meranda.us/data/census-derived-all-first.txt')

# US Census first names (5000 +) 
firstnamelist = re.findall(r'\n(.*?)\s', r.text, re.DOTALL)

# turn list to string, force lower case
# simplified things here
names = [w.lower() for w in firstnamelist]


df = pd.DataFrame(data=[(0,"My name is Tommmy, please help with"), 
                        (1, "Hi Tommy , we understand your question"),
                        (2, "I don't talk to Johhn any longer"),
                        (3, 'Michale says this is stupid')
                       ], columns=["A", "message"])

def match_names(token, tag):
    print token, tag
    if tag == "NNP":
        best_match = difflib.get_close_matches(token, names, n=1)
        if len(best_match) > 0:
            return "FirstName" # or best_match[0] if you want to return the name found
        else:
            return token
    else:
        return token

def fuzzy_replace(x):
    tokens = wordpunct_tokenize(x)
    pos_tokens = pos_tag(tokens)
    # Every token is a tuple (token, tag)
    result = [match_names(token, tag) for token, tag in pos_tokens]
    x = u" ".join(result)
    return x

df['message'].apply(lambda x: fuzzy_replace(x))

我进入输出:

0       My name is FirstName , please help with
1    Hi FirstName , we understand your question
2        I don ' t talk to FirstName any longer
3                 FirstName says this is stupid
Name: message, dtype: object
于 2017-07-27T18:40:08.200 回答