我有一个熊猫栏df_travail[line_text]
,上面有文字。
我想对本专栏中的每个单词进行词法化。
首先我将文本小写:
df_travail ['lowercase'] = df_travail['line_text'].str.lower()
然后,我将其标记化并应用POS(因为wordnet默认配置将每个单词都视为名词)。
from nltk import word_tokenize, pos_tag
tok_and_tag = lambda x: pos_tag(word_tokenize(x))
df_travail ['tok_and_tag'] = df_travail['lowercase'].apply(tok_and_tag)
然后我有以下内容:(提取整个df_travail['tok_and_tag']
"[('so', 'RB'), ('you', 'PRP'), (""'ve"", 'VBP'), ('come', 'VBN'), ('to', 'TO'), ('the', 'DT'), ('master', 'NN'), ('for', 'IN'), ('guidance', 'NN'), ('?', '.'), ('is', 'VBZ'), ('this', 'DT'), ('what', 'WP'), ('you', 'PRP'), (""'re"", 'VBP'), ('saying', 'VBG'), (',', ','), ('grasshopper', 'NN'), ('?', '.')]"
[('actually', 'RB'), (',', ','), ('you', 'PRP'), ('called', 'VBD'), ('me', 'PRP'), ('in', 'IN'), ('here', 'RB'), (',', ','), ('but', 'CC'), ('yeah', 'UH'), ('.', '.')]
但是,考虑到我已应用POS的事实,我迷失了要使用(与Wordnet一起)应用的词形化功能?
编辑:以下链接未提及我的问题的POS部分 Lemmatization of all pandas cells
答案 0 :(得分:0)
以下是用于词法化处理时考虑使用“ VERB”而不是“ NOUN”的示例代码:
from nltk.stem import WordNetLemmatizer
wordnet_lemmatizer = WordNetLemmatizer()
def convert(text):
lemmatized_text = []
for i in text.split():
lemmatized_text.append(str(wordnet_lemmatizer.lemmatize(i,pos="v")))
return ' '.join(lemmatized_text)
df['text'] = df['text'].apply(lambda x: convert(x))