149

NLTK を使用して、各行がドキュメントと見なされるテキスト ファイルに対して kmeans クラスタリングを実行しています。たとえば、私のテキスト ファイルは次のようなものです。

belong finger death punch <br>
hasty <br>
mike hasty walls jericho <br>
jägermeister rules <br>
rules bands follow performing jägermeister stage <br>
approach 

今、実行しようとしているデモ コードは次のとおりです。

import sys

import numpy
from nltk.cluster import KMeansClusterer, GAAClusterer, euclidean_distance
import nltk.corpus
from nltk import decorators
import nltk.stem

stemmer_func = nltk.stem.EnglishStemmer().stem
stopwords = set(nltk.corpus.stopwords.words('english'))

@decorators.memoize
def normalize_word(word):
    return stemmer_func(word.lower())

def get_words(titles):
    words = set()
    for title in job_titles:
        for word in title.split():
            words.add(normalize_word(word))
    return list(words)

@decorators.memoize
def vectorspaced(title):
    title_components = [normalize_word(word) for word in title.split()]
    return numpy.array([
        word in title_components and not word in stopwords
        for word in words], numpy.short)

if __name__ == '__main__':

    filename = 'example.txt'
    if len(sys.argv) == 2:
        filename = sys.argv[1]

    with open(filename) as title_file:

        job_titles = [line.strip() for line in title_file.readlines()]

        words = get_words(job_titles)

        # cluster = KMeansClusterer(5, euclidean_distance)
        cluster = GAAClusterer(5)
        cluster.cluster([vectorspaced(title) for title in job_titles if title])

        # NOTE: This is inefficient, cluster.classify should really just be
        # called when you are classifying previously unseen examples!
        classified_examples = [
                cluster.classify(vectorspaced(title)) for title in job_titles
            ]

        for cluster_id, title in sorted(zip(classified_examples, job_titles)):
            print cluster_id, title

(こちらからもご覧いただけます

私が受け取るエラーはこれです:

Traceback (most recent call last):
File "cluster_example.py", line 40, in
words = get_words(job_titles)
File "cluster_example.py", line 20, in get_words
words.add(normalize_word(word))
File "", line 1, in
File "/usr/local/lib/python2.7/dist-packages/nltk/decorators.py", line 183, in memoize
result = func(*args)
File "cluster_example.py", line 14, in normalize_word
return stemmer_func(word.lower())
File "/usr/local/lib/python2.7/dist-packages/nltk/stem/snowball.py", line 694, in stem
word = (word.replace(u"\u2019", u"\x27")
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 13: ordinal not in range(128)

ここで何が起きてるの?

4

11 に答える 11

16

Ubuntu 18.04 でPython3.6を使用している場合、両方を実行して問題を解決しました。

with open(filename, encoding="utf-8") as lines:

ツールをコマンドラインとして実行している場合:

export LC_ALL=C.UTF-8

Python2.7を使用している場合は、これを別の方法で処理する必要があることに注意してください。まず、デフォルトのエンコーディングを設定する必要があります:

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

io.open次に、エンコーディングの設定に使用する必要があるファイルをロードします。

import io
with io.open(filename, 'r', encoding='utf-8') as lines:

あなたはまだenvをエクスポートする必要があります

export LC_ALL=C.UTF-8
于 2019-05-13T09:03:14.467 に答える
5

関連するすべてのユニコードエラーを見つけるには...次のコマンドを使用します。

grep -r -P '[^\x00-\x7f]' /etc/apache2 /etc/letsencrypt /etc/nginx

で鉱山を見つけました

/etc/letsencrypt/options-ssl-nginx.conf:        # The following CSP directives don't use default-src as 

を使用しshedて、問題のあるシーケンスを見つけました。編集者のミスであることが判明しました。

00008099:     C2  194 302 11000010
00008100:     A0  160 240 10100000
00008101:  d  64  100 144 01100100
00008102:  e  65  101 145 01100101
00008103:  f  66  102 146 01100110
00008104:  a  61  097 141 01100001
00008105:  u  75  117 165 01110101
00008106:  l  6C  108 154 01101100
00008107:  t  74  116 164 01110100
00008108:  -  2D  045 055 00101101
00008109:  s  73  115 163 01110011
00008110:  r  72  114 162 01110010
00008111:  c  63  099 143 01100011
00008112:     C2  194 302 11000010
00008113:     A0  160 240 10100000
于 2018-08-26T13:06:41.460 に答える