Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

# Natural Language Toolkit: Corpus Readers 

# 

# Copyright (C) 2001-2012 NLTK Project 

# Author: Edward Loper <edloper@gradient.cis.upenn.edu> 

# URL: <http://www.nltk.org/> 

# For license information, see LICENSE.TXT 

 

# [xx] this docstring isnt' up-to-date! 

""" 

NLTK corpus readers.  The modules in this package provide functions 

that can be used to read corpus files in a variety of formats.  These 

functions can be used to read both the corpus files that are 

distributed in the NLTK corpus package, and corpus files that are part 

of external corpora. 

 

Available Corpora 

================= 

 

Please see http://nltk.googlecode.com/svn/trunk/nltk_data/index.xml 

for a complete list.  Install corpora using nltk.download(). 

 

Corpus Reader Functions 

======================= 

Each corpus module defines one or more "corpus reader functions", 

which can be used to read documents from that corpus.  These functions 

take an argument, ``item``, which is used to indicate which document 

should be read from the corpus: 

 

- If ``item`` is one of the unique identifiers listed in the corpus 

  module's ``items`` variable, then the corresponding document will 

  be loaded from the NLTK corpus package. 

- If ``item`` is a filename, then that file will be read. 

 

Additionally, corpus reader functions can be given lists of item 

names; in which case, they will return a concatenation of the 

corresponding documents. 

 

Corpus reader functions are named based on the type of information 

they return.  Some common examples, and their return types, are: 

 

- words(): list of str 

- sents(): list of (list of str) 

- paras(): list of (list of (list of str)) 

- tagged_words(): list of (str,str) tuple 

- tagged_sents(): list of (list of (str,str)) 

- tagged_paras(): list of (list of (list of (str,str))) 

- chunked_sents(): list of (Tree w/ (str,str) leaves) 

- parsed_sents(): list of (Tree with str leaves) 

- parsed_paras(): list of (list of (Tree with str leaves)) 

- xml(): A single xml ElementTree 

- raw(): unprocessed corpus contents 

 

For example, to read a list of the words in the Brown Corpus, use 

``nltk.corpus.brown.words()``: 

 

    >>> from nltk.corpus import brown 

    >>> brown.words() 

    ['The', 'Fulton', 'County', 'Grand', 'Jury', 'said', ...] 

 

""" 

 

import re 

 

from nltk.tokenize import RegexpTokenizer 

from nltk.tag import simplify_brown_tag, simplify_wsj_tag,\ 

                     simplify_alpino_tag, simplify_indian_tag,\ 

                     simplify_tag 

 

from .util import LazyCorpusLoader 

from .reader import * 

 

abc = LazyCorpusLoader( 

    'abc', PlaintextCorpusReader, r'(?!\.).*\.txt') 

alpino = LazyCorpusLoader( 

    'alpino', AlpinoCorpusReader, tag_mapping_function=simplify_alpino_tag) 

brown = LazyCorpusLoader( 

    'brown', CategorizedTaggedCorpusReader, r'c[a-z]\d\d', 

    cat_file='cats.txt', tag_mapping_function=simplify_brown_tag) 

cess_cat = LazyCorpusLoader( 

    'cess_cat', BracketParseCorpusReader, r'(?!\.).*\.tbf', 

    tag_mapping_function=simplify_tag) 

cess_esp = LazyCorpusLoader( 

    'cess_esp', BracketParseCorpusReader, r'(?!\.).*\.tbf', 

    tag_mapping_function=simplify_tag) 

cmudict = LazyCorpusLoader( 

    'cmudict', CMUDictCorpusReader, ['cmudict']) 

comtrans = LazyCorpusLoader( 

    'comtrans', AlignedCorpusReader, r'(?!\.).*\.txt') 

conll2000 = LazyCorpusLoader( 

    'conll2000', ConllChunkCorpusReader, 

    ['train.txt', 'test.txt'], ('NP','VP','PP'), 

    tag_mapping_function=simplify_wsj_tag) 

conll2002 = LazyCorpusLoader( 

    'conll2002', ConllChunkCorpusReader, '.*\.(test|train).*', 

    ('LOC', 'PER', 'ORG', 'MISC'), encoding='utf-8') 

conll2007 = LazyCorpusLoader( 

    'conll2007', DependencyCorpusReader, '.*\.(test|train).*', 

    encoding='utf-8') 

dependency_treebank = LazyCorpusLoader( 

    'dependency_treebank', DependencyCorpusReader, '.*\.dp') 

floresta = LazyCorpusLoader( 

    'floresta', BracketParseCorpusReader, r'(?!\.).*\.ptb', '#', 

    tag_mapping_function=simplify_tag) 

