【发布时间】:2017-11-28 14:57:08
【问题描述】:
我正在使用 tweepy 库来获取推文文本列表,我需要将 200 条推文中的单词与停用词列表进行比较,并删除推文文本列表中的停用词,这样我才能说出这些词是什么在搜索的推文中出现最多的。
问题是当我检索 tweet.texts 时,我必须对其进行编码才能得到它,因此我得到了一个 b'word' 列表,它无法与我的常规字符串停用词列表进行比较。
def get_tweetwords():
print("Ingrese el hashtag a buscar, no olvide escribir el numeral (#)(Ctrl+3)")
hashtag=str(input())
while (hashtag[0])!="#":
print("No olvide escribir el numeral. Vuelva a escribir el hashtag:")
hashtag=str(input())
busqueda=tweepy.Cursor(api.search,q=hashtag).items(10)
twlist=[]
listadepalabras=[]
for tweets in busqueda:
twlist.append(tweets.text.encode('utf-8'))
for i in twlist:
x=i.split()
for j in x:
listadepalabras.append(j)
return(listadepalabras)
我需要将listadepalabras 列表解码为字符串列表,以便将其与stopwords 列表进行比较并删除其停用词。
def get_stopwords():
listastopwords=[item for item in open("stopwords.txt").readlines()]
for item in listastopwords:
if "\n" in listastopwords:
listastopwords[listastopwords.index(item)]=item.replace("\n","")
return(listastopwords)
def sacar_stopwords():
listadepalabras=get_tweetwords()
listastopwords=get_stopwords()
for i in listadepalabras:
for j in listastopwords:
if i==j:
listadepalabras.remove(j)
return(listadepalabras)
这不起作用,因为我的文本列表包含 b'word' 格式的单词,而我的停用词列表只是 'word'
def repeticiones_palabra():
listadepalabras=sacar_stopwords()
diccionario=collections.Counter(listadepalabras)
diccionario=dict(diccionario.most_common(10))
print ("-LAS 10 PALABRAS MAS UTILIZADAS SON-")
print(diccionario)
这应该让我获得列表中最常用的 10 个单词,并且它运行良好,但我得到的主要是停用词和我寻找的主题标签,因此我可以知道我的停用词没有从列表中删除。
repeticiones_palabra()
我希望我说清楚了,我是 python 和堆栈溢出的初学者。提前致谢。
【问题讨论】:
-
b 是字节字符串,而不是 UTF-8 字符串。使用:b"abcde".decode("utf-8")
标签: python string list twitter utf-8