我正在对一组来自web的字符串库执行NLP任务,正如您所期望的那样,存在编码问题。以下是几个例子:they don’t serve sushi : the apostrophe in don't is not standard ' but \xe2\x80\x99
Delicious food – Wow : the hyphen before wow is \xe2\x80\x93
现在,我将阅读这些行,将它们传递给NLTK进行解析,使用解析信息通过mallet训练CRF模型。在
让我们从堆栈溢出的解决方案开始。这是一些前男友边缘:-在
^{pr2}$
因此,这些只是尝试和错误的尝试,看看是否有一些东西可以工作。在
最后我使用了编码的句子,并将其传递给下一个部分-使用nltk进行词性标记。posTags = nltk.pos_tag(tokens)它抛出了一个大家都知道的丑陋异常:File "C:\Users\user\workspacePy\_projectname_\CRF\FeatureGen.py", line 95, in getSentenceFeatures
posTags = nltk.pos_tag(tokens)
File "C:\Users\user\Anaconda\lib\site-packages\nltk\tag\__init__.py", line 101, in pos_tag
return tagger.tag(tokens)
File "C:\Users\user\Anaconda\lib\site-packages\nltk\tag\sequential.py", line 61, in tag
tags.append(self.tag_one(tokens, i, tags))
File "C:\Users\user\Anaconda\lib\site-packages\nltk\tag\sequential.py", line 81, in tag_one
tag = tagger.choose_tag(tokens, index, history)
File "C:\Users\user\Anaconda\lib\site-packages\nltk\tag\sequential.py", line 634, in choose_tag
featureset = self.feature_detector(tokens, index, history)
File "C:\Users\user\Anaconda\lib\site-packages\nltk\tag\sequential.py", line 736, in feature_detector
'prevtag+word': '%s+%s' % (prevtag, word.lower()),
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 0: ordinal not in range(128)
当我尝试解码时,它在我解码字符串的那一行写着UnicodeEncodeError: 'ascii' codec can't encode character u'\u2013' in position 42: ordinal not in range(128)。在
所以我目前的解决方案是删除所有非ascii字符。但它完全改变了基于单字双元组(unigram-bigram,word-combination)的数据丢失模型。在
正确的方法是什么?在