【问题标题】:NLTK set method prints characters, not wordsNLTK set 方法打印字符,而不是单词
【发布时间】:2013-12-27 18:03:00
【问题描述】:

我是 NLTK(和 python...)的新手,我在使用它的一种基本方法时遇到两个问题:当我调用时

sorted(set(<one of nltk's preloaded corpora>))

它打印文本中所有单词的列表,但每个单词前面都有“u”,如下所示:[u'yourselves', u'youth']。我以为我破坏了标记器,但我尝试重新克隆 repo 并重新安装。

第二个可能相关的问题是,当我在自己传入的字符串上使用这些方法定义尝试时,我得到的是单个字符,而不是单词。在使用 set() 之前是否需要解析我传入的文本?

【问题讨论】:

  • u 只是说字符串是 unicode。别担心。至于您的第二个问题,set 从传递的可迭代中创建了一个集合。如果你想做一组单词,你需要先把你的句子拆分成单词,然后再传入。
  • 太好了,非常感谢。

标签: python nlp nltk


【解决方案1】:

u'foo bar' 只是unicode 中的一个字符串。 strunicode 都被视为 basestring(参见 http://docs.python.org/2/howto/unicode.htmlhttp://docs.python.org/2/library/functions.html#basestring

>>> x = u'foobar'
>>> isinstance(x, str)
False
>>> isinstance(x,unicode)
True
>>> isinstance(x,basestring)
True
>>> print x
foobar

当您尝试从 NLTK 的语料库阅读器访问语料库时,默认数据结构是一个句子列表,其中每个句子都是一个标记列表,每个标记都是一个基本字符串。

>>> from nltk.corpus import brown
>>> print brown.sents()
[['The', 'Fulton', 'County', 'Grand', 'Jury', 'said', 'Friday', 'an', 'investigation', 'of', "Atlanta's", 'recent', 'primary', 'election', 'produced', '``', 'no', 'evidence', "''", 'that', 'any', 'irregularities', 'took', 'place', '.'], ['The', 'jury', 'further', 'said', 'in', 'term-end', 'presentments', 'that', 'the', 'City', 'Executive', 'Committee', ',', 'which', 'had', 'over-all', 'charge', 'of', 'the', 'election', ',', '``', 'deserves', 'the', 'praise', 'and', 'thanks', 'of', 'the', 'City', 'of', 'Atlanta', "''", 'for', 'the', 'manner', 'in', 'which', 'the', 'election', 'was', 'conducted', '.'], ...]

如果你想要一个纯文本版本的语料库,你可以这样做:

>>> for i in brown.sents():
...     print " ".join(i)
...     break
... 
The Fulton County Grand Jury said Friday an investigation of Atlanta's recent primary election produced `` no evidence '' that any irregularities took place .

NLTK 中有许多内部魔法可以使语料库像 NLTK 的模块一样工作,但是了解这些“预加载”语料库(或更准确地说是“预编码”语料库阅读器)中的内容的最简单方法是使用:

>>> dir(brown)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_add', '_c2f', '_delimiter', '_encoding', '_f2c', '_file', '_fileids', '_get_root', '_init', '_map', '_para_block_reader', '_pattern', '_resolve', '_root', '_sent_tokenizer', '_sep', '_tag_mapping_function', '_word_tokenizer', 'abspath', 'abspaths', 'categories', 'encoding', 'fileids', 'open', 'paras', 'raw', 'readme', 'root', 'sents', 'tagged_paras', 'tagged_sents', 'tagged_words', 'words']

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-17
    • 2016-04-07
    • 1970-01-01
    • 1970-01-01
    • 2021-06-12
    • 2016-01-20
    • 2015-09-06
    相关资源
    最近更新 更多