Python 删除停止字/标点符号,标记化并应用计数器()
我编写了一个函数,用于删除stopwords并标记如下:Python 删除停止字/标点符号,标记化并应用计数器(),python,python-3.x,string,counter,tokenize,Python,Python 3.x,String,Counter,Tokenize,我编写了一个函数,用于删除stopwords并标记如下: 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()] punct = li
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()]
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))
我将其应用于一个专栏tweet['cleaned_text']
,如下所示:
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()]
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
正如您所见,我无法摆脱标签#
,即使它包含在标点符号列表中(一些停止词也很明显)#土地管理局和土地管理局本应相同,但却重复计算
我一定是在代码中遗漏了什么。当你处理代币时,你保留了整个单词,如果你想去掉前导的。
你可以使用str.strip(“#”)
您只是在筛选,因此如果认为整个单词有效,则会保留该单词。。你想做一些类似于“tok.strip”(“#”)的动作来去掉礼物中的那个字符吗?非常感谢,你的建议很管用。然而,我仍然没有过滤掉停止词。有什么建议吗?你能给我一个合适的答案吗?谢谢好的,我想我需要澄清一下,你说的停止语是什么意思?你能给出一个列表中停止词的例子吗?哪些不应该是由于停止词而出现的?我不认为我完全理解这个问题。好吧,我认为“(一些停止词也很明显)”把我甩了,我不确定是否还有更多的错误,很高兴我能帮助:)