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

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

# Natural Language Toolkit: IPI PAN Corpus Reader 

# 

# Copyright (C) 2001-2012 NLTK Project 

# Author: Konrad Goluchowski <kodie@mimuw.edu.pl> 

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

# For license information, see LICENSE.TXT 

 

import functools 

 

from nltk import compat 

from .util import StreamBackedCorpusView, concat 

from .api import CorpusReader 

 

def _parse_args(fun): 

    @functools.wraps(fun) 

    def decorator(self, fileids=None, **kwargs): 

        kwargs.pop('tags', None) 

        if not fileids: 

            fileids = self.fileids() 

        return fun(self, fileids, **kwargs) 

    return decorator 

 

class IPIPANCorpusReader(CorpusReader): 

    """ 

    Corpus reader designed to work with corpus created by IPI PAN. 

    See http://korpus.pl/en/ for more details about IPI PAN corpus. 

 

    The corpus includes information about text domain, channel and categories. 

    You can access possible values using ``domains()``, ``channels()`` and 

    ``categories()``. You can use also this metadata to filter files, e.g.: 

    ``fileids(channel='prasa')``, ``fileids(categories='publicystyczny')``. 

 

    The reader supports methods: words, sents, paras and their tagged versions. 

    You can get part of speech instead of full tag by giving "simplify_tags=True" 

    parameter, e.g.: ``tagged_sents(simplify_tags=True)``. 

 

    Also you can get all tags disambiguated tags specifying parameter 

    "one_tag=False", e.g.: ``tagged_paras(one_tag=False)``. 

 

    You can get all tags that were assigned by a morphological analyzer specifying 

    parameter "disamb_only=False", e.g. ``tagged_words(disamb_only=False)``. 

 

    The IPIPAN Corpus contains tags indicating if there is a space between two 

    tokens. To add special "no space" markers, you should specify parameter 

    "append_no_space=True", e.g. ``tagged_words(append_no_space=True)``. 

    As a result in place where there should be no space between two tokens new 

    pair ('', 'no-space') will be inserted (for tagged data) and just '' for 

    methods without tags. 

 

    The corpus reader can also try to append spaces between words. To enable this 

    option, specify parameter "append_space=True", e.g. ``words(append_space=True)``. 

    As a result either ' ' or (' ', 'space') will be inserted between tokens. 

 

    By default, xml entities like &quot; and &amp; are replaced by corresponding 

    characters. You can turn off this feature, specifying parameter 

    "replace_xmlentities=False", e.g. ``words(replace_xmlentities=False)``. 

    """ 

 

    def __init__(self, root, fileids): 

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

 

    def raw(self, fileids=None): 

        if not fileids: 

            fileids = self.fileids() 

        return ''.join([open(fileid, 'r').read() 

            for fileid in self._list_morph_files(fileids)]) 

 

    def channels(self, fileids=None): 

        if not fileids: 

            fileids = self.fileids() 

        return self._parse_header(fileids, 'channel') 

 

    def domains(self, fileids=None): 

        if not fileids: 

            fileids = self.fileids() 

        return self._parse_header(fileids, 'domain') 

 

    def categories(self, fileids=None): 

        if not fileids: 

            fileids = self.fileids() 

        return [self._map_category(cat) 

                for cat in self._parse_header(fileids, 'keyTerm')] 

 

    def fileids(self, channels=None, domains=None, categories=None): 

        if channels is not None and domains is not None and \ 

                categories is not None: 

            raise ValueError('You can specify only one of channels, domains ' 

                             'and categories parameter at once') 

        if channels is None and domains is None and \ 

                categories is None: 

            return CorpusReader.fileids(self) 

        if isinstance(channels, compat.string_types): 

            channels = [channels] 

        if isinstance(domains, compat.string_types): 

            domains = [domains] 

        if isinstance(categories, compat.string_types): 

            categories = [categories] 

        if channels: 

            return self._list_morph_files_by('channel', channels) 

        elif domains: 

            return self._list_morph_files_by('domain', domains) 

        else: 

            return self._list_morph_files_by('keyTerm', categories, 

                    map=self._map_category) 

 

    @_parse_args 

    def sents(self, fileids=None, **kwargs): 

        return concat([self._view(fileid, 

            mode=IPIPANCorpusView.SENTS_MODE, tags=False, **kwargs) 

            for fileid in self._list_morph_files(fileids)]) 

 

    @_parse_args 

    def paras(self, fileids=None, **kwargs): 

        return concat([self._view(fileid, 

            mode=IPIPANCorpusView.PARAS_MODE, tags=False, **kwargs) 

            for fileid in self._list_morph_files(fileids)]) 

 

    @_parse_args 

    def words(self, fileids=None, **kwargs): 

        return concat([self._view(fileid, tags=False, **kwargs) 

            for fileid in self._list_morph_files(fileids)]) 

 

    @_parse_args 

    def tagged_sents(self, fileids=None, **kwargs): 

        return concat([self._view(fileid, mode=IPIPANCorpusView.SENTS_MODE, 

            **kwargs) 

            for fileid in self._list_morph_files(fileids)]) 

 

    @_parse_args 

    def tagged_paras(self, fileids=None, **kwargs): 

        return concat([self._view(fileid, mode=IPIPANCorpusView.PARAS_MODE, 

            **kwargs) 

            for fileid in self._list_morph_files(fileids)]) 

 

    @_parse_args 

    def tagged_words(self, fileids=None, **kwargs): 

        return concat([self._view(fileid, **kwargs) 

            for fileid in self._list_morph_files(fileids)]) 

 

    def _list_morph_files(self, fileids): 

        return [f for f in self.abspaths(fileids)] 

 

    def _list_header_files(self, fileids): 

        return [f.replace('morph.xml', 'header.xml') 

                for f in self._list_morph_files(fileids)] 

 

    def _parse_header(self, fileids, tag): 

        values = set() 

        for f in self._list_header_files(fileids): 

            values_list = self._get_tag(f, tag) 

            for v in values_list: 

                values.add(v) 

        return list(values) 

 

    def _list_morph_files_by(self, tag, values, map=None): 

        fileids = self.fileids() 

        ret_fileids = set() 

        for f in fileids: 

            fp = self.abspath(f).replace('morph.xml', 'header.xml') 

            values_list = self._get_tag(fp, tag) 

            for value in values_list: 

                if map is not None: 

                    value = map(value) 

                if value in values: 

                    ret_fileids.add(f) 

        return list(ret_fileids) 

 

    def _get_tag(self, f, tag): 

        tags = [] 

        header = open(f, 'r').read() 

        tag_end = 0 

        while True: 

            tag_pos = header.find('<'+tag, tag_end) 

            if tag_pos < 0: return tags 

            tag_end = header.find('</'+tag+'>', tag_pos) 

            tags.append(header[tag_pos+len(tag)+2:tag_end]) 

 

    def _map_category(self, cat): 

        pos = cat.find('>') 

        if pos == -1: 

            return cat 

        else: 

            return cat[pos+1:] 

 

    def _view(self, filename, **kwargs): 

        tags = kwargs.pop('tags', True) 

        mode = kwargs.pop('mode', 0) 

        simplify_tags = kwargs.pop('simplify_tags', False) 

        one_tag = kwargs.pop('one_tag', True) 

        disamb_only = kwargs.pop('disamb_only', True) 

        append_no_space = kwargs.pop('append_no_space', False) 

        append_space = kwargs.pop('append_space', False) 

        replace_xmlentities = kwargs.pop('replace_xmlentities', True) 

 

        if len(kwargs) > 0: 

            raise ValueError('Unexpected arguments: %s' % kwargs.keys()) 

        if not one_tag and not disamb_only: 

            raise ValueError('You cannot specify both one_tag=False and ' 

                             'disamb_only=False') 

        if not tags and (simplify_tags or not one_tag or not disamb_only): 

            raise ValueError('You cannot specify simplify_tags, one_tag or ' 

                             'disamb_only with functions other than tagged_*') 

 

        return IPIPANCorpusView(filename, 

                 tags=tags, mode=mode, simplify_tags=simplify_tags, 

                 one_tag=one_tag, disamb_only=disamb_only, 

                 append_no_space=append_no_space, 

                 append_space=append_space, 

                 replace_xmlentities=replace_xmlentities 

                 ) 

 

 

