我编写了以下代码,它在我的计算机上运行良好,但在其他计算机上返回null。你能帮我解决这个问题吗?
import string
import nltk
from nltk.tokenize import RegexpTokenizer
from nltk.corpus import stopwords
def preprocess(sentence):
sentence = sentence.lower()
specialChrs={'\xc2',''}
pattern=pattern = r'''(?x) # set flag to allow verbose regexps
([A-Z]\.)+ # abbreviations, e.g. U.S.A.
| \$?\d+%?
| \$?\d+(,|.\d+)*
| \w+([-'/]\w+)* # words w/ optional internal hyphens/apostrophe
|/\m+([-'/]\w+)*
'''
tokenizer = RegexpTokenizer(pattern)
tokens = tokenizer.tokenize(sentence)
print tokens
realToken= [e for e in tokens if len(e)>= 3 and len(e)<10]
stopWords = set(stopwords.words('english'))
stop_words = [w for w in realToken if not w in stopWords]
filtered_words = [w for w in stop_words if not w in specialChrs]
print filtered_words
# final_words = [w for w in filtered_words if not w[0]=='0' and w[1]=='x']
return filtered_words
str='I have one generalized rule, where in shellscript I check for all need packages, if any package does not exist, then install it other wise skip to next check. As I need to check and execute few other python as well shellscripts, I am using it. Is using shellscript for this is bad idea?'
preprocess(str)这些是我电脑输出的一部分:
“我”、“有”、“一”、“广义”、“规则”、“哪里”、“在哪里”、“外壳”、“我”、“检查”、“为”、“全部”、“需要”、.“想法”
其他计算机的结果是:
(“、”)、“”、“)、”、“.
我的电脑信息
Python2.7.12Anaconda 2.3.0 (64位)(默认,2016年7月2日,17:42:40) GCC 4.4.7 20120313 (RedHat4.4.7-1)在linux2类型“帮助”、“版权”、“信贷”或“许可”中获得更多信息。Anaconda是由连续分析带给你的。请查看:http://continuum.io/thanks和https://anaconda.org 进口nltk 打印(‘nltk版本是{}.'.format(nltk.version)) nltk版本为3.2.1。
我的朋友电脑
Python2.7.12Anaconda 4.1.1 ( 64位)(64位)(缺省值,2016年6月29日,11:42:40) MSC v.1500 64位(AMD64)上的win32类型“帮助”、“版权”、“信用”或“许可”以获取更多信息。Anaconda是由连续分析带给你的。请查看:http://continuum.io/thanks和https://anaconda.org 进口nltk 打印(‘nltk版本是{}.'.format(nltk.version)) nltk版本为3.2.1。
另外,我在另一台计算机上测试我的代码,并得到相同的结果。
那台计算机的信息是:
Python2.7.3(默认,2016年10月26日,21:01:49) GCC 4.6.3在linux2类型“帮助”、“版权”、“信用”或“许可”中获得更多信息。
发布于 2017-02-08 14:47:38
in this page回答了您的问题
您需要以这种方式和顺序更改正则表达式以解决问题。
`pattern = r'''(?x) # set flag to allow verbose regexps
(?:[A-Z]\.)+ # abbreviations, e.g. U.S.A.
| \$?\d+(?:\.\d+)?%?
| \w+(?:-\w+)* # words with optional internal hyphens
|/\m+(?:[-'/]\w+)*
'''`https://stackoverflow.com/questions/42080797
复制相似问题