0

我正在尝试删除数据中的停用词。所以它会从这个开始

data['text'].head(5)
Out[25]: 
0    go until jurong point, crazy.. available only ...
1                        ok lar... joking wif u oni...
2    free entry in 2 a wkly comp to win fa cup fina...
3    u dun say so early hor... u c already then say...
4    nah i don't think he goes to usf, he lives aro...
Name: text, dtype: object

对此

data['newt'].head(5)
Out[26]: 
0    [go, jurong, point,, crazy.., available, bugis...
1                 [ok, lar..., joking, wif, u, oni...]
2    [free, entry, 2, wkly, comp, win, fa, cup, fin...
3    [u, dun, say, early, hor..., u, c, already, sa...
4      [nah, think, goes, usf,, lives, around, though]
Name: newt, dtype: object

关于如何做到这一点,我有两种选择。我正在分别尝试这两个选项,所以它不会覆盖任何东西。首先,我将一个函数应用于数据列。这行得通,它消除了我想做的事情。

def process(data):
    data = data.lower()
    data = data.split()
    data = [row for row in data if row not in stopwords]
    return data

data['newt'] = data['text'].apply(process)

第二个选项不使用应用函数参数。它和函数一模一样,但为什么会返回TypeError: unhashable type: 'list'?我检查if row not in stopwords该行中的原因是什么,因为当我删除它时,它会运行但它不会删除停用词

data['newt'] = data['text'].str.lower()
data['newt'] = data['newt'].str.split()
data['newt'] = [row for row in data['newt'] if row not in stopwords]
4

1 回答 1

1

您的列表理解失败,因为它检查您的整个数据框行是否在停用词列表中。这绝不是真的,所以[row for row in data['newt'] if row not in stopwords]产生的只是原始data['newt']列中的值列表。

我认为按照您的逻辑,您删除停用词的最后几行可能是

data['newt'] = data['text'].str.lower()
data['newt'] = data['newt'].str.split()
data['newt'] = [[word for word in row if word not in stopwords] for row in data['newt']]

如果您可以使用apply,最后一行可以替换为

data['newt'] = data['newt'].apply(lambda row: [word for word in row if word not in stopwords])

最后,你也可以打电话

data['newt'].apply(lambda row: " ".join(row))

在流程结束时取回字符串。

请注意,这str.split可能不是进行标记化的最佳方式,您可以选择使用专用库的解决方案,例如spacy使用spacy 删除停用词和使用 spacy添加/删除自定义停用词的组合

要说服自己相信上述论点,请尝试以下代码:

import spacy

sent = "She said: 'beware, your sentences may contain a lot of funny chars!'"

# spacy tokenization
spacy.cli.download("en_core_web_sm")
nlp = spacy.load('en_core_web_sm')
doc = nlp(sent)
print([token.text for token in doc])

# simple split
print(sent.split())

并比较两个输出。

于 2020-05-20T11:12:24.973 回答