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

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

# Natural Language Toolkit: Tagged Corpus Reader 

# 

# Copyright (C) 2001-2012 NLTK Project 

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

#         Steven Bird <sb@ldc.upenn.edu> 

#         Jacob Perkins <japerk@gmail.com> 

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

# For license information, see LICENSE.TXT 

 

""" 

A reader for corpora whose documents contain part-of-speech-tagged words. 

""" 

 

import os 

 

from nltk import compat 

from nltk.tag import str2tuple 

from nltk.tokenize import * 

 

from .api import * 

from .util import * 

from .timit import read_timit_block 

 

class TaggedCorpusReader(CorpusReader): 

    """ 

    Reader for simple part-of-speech tagged corpora.  Paragraphs are 

    assumed to be split using blank lines.  Sentences and words can be 

    tokenized using the default tokenizers, or by custom tokenizers 

    specified as parameters to the constructor.  Words are parsed 

    using ``nltk.tag.str2tuple``.  By default, ``'/'`` is used as the 

    separator.  I.e., words should have the form:: 

 

       word1/tag1 word2/tag2 word3/tag3 ... 

 

    But custom separators may be specified as parameters to the 

    constructor.  Part of speech tags are case-normalized to upper 

    case. 

    """ 

    def __init__(self, root, fileids, 

                 sep='/', word_tokenizer=WhitespaceTokenizer(), 

                 sent_tokenizer=RegexpTokenizer('\n', gaps=True), 

                 para_block_reader=read_blankline_block, 

                 encoding=None, 

                 tag_mapping_function=None): 

        """ 

        Construct a new Tagged Corpus reader for a set of documents 

        located at the given root directory.  Example usage: 

 

            >>> root = '/...path to corpus.../' 

            >>> reader = TaggedCorpusReader(root, '.*', '.txt') 

 

        :param root: The root directory for this corpus. 

        :param fileids: A list or regexp specifying the fileids in this corpus. 

        """ 

        CorpusReader.__init__(self, root, fileids, encoding) 

        self._sep = sep 

        self._word_tokenizer = word_tokenizer 

        self._sent_tokenizer = sent_tokenizer 

        self._para_block_reader = para_block_reader 

        self._tag_mapping_function = tag_mapping_function 

 

    def raw(self, fileids=None): 

        """ 

        :return: the given file(s) as a single string. 

        :rtype: str 

        """ 

        if fileids is None: fileids = self._fileids 

        elif isinstance(fileids, compat.string_types): fileids = [fileids] 

        return concat([self.open(f).read() for f in fileids]) 

 

    def words(self, fileids=None): 

        """ 

        :return: the given file(s) as a list of words 

            and punctuation symbols. 

        :rtype: list(str) 

        """ 

        return concat([TaggedCorpusView(fileid, enc, 

                                        False, False, False, 

                                        self._sep, self._word_tokenizer, 

                                        self._sent_tokenizer, 

                                        self._para_block_reader, 

                                        None) 

                       for (fileid, enc) in self.abspaths(fileids, True)]) 

 

    def sents(self, fileids=None): 

        """ 

        :return: the given file(s) as a list of 

            sentences or utterances, each encoded as a list of word 

            strings. 

        :rtype: list(list(str)) 

        """ 

        return concat([TaggedCorpusView(fileid, enc, 

                                        False, True, False, 

                                        self._sep, self._word_tokenizer, 

                                        self._sent_tokenizer, 

                                        self._para_block_reader, 

                                        None) 

                       for (fileid, enc) in self.abspaths(fileids, True)]) 

 

    def paras(self, fileids=None): 

        """ 

        :return: the given file(s) as a list of 

            paragraphs, each encoded as a list of sentences, which are 

            in turn encoded as lists of word strings. 

        :rtype: list(list(list(str))) 

        """ 

        return concat([TaggedCorpusView(fileid, enc, 

                                        False, True, True, 

                                        self._sep, self._word_tokenizer, 

                                        self._sent_tokenizer, 

                                        self._para_block_reader, 

                                        None) 

                       for (fileid, enc) in self.abspaths(fileids, True)]) 

 

    def tagged_words(self, fileids=None, simplify_tags=False): 

        """ 

        :return: the given file(s) as a list of tagged 

            words and punctuation symbols, encoded as tuples 

            ``(word,tag)``. 

        :rtype: list(tuple(str,str)) 

        """ 

        if simplify_tags: 

            tag_mapping_function = self._tag_mapping_function 

        else: 

            tag_mapping_function = None 

        return concat([TaggedCorpusView(fileid, enc, 

                                        True, False, False, 

                                        self._sep, self._word_tokenizer, 

                                        self._sent_tokenizer, 

                                        self._para_block_reader, 

                                        tag_mapping_function) 

                       for (fileid, enc) in self.abspaths(fileids, True)]) 

 

    def tagged_sents(self, fileids=None, simplify_tags=False): 

        """ 

        :return: the given file(s) as a list of 

            sentences, each encoded as a list of ``(word,tag)`` tuples. 

 

        :rtype: list(list(tuple(str,str))) 

        """ 

        if simplify_tags: 

            tag_mapping_function = self._tag_mapping_function 

        else: 

            tag_mapping_function = None 

        return concat([TaggedCorpusView(fileid, enc, 

                                        True, True, False, 

                                        self._sep, self._word_tokenizer, 

                                        self._sent_tokenizer, 

                                        self._para_block_reader, 

                                        tag_mapping_function) 

                       for (fileid, enc) in self.abspaths(fileids, True)]) 

 

    def tagged_paras(self, fileids=None, simplify_tags=False): 

        """ 

        :return: the given file(s) as a list of 

            paragraphs, each encoded as a list of sentences, which are 

            in turn encoded as lists of ``(word,tag)`` tuples. 

        :rtype: list(list(list(tuple(str,str)))) 

        """ 

        if simplify_tags: 

            tag_mapping_function = self._tag_mapping_function 

        else: 

            tag_mapping_function = None 

        return concat([TaggedCorpusView(fileid, enc, 

                                        True, True, True, 

                                        self._sep, self._word_tokenizer, 

                                        self._sent_tokenizer, 

                                        self._para_block_reader, 

                                        tag_mapping_function) 

                       for (fileid, enc) in self.abspaths(fileids, True)]) 

 