gazetteers = LazyCorpusLoader( 

    'gazetteers', WordListCorpusReader, r'(?!LICENSE|\.).*\.txt') 

genesis = LazyCorpusLoader( 

    'genesis', PlaintextCorpusReader, r'(?!\.).*\.txt', encoding=[ 

        ('finnish|french|german', 'latin_1'), 

        ('swedish', 'cp865'), 

        ('.*', 'utf_8')]) 

gutenberg = LazyCorpusLoader( 

    'gutenberg', PlaintextCorpusReader, r'(?!\.).*\.txt') 

# corpus not available with NLTK; these lines caused help(nltk.corpus) to break 

#hebrew_treebank = LazyCorpusLoader( 

#    'hebrew_treebank', BracketParseCorpusReader, r'.*\.txt') 

ieer = LazyCorpusLoader( 

    'ieer', IEERCorpusReader, r'(?!README|\.).*') 

inaugural = LazyCorpusLoader( 

    'inaugural', PlaintextCorpusReader, r'(?!\.).*\.txt') 

# [XX] This should probably just use TaggedCorpusReader: 

indian = LazyCorpusLoader( 

    'indian', IndianCorpusReader, r'(?!\.).*\.pos', 

    tag_mapping_function=simplify_indian_tag) 

ipipan = LazyCorpusLoader( 

    'ipipan', IPIPANCorpusReader, r'(?!\.).*morph\.xml') 

jeita = LazyCorpusLoader( 

    'jeita', ChasenCorpusReader, r'.*\.chasen', encoding='utf-8') 

knbc = LazyCorpusLoader( 

    'knbc/corpus1', KNBCorpusReader, r'.*/KN.*', encoding='euc-jp') 

lin_thesaurus = LazyCorpusLoader( 

    'lin_thesaurus', LinThesaurusCorpusReader, r'.*\.lsp') 

mac_morpho = LazyCorpusLoader( 

    'mac_morpho', MacMorphoCorpusReader, r'(?!\.).*\.txt', 

    tag_mapping_function=simplify_tag, encoding='latin-1') 

machado = LazyCorpusLoader( 

    'machado', PortugueseCategorizedPlaintextCorpusReader, 

    r'(?!\.).*\.txt', cat_pattern=r'([a-z]*)/.*', encoding='latin-1') 

movie_reviews = LazyCorpusLoader( 

    'movie_reviews', CategorizedPlaintextCorpusReader, 

    r'(?!\.).*\.txt', cat_pattern=r'(neg|pos)/.*') 

names = LazyCorpusLoader( 

    'names', WordListCorpusReader, r'(?!\.).*\.txt') 

nps_chat = LazyCorpusLoader( 

    'nps_chat', NPSChatCorpusReader, r'(?!README|\.).*\.xml', 

    tag_mapping_function=simplify_wsj_tag) 

pl196x = LazyCorpusLoader( 

    'pl196x', Pl196xCorpusReader, r'[a-z]-.*\.xml', 

    cat_file='cats.txt', textid_file='textids.txt') 

ppattach = LazyCorpusLoader( 

    'ppattach', PPAttachmentCorpusReader, ['training', 'test', 'devset']) 

# ptb = LazyCorpusLoader( # Penn Treebank v3: WSJ and Brown portions 

#    'ptb3', CategorizedBracketParseCorpusReader, r'(WSJ/\d\d/WSJ_\d\d|BROWN/C[A-Z]/C[A-Z])\d\d.MRG', 

#    cat_file='allcats.txt', tag_mapping_function=simplify_wsj_tag) 

qc = LazyCorpusLoader( 

    'qc', StringCategoryCorpusReader, ['train.txt', 'test.txt']) 

reuters = LazyCorpusLoader( 

    'reuters', CategorizedPlaintextCorpusReader, '(training|test).*', 

    cat_file='cats.txt') 

rte = LazyCorpusLoader( 

    'rte', RTECorpusReader, r'(?!\.).*\.xml') 

semcor = LazyCorpusLoader( 

    'semcor', XMLCorpusReader, r'brown./tagfiles/br-.*\.xml') 

senseval = LazyCorpusLoader( 

    'senseval', SensevalCorpusReader, r'(?!\.).*\.pos') 

shakespeare = LazyCorpusLoader( 

    'shakespeare', XMLCorpusReader, r'(?!\.).*\.xml') 

sinica_treebank = LazyCorpusLoader( 

    'sinica_treebank', SinicaTreebankCorpusReader, ['parsed'], 

    tag_mapping_function=simplify_tag) 

state_union = LazyCorpusLoader( 

    'state_union', PlaintextCorpusReader, r'(?!\.).*\.txt') 

