Python NLTK:SyntaxError:文件中的非ASCII字符'\ xc3'(情感分析-NLP) [英] Python NLTK: SyntaxError: Non-ASCII character '\xc3' in file (Sentiment Analysis -NLP)

查看:306
本文介绍了Python NLTK:SyntaxError:文件中的非ASCII字符'\ xc3'(情感分析-NLP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在和NLTK一起做情绪分析作业.我正在使用Python 2.7. NLTK 3.0和NumPy1.9.1版本.

I am playing around with NLTK to do an assignment on sentiment analysis. I am using Python 2.7. NLTK 3.0 and NumPy1.9.1 version.

这是代码:

__author__ = 'karan'
import nltk
import re
import sys



def main():
    print("Start");
    # getting the stop words
    stopWords = open("english.txt","r");
    stop_word = stopWords.read().split();
    AllStopWrd = []
    for wd in stop_word:
        AllStopWrd.append(wd);
    print("stop words-> ",AllStopWrd);

    # sample and also cleaning it
    tweet1= 'Love, my new toyí ½í¸í ½í¸#iPhone6. Its good https://twitter.com/Sandra_Ortega/status/513807261769424897/photo/1'
    print("old tweet-> ",tweet1)
    tweet1 = tweet1.lower()
    tweet1 = ' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)"," ",tweet1).split())
    print(tweet1);
    tw = tweet1.split()
    print(tw)


    #tokenize
    sentences = nltk.word_tokenize(tweet1)
    print("tokenized ->", sentences)


    #remove stop words
    Otweet =[]
    for w in tw:
        if w not in AllStopWrd:
            Otweet.append(w);
    print("sans stop word-> ",Otweet)


    # get taggers for neg/pos/inc/dec/inv words
    taggers ={}
    negWords = open("neg.txt","r");
    neg_word = negWords.read().split();
    print("ned words-> ",neg_word)
    posWords = open("pos.txt","r");
    pos_word = posWords.read().split();
    print("pos words-> ",pos_word)
    incrWords = open("incr.txt","r");
    inc_word = incrWords.read().split();
    print("incr words-> ",inc_word)
    decrWords = open("decr.txt","r");
    dec_word = decrWords.read().split();
    print("dec wrds-> ",dec_word)
    invWords = open("inverse.txt","r");
    inv_word = invWords.read().split();
    print("inverse words-> ",inv_word)
    for nw in neg_word:
        taggers.update({nw:'negative'});
    for pw in pos_word:
        taggers.update({pw:'positive'});
    for iw in inc_word:
        taggers.update({iw:'inc'});
    for dw in dec_word:
        taggers.update({dw:'dec'});
    for ivw in inv_word:
        taggers.update({ivw:'inv'});
    print("tagger-> ",taggers)
    print(taggers.get('little'))

    # get parts of speech
    posTagger = [nltk.pos_tag(tw)]
    print("posTagger-> ",posTagger)

main();

这是我在运行代码时遇到的错误:

This is the error that I am getting when running my code:

SyntaxError: Non-ASCII character '\xc3' in file C:/Users/karan/PycharmProjects/mainProject/sentiment.py on line 19, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

如何解决此错误?

我也使用Python 3.4.2和NLTK 3.0和NumPy 1.9.1尝试了代码,但随后出现错误:

I also tried the code using Python 3.4.2 and with NLTK 3.0 and NumPy 1.9.1 but then I get the error:

Traceback (most recent call last):
  File "C:/Users/karan/PycharmProjects/mainProject/sentiment.py", line 80, in <module>
    main();
  File "C:/Users/karan/PycharmProjects/mainProject/sentiment.py", line 72, in main
    posTagger = [nltk.pos_tag(tw)]
  File "C:\Python34\lib\site-packages\nltk\tag\__init__.py", line 100, in pos_tag
    tagger = load(_POS_TAGGER)
  File "C:\Python34\lib\site-packages\nltk\data.py", line 779, in load
    resource_val = pickle.load(opened_resource)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xcb in position 0: ordinal not in range(128)

推荐答案

将以下内容添加到文件的顶部# coding=utf-8

Add the following to the top of your file # coding=utf-8

如果您转到错误链接,则可以看到原因:

If you go to the link in the error you can seen the reason why:

定义编码

Python将默认使用ASCII作为标准编码 给出了编码提示. 要定义源代码编码,必须添加魔术注释 放在第一或第二个源文件中 文件中的行,例如: #coding =

Python will default to ASCII as standard encoding if no other encoding hints are given. To define a source code encoding, a magic comment must be placed into the source files either as first or second line in the file, such as: # coding=

这篇关于Python NLTK:SyntaxError:文件中的非ASCII字符'\ xc3'(情感分析-NLP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