【问题标题】:how to stem csv file using PorterStemmer in Python?如何在 Python 中使用 PorterStemmer 提取 csv 文件?
【发布时间】:2016-03-16 01:29:15
【问题描述】:

我最近正在使用 python 在 nlp 中做项目。我需要预处理一个 csv 文件,该文件包含具有许多行和列的文本。我只能只提取简单的句子。并且无法一次阻止整个 csv 文件。我怎样才能做到这一点??在尝试阻止简单的 csv 文件时出现错误

import csv
from nltk import PorterStemmer
port = PorterStemmer()

with open('status.csv', 'rb') as f:
    reader = csv.reader(f)
    for row in reader:
        print(port.stem(row))

error was

【问题讨论】:

  • @leavesof3 是对的,您还可以使用 python pandas 包来操作 csv 文件并应用 stem 和其他 nlp 活动。

标签: python csv porter-stemmer stem


【解决方案1】:

因此,您需要执行几个步骤。

#Part 1
>>> import nltk
>>> from nltk import PorterStemmer
>>> test = 'this sentence is just a tester set of words'
>>> test_tokenize = nltk.word_tokenize(test)
>>> test_tokenize
['this', 'sentence', 'is', 'just', 'a', 'tester', 'set', 'of', 'words']
>>> port = PorterStemmer()
>>> for word in test_tokenize:
...     print port.stem(word)
... 
thi
sentenc
is
just
a
tester
set
of
word

#Part 2
with open('status.csv', 'rb') as f: 
    reader = csv.reader(f)
    for row in reader:
       #reference the column where the text is located
       #text = row[column_index_for_text]
       #then just complete the steps in part 1 to get the stemmed words

【讨论】:

    猜你喜欢
    • 2022-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-08
    • 1970-01-01
    • 2021-05-22
    • 2020-08-12
    相关资源
    最近更新 更多