这是我的代码,它不起作用
from pythainlp.corpus import thai_stopwords
stopwords =thai_stopwords()
def remove_stopwords(x):
list_token = []
for i in x:
if i not in stopwords:
list_token.append(i)
return list_token
df['tokens']=df['tokens'].apply(remove_stopwords)
我已经尝试过了
df['tokens'] = df['tokens'].apply(lambda x: [item for item in x if item not in stopwords])
假设您stopwords
是一个列表,并且df['tokens']
每个单词或标记都是一个列表。
简单方法:
clear_tokens = []
for i in df.index:
clear_tokens.append([item for item in df.tokens[i] if item not in stopwords])
df['tokens'] = clear_tokens
如果您df.tokens
是每一行中的一个句子,则:
clear_tokens = []
for i in df.index:
tokenlist = df.tokens[i].split()
clear_tokens.append(' '.join([item for item in tokenlist if item not in stopwords]))
df['tokens'] = clear_tokens
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句