Python 词干提取(使用 pandas 数据框)

标签 python pandas nlp stemming

我创建了一个数据框,其中包含要提取的句子。 我想使用 Snowballstemmer 通过我的分类算法获得更高的准确性。我怎样才能做到这一点?

import pandas as pd
from nltk.stem.snowball import SnowballStemmer

# Use English stemmer.
stemmer = SnowballStemmer("english")

# Sentences to be stemmed.
data = ["programmers program with programming languages", "my code is working so there must be a bug in the interpreter"] 
    
# Create the Pandas dataFrame.
df = pd.DataFrame(data, columns = ['unstemmed']) 

# Split the sentences to lists of words.
df['unstemmed'] = df['unstemmed'].str.split()

# Make sure we see the full column.
pd.set_option('display.max_colwidth', -1)

# Print dataframe.
df 

+----+---------------------------------------------------------------+
|    | unstemmed                                                     |
|----+---------------------------------------------------------------|
|  0 | ['programmers', 'program', 'with', 'programming', 'languages']|
|  1 | ['my', 'code', 'is', 'working', 'so', 'there', 'must',        |  
|    |  'be', 'a', 'bug', 'in', 'the', 'interpreter']                |
+----+---------------------------------------------------------------+

最佳答案

您必须对每个单词应用词干提取并将其存储到“词干提取”列中。

df['stemmed'] = df['unstemmed'].apply(lambda x: [stemmer.stem(y) for y in x]) # Stem every word.
df = df.drop(columns=['unstemmed']) # Get rid of the unstemmed column.
df # Print dataframe.

+----+--------------------------------------------------------------+
|    | stemmed                                                      |
|----+--------------------------------------------------------------|
|  0 | ['program', 'program', 'with', 'program', 'languag']         |
|  1 | ['my', 'code', 'is', 'work', 'so', 'there', 'must',          |   
|    |  'be', 'a', 'bug', 'in', 'the', 'interpret']                 |
+----+--------------------------------------------------------------+

关于Python 词干提取(使用 pandas 数据框),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37443138/

相关文章:

python - show() 不再重绘

python-3.x - 如何使用 Pandas 在 Python 中对字典中的数据进行排序

python - 在 Pandas 数据框中将第二行移到上方一行

python - 根据行索引创建名为 "Id"的列

javascript - 如何使用 IBM Watson 情感分析来分析任何文本

Python 从 url 逐行下载大型 csv 文件,只有 10 个条目

javascript - ES6命名导入引入了const?

python - 查找 HTML 页面中与特定 URL 模板匹配的所有链接

r - 自然语言识别并赋值为 "en", "fr", "tr"

python - NLTK 使用的实际例子