class IPIPANCorpusView(StreamBackedCorpusView): 

 

    WORDS_MODE = 0 

    SENTS_MODE = 1 

    PARAS_MODE = 2 

 

    def __init__(self, filename, startpos=0, **kwargs): 

        StreamBackedCorpusView.__init__(self, filename, None, startpos, None) 

        self.in_sentence = False 

        self.position = 0 

 

        self.show_tags = kwargs.pop('tags', True) 

        self.disamb_only = kwargs.pop('disamb_only', True) 

        self.mode = kwargs.pop('mode', IPIPANCorpusView.WORDS_MODE) 

        self.simplify_tags = kwargs.pop('simplify_tags', False) 

        self.one_tag = kwargs.pop('one_tag', True) 

        self.append_no_space = kwargs.pop('append_no_space', False) 

        self.append_space = kwargs.pop('append_space', False) 

        self.replace_xmlentities = kwargs.pop('replace_xmlentities', True) 

 

    def read_block(self, stream): 

        sentence = [] 

        sentences = [] 

        space = False 

        no_space = False 

 

        tags = set() 

 

        lines = self._read_data(stream) 

 

        while True: 

 

            # we may have only part of last line 

            if len(lines) <= 1: 

                self._seek(stream) 

                lines = self._read_data(stream) 

 

            if lines == ['']: 

                assert not sentences 

                return [] 

 

            line = lines.pop() 

            self.position += len(line) + 1 

 

            if line.startswith('<chunk type="s"'): 

                self.in_sentence = True 

            elif line.startswith('<chunk type="p"'): 

                pass 

            elif line.startswith('<tok'): 

                if self.append_space and space and not no_space: 

                    self._append_space(sentence) 

                space = True 

                no_space = False 

                orth = "" 

                tags = set() 

            elif line.startswith('</chunk'): 

                if self.in_sentence: 

                    self.in_sentence = False 

                    self._seek(stream) 

                    if self.mode == self.SENTS_MODE: 

                        return [sentence] 

                    elif self.mode == self.WORDS_MODE: 

                        if self.append_space: 

                            self._append_space(sentence) 

                        return sentence 

                    else: 

                        sentences.append(sentence) 

                elif self.mode == self.PARAS_MODE: 

                    self._seek(stream) 

                    return [sentences] 

            elif line.startswith('<orth'): 

                orth = line[6:-7] 

                if self.replace_xmlentities: 

                    orth = orth.replace('&quot;', '"').replace('&amp;', '&') 

            elif line.startswith('<lex'): 

                if not self.disamb_only or line.find('disamb=') != -1: 

                    tag = line[line.index('<ctag')+6 : line.index('</ctag') ] 

                    tags.add(tag) 

            elif line.startswith('</tok'): 

                if self.show_tags: 

                    if self.simplify_tags: 

                        tags = [t.split(':')[0] for t in tags] 

                    if not self.one_tag or not self.disamb_only: 

                        sentence.append((orth, tuple(tags))) 

                    else: 

                        sentence.append((orth, tags.pop())) 

                else: 

                    sentence.append(orth) 

            elif line.startswith('<ns/>'): 

                if self.append_space: 

                    no_space = True 

                if self.append_no_space: 

                    if self.show_tags: 

                        sentence.append(('', 'no-space')) 

                    else: 

                        sentence.append('') 

            elif line.startswith('</cesAna'): 

                pass 

 

    def _read_data(self, stream): 

        self.position = stream.tell() 

        buff = stream.read(4096) 

        lines = buff.split('\n') 

        lines.reverse() 

        return lines 

 

    def _seek(self, stream): 

        stream.seek(self.position) 

 

    def _append_space(self, sentence): 

        if self.show_tags: 

            sentence.append((' ', 'space')) 

        else: 

            sentence.append(' ')