【发布时间】:2019-12-15 10:15:59
【问题描述】:
我有一个 pandas 数据框,其中包含以下列:df['adjectives']、df['nouns'] 和 df['adverbs']。这些列中的每一列都包含基于其各自词性的标记列表。
我想使用 TextBlob 在我的数据框中创建三个新列,df['adjlemmatized']、df['nounlemmatized'] 和 df['advlemmatized']。
这些列中的每一列都应包含由单数化引理形式的单词组成的单词列表。
我已尝试遵循 TextBlob 文档,但我一直在编写将迭代整个数据帧的函数。
Words Inflection and Lemmatization
Each word in TextBlob.words or Sentence.words is a Word object (a subclass of unicode) with useful methods, e.g. for word inflection.
>>> sentence = TextBlob('Use 4 spaces per indentation level.')
>>> sentence.words
WordList(['Use', '4', 'spaces', 'per', 'indentation', 'level'])
>>> sentence.words[2].singularize()
'space'
>>> sentence.words[-1].pluralize()
'levels'
Words can be lemmatized by calling the lemmatize method.
>>> from textblob import Word
>>> w = Word("octopi")
>>> w.lemmatize()
'octopus'
>>> w = Word("went")
>>> w.lemmatize("v") # Pass in WordNet part of speech (verb)
'go'
这是我用来从文本中获取词性的代码:
# get adjectives
def get_adjectives(text):
blob = TextBlob(text)
print(text)
return [word for (word,tag) in blob.tags if tag.startswith("JJ")]
df['adjectives'] = df['clean_reviews'].apply(get_adjectives)
【问题讨论】:
-
您的数据框目前有哪些列?一个形容词,一个名词,一个副词?
-
是的,但我也有一个包含派生词性词表的文本本身。
标签: python pandas text data-cleaning lemmatization