gpt4 book ai didi

python - NLTK 词干分析器有时会在词干词中包含标点符号

转载 作者:行者123 更新时间:2023-11-30 22:33:40 26 4
gpt4 key购买 nike

更新:

尽管进行了严格的清理,一些带有句点的单词仍然被标记为完整的句点,包括在句点和引号之间填充空格的字符串。我在 Jupyter Notebook 中创建了一个公共(public)链接,其中包含问题的示例:https://drive.google.com/file/d/0B90qb2J7ZLYrZmItME5RRlhsVWM/view?usp=sharing

或更短的示例:

word_tokenize('This is a test. "')
['This', 'is', 'a', 'test.', '``']

但是当使用其他类型的双引号时消失:

word_tokenize('This is a test. ”')
['This', 'is', 'a', 'test', '.', '”']

原文:

我正在提取大量文本,并创建了一个计数器来查看每个单词的计数,并将该计数器转移到数据帧中以便于处理。每段文本都是一个 100-5000 个单词的大字符串。包含字数的数据框如下所示,以计数为 11 的单词为例:

allwordsdf[(allwordsdf['count'] == 11)]


words count
551 throughlin 11
1921 rampd 11
1956 pinhol 11
2476 reckhow 11

我注意到有很多单词没有完全词干,并且它们的末尾附有句点。例如:

4233    activist.   11
9243 storyline. 11

我不确定这是什么原因造成的。我知道它通常是单独提取句号,因为句号行位于:

23  .   5702880

此外,它似乎并没有对“激进主义者”的每个实例都这样做。:

len(articles[articles['content'].str.contains('activist.')])
9600

不确定我是否忽略了某些东西——昨天我遇到了 problem with the NLTK stemmer that was a bug ,而且我不知道这是否是我正在做的事情(总是更有可能)。

感谢您的指导。

编辑:

这是我正在使用的函数:

progress = 0
start = time.time()

def stem(x):
end = time.time()
tokens = word_tokenize(x)
global start
global progress
progress += 1
sys.stdout.write('\r {} percent, {} position, {} per second '.format(str(float(progress / len(articles))),
str(progress), (1 / (end - start))))

stems = [stemmer.stem(e) for e in tokens]
start = time.time()
return stems


articles['stems'] = articles.content.apply(lambda x: stem(x))

编辑2:

Here is a JSON某些数据:所有字符串、标记和词干。

这是当我查找所有单词时得到的片段,在标记化和词干之后,仍然有句点:

allwordsdf[allwordsdf['words'].str.contains('\.')] #dataframe made from the counter dict

words count
23 . 5702875
63 years. 1231
497 was. 281
798 lost. 157
817 jie. 1
819 teacher.24
858 domains.1
875 fallout.3
884 net. 23
889 option. 89
895 step. 67
927 pool. 30
936 that. 4245
954 compute.2
1001 dr. 11007
1010 decisions. 159

该切片的长度约为 49,000。

编辑3:

阿尔瓦斯的回答帮助将带句点的单词数量减少了大约一半,达到 24,000 个唯一单词,总计数为 518980,这是一个很大的数字。正如我发现的那样,问题在于每次出现句点和引号时它都会执行此操作。例如,以字符串“sickened.”为例,它在标记化单词中出现一次。

如果我搜索语料库:

articles[articles['content'].str.contains(r'sickened\.[^\s]')]

整个语料库中它唯一出现的地方是这里:

...说他“恶心”。特朗普的竞选搭档...

这不是一个孤立的事件,而是我在搜索这些术语时反复看到的情况。它们每次后面都有一个引号。分词器不仅不能处理带有字符-句点-引号-字符的单词,还不能处理带有字符-句点-引号-空格的单词。

最佳答案

您需要在词干提取之前对字符串进行标记:

>>> from nltk.stem import PorterStemmer
>>> from nltk import word_tokenize
>>> text = 'This is a foo bar sentence, that contains punctuations.'
>>> porter = PorterStemmer()
>>> [porter.stem(word) for word in text.split()]
[u'thi', 'is', 'a', 'foo', 'bar', 'sentence,', 'that', u'contain', 'punctuations.']
>>> [porter.stem(word) for word in word_tokenize(text)]
[u'thi', 'is', 'a', 'foo', 'bar', u'sentenc', ',', 'that', u'contain', u'punctuat', '.']
<小时/>

在数据框中:

porter = PorterStemmer()
articles['tokens'] = articles['content'].apply(word_tokenize)
articles['stem'] = articles['tokens'].apply(lambda x: [porter.stem(word) for word in x])
<小时/>
>>> import pandas as pd
>>> from nltk.stem import PorterStemmer
>>> from nltk import word_tokenize
>>> sents = ['This is a foo bar, sentence.', 'Yet another, foo bar!']
>>> df = pd.DataFrame(sents, columns=['content'])
>>> df
content
0 This is a foo bar, sentence.
1 Yet another, foo bar!

# Apply tokenizer.
>>> df['tokens'] = df['content'].apply(word_tokenize)
>>> df
content tokens
0 This is a foo bar, sentence. [This, is, a, foo, bar, ,, sentence, .]
1 Yet another, foo bar! [Yet, another, ,, foo, bar, !]

# Without DataFrame.apply
>>> df['tokens'][0]
['This', 'is', 'a', 'foo', 'bar', ',', 'sentence', '.']
>>> [porter.stem(word) for word in df['tokens'][0]]
[u'thi', 'is', 'a', 'foo', 'bar', ',', u'sentenc', '.']

# With DataFrame.apply
>>> df['tokens'].apply(lambda row: [porter.stem(word) for word in row])
0 [thi, is, a, foo, bar, ,, sentenc, .]
1 [yet, anoth, ,, foo, bar, !]

# Or if you like nested lambdas.
>>> df['tokens'].apply(lambda x: map(lambda y: porter.stem(y), x))
0 [thi, is, a, foo, bar, ,, sentenc, .]
1 [yet, anoth, ,, foo, bar, !]

关于python - NLTK 词干分析器有时会在词干词中包含标点符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45091297/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com