首页 / PYTHON / 阻止过程在Python中不起作用
阻止过程在Python中不起作用
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了阻止过程在Python中不起作用,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2393字,纯文字阅读大概需要4分钟。
内容图文
![阻止过程在Python中不起作用](/upload/InfoBanner/zyjiaocheng/683/72007fb263d548079a93989f8727e833.jpg)
我有一个文本文件,在删除了停用词之后,我试图阻止它运行,但是运行该文件似乎没有任何变化.我的文件称为data0.
这是我的代码:
## Removing stopwords and tokenizing by words (split each word)
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
data0 = word_tokenize(data0)
data0 = ' '.join([word for word in data0 if word not in (stopwords.words('english'))])
print(data0)
## Stemming the data
from nltk.stem import PorterStemmer
ps = PorterStemmer()
data0 = ps.stem(data0)
print(data0)
我得到以下内容(为清晰起见而包装):
For us around Aberdeen , question “ What oil industry ? ( Evening Express , October 26 ) touch deja vu . That question asked almost since day first drop oil pumped North Sea . In past 30 years seen constant cycle ups downs , booms busts industry . I predict happen next . There period worry uncertainty scrabble find something keep local economy buoyant oil gone . Then upturn see jobs investment oil , everyone breathe sigh relief quest diversify go back burner . That downfall . Major industries prone collapse . Look nation ‘s defunct shipyards extinct coal steel industries . That ‘s vital n’t panic downturns , start planning sensibly future . Our civic business leaders need constantly looking something secure prosperity – tourism , technology , bio-science emerging industries . We need economically strong rather waiting see happens oil roller coaster hits buffers . N JonesEllon
该代码的第一部分工作正常(删除停用词并标记化),但是第二部分(Stem)无效.知道为什么吗?
解决方法:
从Stemmer docs http://www.nltk.org/howto/stem.html看来,Stemmer旨在一次被单个单词调用.
尝试对每个单词运行
[word for word in data0 if word not in (stopwords.words('english'))]
即致电加入之前
例如.
stemmed_list = []
for str in [word for word in data0 if word not in (stopwords.words('english'))]:
stemmed_list.append(ps.stem(str))
编辑:评论响应.
我运行了以下命令-它似乎可以正确阻止:
>>> from nltk.stem import PorterStemmer
>>> ps = PorterStemmer()
>>> data0 = '''<Your Data0 string>'''
>>> words = data0.split(" ")
>>> stemmed_words = map(ps.stem, words)
>>> print(list(stemmed_words)) # list cast needed because of 'map'
[..., 'industri', ..., 'diversifi']
我认为没有可以直接应用于文本的词干分析器,但是您可以将其包装在自己的接受“ ps”和文本的函数中:
def my_stem(text, stemmer):
words = text.split(" ")
stemmed_words = map(stemmer, words)
result = " ".join(list(stemmed_words))
return result
内容总结
以上是互联网集市为您收集整理的阻止过程在Python中不起作用全部内容,希望文章能够帮你解决阻止过程在Python中不起作用所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。