我编写了一个函数来删除停用词并进行标记,如下所示:
def process(text, tokenizer=TweetTokenizer(), stopwords=[]):
text = text.lower()
tokens = tokenizer.tokenize(text)
return [tok for tok in tokens if tok not in stopwords and not tok.isdigit()]
我将其应用到列tweet['cleaned_text']
,如下所示:
punct = list(string.punctuation)
stopword_list = stopwords.words('english') + punct + ['rt', 'via', '...','“', '”','’']
tf = Counter()
for i in list(tweet['cleaned_text']):
temp=process(i, tokenizer=TweetTokenizer(), stopwords=stopword_list)
tf.update(temp)
for tag, count in tf.most_common(20):
print("{}: {}".format(tag, count))
输出应该是最常见的单词。这里有:
#blm: 12718
black: 2751
#blacklivesmatter: 2054
people: 1375
lives: 1255
matter: 1039
white: 914
like: 751
police: 676
get: 564
movement: 563
support: 534
one: 534
racist: 532
know: 520
us: 471
blm: 449
#antifa: 414
hate: 396
see: 382
正如您所看到的,我无法删除主题标签#
,尽管它包含在标点符号
列表中(一些停用词也很明显)。 #blm 和 blm 应该相同,但它们却被重复计算。
我一定是在代码中遗漏了一些东西。
最佳答案
当你处理标记时,你会保留整个单词,如果你想去掉前导的#
,你可以使用str.strip("#")
def process(text, tokenizer=TweetTokenizer(), stopwords=[]):
text = text.lower()
tokens = tokenizer.tokenize(text)
return [tok.strip("#") for tok in tokens if tok not in stopwords and not tok.isdigit()]
关于python - 删除停用词/标点符号,标记并应用 Counter(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62934652/