stopwords = LazyCorpusLoader( 

    'stopwords', WordListCorpusReader, r'(?!README|\.).*') 

swadesh = LazyCorpusLoader( 

    'swadesh', SwadeshCorpusReader, r'(?!README|\.).*') 

switchboard = LazyCorpusLoader( 

    'switchboard', SwitchboardCorpusReader, 

    tag_mapping_function=simplify_wsj_tag) 

timit = LazyCorpusLoader( 

    'timit', TimitCorpusReader) 

timit_tagged = LazyCorpusLoader( 

    'timit', TimitTaggedCorpusReader, '.+\.tags', 

    tag_mapping_function=simplify_wsj_tag) 

toolbox = LazyCorpusLoader( 

    'toolbox', ToolboxCorpusReader, r'(?!.*(README|\.)).*\.(dic|txt)') 

treebank = LazyCorpusLoader( 

    'treebank/combined', BracketParseCorpusReader, r'wsj_.*\.mrg', 

    tag_mapping_function=simplify_wsj_tag) 

treebank_chunk = LazyCorpusLoader( 

    'treebank/tagged', ChunkedCorpusReader, r'wsj_.*\.pos', 

    sent_tokenizer=RegexpTokenizer(r'(?<=/\.)\s*(?![^\[]*\])', gaps=True), 

    para_block_reader=tagged_treebank_para_block_reader) 

treebank_raw = LazyCorpusLoader( 

    'treebank/raw', PlaintextCorpusReader, r'wsj_.*') 

udhr = LazyCorpusLoader( 

    'udhr', PlaintextCorpusReader, r'(?!README|\.).*', 

    # Encodings specified in filenames but not mapped to anything: 

    # DallakHelv, VIQR, Cyrillic+Abkh, WinResearcher, font, 

    # Afenegus6..60375, VG2Main, VPS, Turkish, TCVN, Az.Times.Lat0117, 

    # EUC, Baltic, err, Az.Times.Cyr.Normal0117, T61, Amahuaca, Agra 

    encoding=[('.*-UTF8$', 'utf-8'), ('.*-Latin1$', 'latin-1'), 

              ('.*-Hebrew$', 'hebrew'), ('.*-Arabic$', 'arabic'), 

              ('.*-Cyrillic$', 'cyrillic'), ('.*-SJIS$', 'SJIS'), 

              ('.*-GB2312$', 'GB2312'), ('.*-Latin2$', 'ISO-8859-2'), 

              ('.*-Greek$', 'greek'), ('.*-UFT8$', 'utf-8'), 

              ('Hungarian_Magyar-Unicode', 'utf-16-le')] 

    ) 

verbnet = LazyCorpusLoader( 

    'verbnet', VerbnetCorpusReader, r'(?!\.).*\.xml') 

webtext = LazyCorpusLoader( 

    'webtext', PlaintextCorpusReader, r'(?!README|\.).*\.txt') 

wordnet = LazyCorpusLoader( 

    'wordnet', WordNetCorpusReader) 

wordnet_ic = LazyCorpusLoader( 

    'wordnet_ic', WordNetICCorpusReader, '.*\.dat') 

words = LazyCorpusLoader( 

    'words', WordListCorpusReader, r'(?!README|\.).*') 

ycoe = LazyCorpusLoader( 

    'ycoe', YCOECorpusReader) 

# defined after treebank 

propbank = LazyCorpusLoader( 

    'propbank', PropbankCorpusReader, 

    'prop.txt', 'frames/.*\.xml', 'verbs.txt', 

    lambda filename: re.sub(r'^wsj/\d\d/', '', filename), 

    treebank) # Must be defined *after* treebank corpus. 

nombank = LazyCorpusLoader( 

    'nombank.1.0', NombankCorpusReader, 

    'nombank.1.0', 'frames/.*\.xml', 'nombank.1.0.words', 

    lambda filename: re.sub(r'^wsj/\d\d/', '', filename), 

    treebank) # Must be defined *after* treebank corpus. 

 

def demo(): 

    # This is out-of-date: 

    abc.demo() 

    brown.demo() 

#    chat80.demo() 

    cmudict.demo() 

    conll2000.demo() 

    conll2002.demo() 

    genesis.demo() 

    gutenberg.demo() 

    ieer.demo() 

    inaugural.demo() 

    indian.demo() 

    names.demo() 

    ppattach.demo() 

    senseval.demo() 

    shakespeare.demo() 

    sinica_treebank.demo() 

    state_union.demo() 

    stopwords.demo() 

    timit.demo() 

    toolbox.demo() 

    treebank.demo() 

    udhr.demo() 

    webtext.demo() 

    words.demo() 

#    ycoe.demo() 

 

if __name__ == '__main__': 

    #demo() 

    pass