class CategorizedTaggedCorpusReader(CategorizedCorpusReader, 

                                    TaggedCorpusReader): 

    """ 

    A reader for part-of-speech tagged corpora whose documents are 

    divided into categories based on their file identifiers. 

    """ 

    def __init__(self, *args, **kwargs): 

        """ 

        Initialize the corpus reader.  Categorization arguments 

        (``cat_pattern``, ``cat_map``, and ``cat_file``) are passed to 

        the ``CategorizedCorpusReader`` constructor.  The remaining arguments 

        are passed to the ``TaggedCorpusReader``. 

        """ 

        CategorizedCorpusReader.__init__(self, kwargs) 

        TaggedCorpusReader.__init__(self, *args, **kwargs) 

 

    def _resolve(self, fileids, categories): 

        if fileids is not None and categories is not None: 

            raise ValueError('Specify fileids or categories, not both') 

        if categories is not None: 

            return self.fileids(categories) 

        else: 

            return fileids 

    def raw(self, fileids=None, categories=None): 

        return TaggedCorpusReader.raw( 

            self, self._resolve(fileids, categories)) 

    def words(self, fileids=None, categories=None): 

        return TaggedCorpusReader.words( 

            self, self._resolve(fileids, categories)) 

    def sents(self, fileids=None, categories=None): 

        return TaggedCorpusReader.sents( 

            self, self._resolve(fileids, categories)) 

    def paras(self, fileids=None, categories=None): 

        return TaggedCorpusReader.paras( 

            self, self._resolve(fileids, categories)) 

    def tagged_words(self, fileids=None, categories=None, simplify_tags=False): 

        return TaggedCorpusReader.tagged_words( 

            self, self._resolve(fileids, categories), simplify_tags) 

    def tagged_sents(self, fileids=None, categories=None, simplify_tags=False): 

        return TaggedCorpusReader.tagged_sents( 

            self, self._resolve(fileids, categories), simplify_tags) 

    def tagged_paras(self, fileids=None, categories=None, simplify_tags=False): 

        return TaggedCorpusReader.tagged_paras( 

            self, self._resolve(fileids, categories), simplify_tags) 

 

