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

# Natural Language Toolkit: NPS Chat Corpus Reader 

# 

# 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 

 

import re 

import textwrap 

 

from nltk.util import LazyConcatenation 

from nltk.internals import ElementWrapper 

 

from .util import * 

from .api import * 

from .xmldocs import * 

 

class NPSChatCorpusReader(XMLCorpusReader): 

 

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

        XMLCorpusReader.__init__(self, root, fileids, wrap_etree) 

        self._tag_mapping_function = tag_mapping_function 

 

    def xml_posts(self, fileids=None): 

        if self._wrap_etree: 

            return concat([XMLCorpusView(fileid, 'Session/Posts/Post', 

                                         self._wrap_elt) 

                           for fileid in self.abspaths(fileids)]) 

        else: 

            return concat([XMLCorpusView(fileid, 'Session/Posts/Post') 

                           for fileid in self.abspaths(fileids)]) 

 

    def posts(self, fileids=None): 

        return concat([XMLCorpusView(fileid, 'Session/Posts/Post/terminals', 

                                     self._elt_to_words) 

                       for fileid in self.abspaths(fileids)]) 

 

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

        def reader(elt, handler): 

            return self._elt_to_tagged_words(elt, handler, simplify_tags) 

        return concat([XMLCorpusView(fileid, 'Session/Posts/Post/terminals', 

                                     reader) 

                       for fileid in self.abspaths(fileids)]) 

 

    def words(self, fileids=None): 

        return LazyConcatenation(self.posts(fileids)) 

 

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

        return LazyConcatenation(self.tagged_posts(fileids, simplify_tags)) 

 

    def _wrap_elt(self, elt, handler): 

        return ElementWrapper(elt) 

 

    def _elt_to_words(self, elt, handler): 

        return [self._simplify_username(t.attrib['word']) 

                for t in elt.findall('t')] 

 

    def _elt_to_tagged_words(self, elt, handler, simplify_tags=False): 

        tagged_post = [(self._simplify_username(t.attrib['word']), 

                        t.attrib['pos']) for t in elt.findall('t')] 

        if simplify_tags: 

            tagged_post = [(w, self._tag_mapping_function(t)) 

                           for (w,t) in tagged_post] 

        return tagged_post 

 

    @staticmethod 

    def _simplify_username(word): 

        if 'User' in word: 

            word = 'U' + word.split('User', 1)[1] 

        return word