u'foo bar'
只是unicode
中的一個字符串。既str
和unicode
被視爲basestring
(見http://docs.python.org/2/howto/unicode.html,http://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的模塊,但知道什麼是在這些「預加載」語料庫中的一個(或者更準確地「預編碼」語料庫讀者)最簡單的方法是使用:
的'u'只是說,該字符串是unicode。不要擔心。至於你的第二個問題,'set'使傳出的迭代器集合成爲一個集合。如果你想製作一組單詞,你需要在傳遞它之前將其分成單詞。 – Blender
太好了,謝謝你。 – TuringTested