python解决编码问题_如何解决这个奇怪的python编码问题?

我正在对一组来自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)的数据丢失模型。在

正确的方法是什么?在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值