如何使用python将tweets中的短单词替换为完整单词

2024-10-01 10:12:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我在微博上做情绪分析。大多数tweet都包含短单词,我想将它们替换为原始/完整单词。在

假设tweet是:

I was wid Ali.

我想转换:

^{pr2}$

同样

wud -> would
u -> you
r -> are

我有6000条微博,里面有很多短句。 我怎样才能替换它们?python中是否有可用于此任务的库?或者网上有什么短词词典?在

我读了Replace appostrophe/short words in python问题的答案,但它只提供了一个近似字典。在

目前我正在使用NLTK,但是这个任务不可能与NLTK一起使用。在


Tags: youali单词are词典tweet情绪nltk
1条回答
网友
1楼 · 发布于 2024-10-01 10:12:18

以下网站似乎有必要的字典: https://www.noslang.com/search 您可以发送来自python代码的请求并返回翻译。在

工作代码如下:

import requests
prefixStr = '<div class="translation-text">'
postfixStr = '</div'

slangText = 'I was wid Ali.'

r = requests.post('https://www.noslang.com/', {'action': 'translate', 'p': 
slangText, 'noswear': 'noswear', 'submit': 'Translate'})
startIndex = r.text.find(prefixStr)+len(prefixStr)
endIndex = startIndex + r.text[startIndex:].find(postfixStr)
print(r.text[startIndex:endIndex])

相关问题 更多 >