class TaggedCorpusView(StreamBackedCorpusView): 

    """ 

    A specialized corpus view for tagged documents.  It can be 

    customized via flags to divide the tagged corpus documents up by 

    sentence or paragraph, and to include or omit part of speech tags. 

    ``TaggedCorpusView`` objects are typically created by 

    ``TaggedCorpusReader`` (not directly by nltk users). 

    """ 

    def __init__(self, corpus_file, encoding, tagged, group_by_sent, 

                 group_by_para, sep, word_tokenizer, sent_tokenizer, 

                 para_block_reader, tag_mapping_function=None): 

        self._tagged = tagged 

        self._group_by_sent = group_by_sent 

        self._group_by_para = group_by_para 

        self._sep = sep 

        self._word_tokenizer = word_tokenizer 

        self._sent_tokenizer = sent_tokenizer 

        self._para_block_reader = para_block_reader 

        self._tag_mapping_function = tag_mapping_function 

        StreamBackedCorpusView.__init__(self, corpus_file, encoding=encoding) 

 

    def read_block(self, stream): 

        """Reads one paragraph at a time.""" 

        block = [] 

        for para_str in self._para_block_reader(stream): 

            para = [] 

            for sent_str in self._sent_tokenizer.tokenize(para_str): 

                sent = [str2tuple(s, self._sep) for s in 

                        self._word_tokenizer.tokenize(sent_str)] 

                if self._tag_mapping_function: 

                    sent = [(w, self._tag_mapping_function(t)) for (w,t) in sent] 

                if not self._tagged: 

                    sent = [w for (w,t) in sent] 

                if self._group_by_sent: 

                    para.append(sent) 

                else: 

                    para.extend(sent) 

            if self._group_by_para: 

                block.append(para) 

            else: 

                block.extend(para) 

        return block 

 

# needs to implement simplified tags 

class MacMorphoCorpusReader(TaggedCorpusReader): 

    """ 

    A corpus reader for the MAC_MORPHO corpus.  Each line contains a 

    single tagged word, using '_' as a separator.  Sentence boundaries 

    are based on the end-sentence tag ('_.').  Paragraph information 

    is not included in the corpus, so each paragraph returned by 

    ``self.paras()`` and ``self.tagged_paras()`` contains a single 

    sentence. 

    """ 

    def __init__(self, root, fileids, encoding=None, tag_mapping_function=None): 

        TaggedCorpusReader.__init__( 

            self, root, fileids, sep='_', 

            word_tokenizer=LineTokenizer(), 

            sent_tokenizer=RegexpTokenizer('.*\n'), 

            para_block_reader=self._read_block, 

            encoding=encoding, 

            tag_mapping_function=tag_mapping_function) 

 

    def _read_block(self, stream): 

        return read_regexp_block(stream, r'.*', r'.*_\.') 

 

class TimitTaggedCorpusReader(TaggedCorpusReader): 

    """ 

    A corpus reader for tagged sentences that are included in the TIMIT corpus. 

    """ 

    def __init__(self, *args, **kwargs): 

        TaggedCorpusReader.__init__( 

            self, para_block_reader=read_timit_block, *args, **kwargs) 

 

    def paras(self): 

        raise NotImplementedError('use sents() instead') 

 

    def tagged_paras(self): 

        raise NotImplementedError('use tagged_sents() instead')