gpt4 book ai didi

python - 使用字典替换数据框中的互联网首字母缩略词

转载 作者:太空宇宙 更新时间:2023-11-03 11:16:03 27 4
gpt4 key购买 nike

我正在从事一个文本挖掘项目,我正在尝试使用手动准备的字典替换文本(在数据框列中)中出现的缩写词、俚语和互联网首字母缩略词。

我面临的问题是代码以数据框列中文本的第一个单词停止,并且没有用字典中的查找单词替换它

这是我使用的示例字典和代码:

abbr_dict = {"abt":"about", "b/c":"because"}

def _lookup_words(input_text):
words = input_text.split()
new_words = []
for word in words:
if word.lower() in abbr_dict:
word = abbr_dict[word.lower()]
new_words.append(word)
new_text = " ".join(new_words)
return new_text
df['new_text'] = df['text'].apply(_lookup_words)

示例输入:

df['text'] =
However, industry experts are divided ab whether a Bitcoin ETF is necessary or not.

期望的输出:

df['New_text'] =
However, industry experts are divided about whether a Bitcoin ETF is necessary or not.

当前输出:

df['New_text'] =
However

最佳答案

您可以尝试使用 lambdajoin 以及 split:

import pandas as pd

abbr_dict = {"abt":"about", "b/c":"because"}

df = pd.DataFrame({'text': ['However, industry experts are divided abt whether a Bitcoin ETF is necessary or not.']})

df['new_text'] = df['text'].apply(lambda row: " ".join(abbr_dict[w]
if w.lower() in abbr_dict else w for w in row.split()))

或者要修复上面的代码,我认为您需要将 new_textreturn 语句的 join 移到 之外for 循环:

def _lookup_words(input_text):
words = input_text.split()
new_words = []
for word in words:
if word.lower() in abbr_dict:
word = abbr_dict[word.lower()]
new_words.append(word)
new_text = " ".join(new_words) # ..... change here
return new_text # ..... change here also
df['new_text'] = df['text'].apply(_lookup_words)

关于python - 使用字典替换数据框中的互联网首字母缩略词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51605300/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com