【问题标题】:Kaggle word2vec competition, part 2Kaggle word2vec 竞赛,第 2 部分
【发布时间】:2015-01-17 17:40:19
【问题描述】:

我的代码是FROM:https://www.kaggle.com/c/word2vec-nlp-tutorial/details/part-2-word-vectors,我读取数据成功, 这里是用 BeautifulSoup 和 nltk 来清理文本,去除非字母而是数字。

def review_to_wordlist( review, remove_stopwords=False ):
    # Function to convert a document to a sequence of words,
    # optionally removing stop words.  Returns a list of words.
    #
    # 1. Remove HTML
    review_text = BeautifulSoup(review).get_text()
    #  
    # 2. Remove non-letters
    review_text = re.sub("[^a-zA-Z]"," ", review_text)
    #
    # 3. Convert words to lower case and split them
    words = review_text.lower().split()
    #
    # 4. Return a list of words
    return(words) 

但是当我继续到这里时,无法继续。

sentences = []  # Initialize an empty list of sentences

print "Parsing sentences from training set"
for review in train["review"]:
    sentences += review_to_sentences(review, tokenizer)

**error: what is meaning?? the before code runs well, i have tried it 3 times, when the code runs here, appear these problems.**
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "<stdin>", line 6, in review_to_sentences
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/nltk/tokenize/punkt.py", line 1270, in tokenize
    return list(self.sentences_from_text(text, realign_boundaries))
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/nltk/tokenize/punkt.py", line 1318, in sentences_from_text
    return [text[s:e] for s, e in self.span_tokenize(text, realign_boundaries)]
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/nltk/tokenize/punkt.py", line 1309, in span_tokenize
    return [(sl.start, sl.stop) for sl in slices]
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/nltk/tokenize/punkt.py", line 1348, in _realign_boundaries
    for sl1, sl2 in _pair_iter(slices):
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/nltk/tokenize/punkt.py", line 355, in _pair_iter
    for el in it:
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/nltk/tokenize/punkt.py", line 1324, in _slices_from_text
    if self.text_contains_sentbreak(context):
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/nltk/tokenize/punkt.py", line 1369, in text_contains_sentbreak
    for t in self._annotate_tokens(self._tokenize_words(text)):
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/nltk/tokenize/punkt.py", line 1504, in _annotate_second_pass
    for t1, t2 in _pair_iter(tokens):
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/nltk/tokenize/punkt.py", line 354, in _pair_iter
    prev = next(it)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/nltk/tokenize/punkt.py", line 621, in _annotate_first_pass
    for aug_tok in tokens:
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/nltk/tokenize/punkt.py", line 586, in _tokenize_words
    for line in plaintext.split('\n'):
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 15: ordinal not in range(128)
>>> 

【问题讨论】:

  • 当我打印 len(sentences) 时只有 4460 通常应该是 857234。

标签: python-2.7 beautifulsoup nltk word2vec kaggle


【解决方案1】:

这是 UnicodeDecodeError,当您的数据不是正确的编码类型时(它应该是 'unicode' 而不是 'str')。对此进行更改可能会有所帮助:

`sentences += review_to_sentences(review.decode("utf8"), tokenizer)`

但这可能需要一些时间。另一种方法是在读取输入数据时在开头指定编码'utf8':

`pd.read_csv("input_file", encoding="utf-8")`

【讨论】:

  • 感谢您的回答,这肯定是 Unicode 问题。 :)
猜你喜欢
  • 2018-05-24
  • 2012-07-12
  • 2018-04-15
  • 1970-01-01
  • 1970-01-01
  • 2014-06-06
  • 1970-01-01
  • 2022-11-02
  • 2020-07-09
相关资源
最近更新 更多