2011-01-22 49 views
1

NLTK带有一些语料库样本: http://nltk.googlecode.com/svn/trunk/nltk_data/index.xml如何从NLTK附带的样本语料库中提取单词?

我希望只有没有编码的文本。我不知道如何提取这些内容。我想提取的是:

1)nps_chat:文件名与解压缩后的文件名相同,如10-19-20s_706posts.xml。 这样的文件是XML格式,如:

<Posts> 
<Post class="Statement" user="10-19-20sUser7">now im left with this gay name<terminals> 

       <t pos="RB" word="now"/> 
       <t pos="PRP" word="im"/> 
       <t pos="VBD" word="left"/> 
       <t pos="IN" word="with"/> 
       <t pos="DT" word="this"/> 
       <t pos="JJ" word="gay"/> 
       <t pos="NN" word="name"/> 
      </terminals> 

     </Post> 
      ... 
      ... 

我只想要实际岗位:

now im left with this gay name 

如何在NLTK或做(什么)保存在本地磁盘剥离编码后裸露的帖子吗?

2)总机转录本。这种类型的文件(解压缩后的文件名是话语)包含以下格式。我要的是剥夺前面标记:

o A.1 utt1: Okay,/
qy A.1 utt2: have you ever served as a juror?/
ng B.2 utt1: Never./
sd^e B.2 utt2: I've never been served on the jury, never been called up in a jury, although some of my friends have been jurors./
b A.3 utt1: Uh-huh./
sd A.3 utt2: I never have either./
% B.4 utt1: You haven't, {F huh. }/
... 
... 

我只想有:

Okay,/
have you ever served as a juror?/
Never./
I've never been served on the jury, never been called up in a jury, although some of my friends have been jurors./
Uh-huh./
I never have either./
You haven't, {F huh. }/
... 
... 

非常感谢你提前。

回答

2

首先,您需要为语料库制作corpus reader。有一些读者文集,你可以在nltk.corpus使用,如:

AlpinoCorpusReader 
BNCCorpusReader 
BracketParseCorpusReader 
CMUDictCorpusReader 
CategorizedCorpusReader 
CategorizedPlaintextCorpusReader 
CategorizedTaggedCorpusReader 
ChunkedCorpusReader 
ConllChunkCorpusReader 
ConllCorpusReader 
CorpusReader 
DependencyCorpusReader 
EuroparlCorpusReader 
IEERCorpusReader 
IPIPANCorpusReader 
IndianCorpusReader 
MacMorphoCorpusReader 
NPSChatCorpusReader 
NombankCorpusReader 
PPAttachmentCorpusReader 
Pl196xCorpusReader 
PlaintextCorpusReader 
PortugueseCategorizedPlaintextCorpusReader 
PropbankCorpusReader 
RTECorpusReader 
SensevalCorpusReader 
SinicaTreebankCorpusReader 
StringCategoryCorpusReader 
SwadeshCorpusReader 
SwitchboardCorpusReader 
SyntaxCorpusReader 
TaggedCorpusReader 
TimitCorpusReader 
ToolboxCorpusReader 
VerbnetCorpusReader 
WordListCorpusReader 
WordNetCorpusReader 
WordNetICCorpusReader 
XMLCorpusReader 
YCOECorpusReader 

一旦你做了一个文集的读者你的文集,像这样:

c = nltk.corpus.whateverCorpusReaderYouChoose(directoryWithCorpus, regexForFileTypes) 

你可以出来的话通过使用以下代码:

paragraphs = [para for para in c.paras()] 
for para in paragraphs: 
    words = [word for sentence in para for word in sentence] 

这应该为您提供您的语料库所有段落中所有单词的列表。

希望这有助于

+0

谢谢inspectorG4dget所有的话,我正在测试你的代码。 – Dylan 2011-01-22 10:52:21

1

可以使用.words()财产NLTK语料库

content = nps_chat.words()

这会给你一个列表

['now', 'im', 'left', 'with', 'this', 'gay', 'name', ...]

相关问题