0

我有这个功能,我从在线 NLTK 书籍第 1 章中的材料修改。它对我非常有用,但是,尽管阅读了关于 Unicode 的章节,我还是像以前一样迷失了方向。

def openbookreturnvocab(book):
    fileopen = open(book)
    rawness = fileopen.read()
    tokens = nltk.wordpunct_tokenize(rawness)
    nltktext = nltk.Text(tokens)
    nltkwords = [w.lower() for w in nltktext]
    nltkvocab = sorted(set(nltkwords))
    return nltkvocab

前几天我在 Also Sprach Zarathustra 上尝试它时,它在 o 和 u 上用 umlat 拼写单词。我相信你们中的一些人会知道为什么会这样。我也确信它很容易修复。我知道它只是与调用一个将标记重新编码为 un​​icode 字符串的函数有关。如果是这样,在我看来它可能根本不会发生在该函数定义中,但在这里,我准备写入文件:

def jotindex(jotted, filename, readmethod):
    filemydata = open(filename, readmethod)
    jottedf = '\n'.join(jotted)
    filemydata.write(jottedf)
    filemydata.close()
    return 0

我听说我必须做的是在从文件中读取字符串后将其编码为 un​​icode。我尝试像这样修改函数:

def openbookreturnvocab(book):
    fileopen = open(book)
    rawness = fileopen.read()
    unirawness = rawness.decode('utf-8')
    tokens = nltk.wordpunct_tokenize(unirawness)
    nltktext = nltk.Text(tokens)
    nltkwords = [w.lower() for w in nltktext]
    nltkvocab = sorted(set(nltkwords))
    return nltkvocab

但这带来了这个错误,当我在匈牙利语上使用它时。当我在德语上使用它时,我没有错误。

>>> import bookroutines
>>> elles1 = bookroutines.openbookreturnvocab("lk1-les1")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "bookroutines.py", line 9, in openbookreturnvocab
    nltktext = nltk.Text(tokens)
  File "/usr/lib/pymodules/python2.6/nltk/text.py", line 285, in __init__
    self.name = " ".join(map(str, tokens[:8])) + "..."
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe1' in position 4: ordinal not in range(128)

我修复了像这样归档数据的函数:

def jotindex(jotted, filename, readmethod):
    filemydata = open(filename, readmethod)
    jottedf = u'\n'.join(jotted)
    filemydata.write(jottedf)
    filemydata.close()
    return 0

但是,当我尝试提交德语时,这带来了这个错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "bookroutines.py", line 23, in jotindex
    filemydata.write(jottedf)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf6' in position 414: ordinal not in range(128)
>>> 

...这是您尝试写入 u'\n'.join'ed 数据时得到的结果。

>>> jottedf = u'/n'.join(elles1)
>>> filemydata.write(jottedf)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf6' in position 504: ordinal not in range(128)
4

1 回答 1

4

对于从文件中读取的每个字符串rawness.decode('utf-8'),如果您有 UTF-8 格式的文本,则可以通过调用将它们转换为 unicode。你最终会得到 unicode 对象。另外,我不知道什么是“记事本”,但您可能想确保它是一个 unicode 对象并改用它u'\n'.join(jotted)

更新:

NLTK 库似乎不喜欢 unicode 对象。好的,那么您必须确保您使用的是带有 UTF-8 编码文本的 str 实例。尝试使用这个:

tokens = nltk.wordpunct_tokenize(unirawness)
nltktext = nltk.Text([token.encode('utf-8') for token in tokens])

还有这个:

jottedf = u'\n'.join(jotted)
filemydata.write(jottedf.encode('utf-8'))

但如果 joted 真的是一个 UTF-8 编码的 str 列表,那么你不需要这个,这应该足够了:

jottedf = '\n'.join(jotted)
filemydata.write(jottedf)

顺便说一句,看起来 NLTK 对 unicode 和编码(至少,演示)不是很谨慎。最好小心并检查它是否正确处理了您的令牌。此外,这可能导致您收到匈牙利文本而不是德语文本的错误,请检查您的编码

于 2010-09-22T10:49:04.480 回答