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

查看:18
本文介绍了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 	])|(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

我该如何解决这个错误?

How do I fix this error?

我还使用 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:Python34libsite-packages
ltk	ag\__init__.py", line 100, in pos_tag
    tagger = load(_POS_TAGGER)
  File "C:Python34libsite-packages
ltkdata.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 NLTK:SyntaxError:文件中的非 ASCII 字符“xc3"(情绪分析 -NLP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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