childes.doctest 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. =======================
  2. CHILDES Corpus Readers
  3. =======================
  4. Read the XML version of the CHILDES corpus.
  5. How to use CHILDESCorpusReader
  6. ==============================
  7. Read the CHILDESCorpusReader class and read the CHILDES corpus saved in
  8. the nltk_data directory.
  9. >>> import nltk
  10. >>> from nltk.corpus.reader import CHILDESCorpusReader
  11. >>> corpus_root = nltk.data.find('corpora/childes/data-xml/Eng-USA-MOR/')
  12. Reading files in the Valian corpus (Valian, 1991).
  13. >>> valian = CHILDESCorpusReader(corpus_root, 'Valian/.*.xml')
  14. >>> valian.fileids()
  15. ['Valian/01a.xml', 'Valian/01b.xml', 'Valian/02a.xml', 'Valian/02b.xml',...
  16. Count the number of files
  17. >>> len(valian.fileids())
  18. 43
  19. Printing properties of the corpus files.
  20. >>> corpus_data = valian.corpus(valian.fileids())
  21. >>> print(corpus_data[0]['Lang'])
  22. eng
  23. >>> for key in sorted(corpus_data[0].keys()):
  24. ... print(key, ": ", corpus_data[0][key])
  25. Corpus : valian
  26. Date : 1986-03-04
  27. Id : 01a
  28. Lang : eng
  29. Version : 2.0.1
  30. {http://www.w3.org/2001/XMLSchema-instance}schemaLocation : http://www.talkbank.org/ns/talkbank http://talkbank.org/software/talkbank.xsd
  31. Printing information of participants of the corpus. The most common codes for
  32. the participants are 'CHI' (target child), 'MOT' (mother), and 'INV' (investigator).
  33. >>> corpus_participants = valian.participants(valian.fileids())
  34. >>> for this_corpus_participants in corpus_participants[:2]:
  35. ... for key in sorted(this_corpus_participants.keys()):
  36. ... dct = this_corpus_participants[key]
  37. ... print(key, ": ", [(k, dct[k]) for k in sorted(dct.keys())])
  38. CHI : [('age', 'P2Y1M3D'), ('group', 'normal'), ('id', 'CHI'), ('language', 'eng'), ('role', 'Target_Child'), ('sex', 'female')]
  39. INV : [('id', 'INV'), ('language', 'eng'), ('role', 'Investigator')]
  40. MOT : [('id', 'MOT'), ('language', 'eng'), ('role', 'Mother')]
  41. CHI : [('age', 'P2Y1M12D'), ('group', 'normal'), ('id', 'CHI'), ('language', 'eng'), ('role', 'Target_Child'), ('sex', 'female')]
  42. INV : [('id', 'INV'), ('language', 'eng'), ('role', 'Investigator')]
  43. MOT : [('id', 'MOT'), ('language', 'eng'), ('role', 'Mother')]
  44. printing words.
  45. >>> valian.words('Valian/01a.xml')
  46. ['at', 'Parent', "Lastname's", 'house', 'with', 'Child', 'Lastname', ...
  47. printing sentences.
  48. >>> valian.sents('Valian/01a.xml')
  49. [['at', 'Parent', "Lastname's", 'house', 'with', 'Child', 'Lastname',
  50. 'and', 'it', 'is', 'March', 'fourth', 'I', 'believe', 'and', 'when',
  51. 'was', "Parent's", 'birthday'], ["Child's"], ['oh', "I'm", 'sorry'],
  52. ["that's", 'okay'], ...
  53. You can specify the participants with the argument *speaker*.
  54. >>> valian.words('Valian/01a.xml',speaker=['INV'])
  55. ['at', 'Parent', "Lastname's", 'house', 'with', 'Child', 'Lastname', ...
  56. >>> valian.words('Valian/01a.xml',speaker=['MOT'])
  57. ["Child's", "that's", 'okay', 'February', 'first', 'nineteen', ...
  58. >>> valian.words('Valian/01a.xml',speaker=['CHI'])
  59. ['tape', 'it', 'up', 'and', 'two', 'tape', 'players', 'have',...
  60. tagged_words() and tagged_sents() return the usual (word,pos) tuple lists.
  61. POS tags in the CHILDES are automatically assigned by MOR and POST programs
  62. (MacWhinney, 2000).
  63. >>> valian.tagged_words('Valian/01a.xml')[:30]
  64. [('at', 'prep'), ('Parent', 'n:prop'), ("Lastname's", 'n:prop'), ('house', 'n'),
  65. ('with', 'prep'), ('Child', 'n:prop'), ('Lastname', 'n:prop'), ('and', 'coord'),
  66. ('it', 'pro'), ('is', 'v:cop'), ('March', 'n:prop'), ('fourth', 'adj'),
  67. ('I', 'pro:sub'), ('believe', 'v'), ('and', 'coord'), ('when', 'adv:wh'),
  68. ('was', 'v:cop'), ("Parent's", 'n:prop'), ('birthday', 'n'), ("Child's", 'n:prop'),
  69. ('oh', 'co'), ("I'm", 'pro:sub'), ('sorry', 'adj'), ("that's", 'pro:dem'),
  70. ('okay', 'adj'), ('February', 'n:prop'), ('first', 'adj'),
  71. ('nineteen', 'det:num'), ('eighty', 'det:num'), ('four', 'det:num')]
  72. >>> valian.tagged_sents('Valian/01a.xml')[:10]
  73. [[('at', 'prep'), ('Parent', 'n:prop'), ("Lastname's", 'n:prop'), ('house', 'n'),
  74. ('with', 'prep'), ('Child', 'n:prop'), ('Lastname', 'n:prop'), ('and', 'coord'),
  75. ('it', 'pro'), ('is', 'v:cop'), ('March', 'n:prop'), ('fourth', 'adj'),
  76. ('I', 'pro:sub'), ('believe', 'v'), ('and', 'coord'), ('when', 'adv:wh'),
  77. ('was', 'v:cop'), ("Parent's", 'n:prop'), ('birthday', 'n')],
  78. [("Child's", 'n:prop')], [('oh', 'co'), ("I'm", 'pro:sub'), ('sorry', 'adj')],
  79. [("that's", 'pro:dem'), ('okay', 'adj')],
  80. [('February', 'n:prop'), ('first', 'adj'), ('nineteen', 'det:num'),
  81. ('eighty', 'det:num'), ('four', 'det:num')],
  82. [('great', 'adj')],
  83. [('and', 'coord'), ("she's", 'pro:sub'), ('two', 'det:num'), ('years', 'n'), ('old', 'adj')],
  84. [('correct', 'adj')],
  85. [('okay', 'co')], [('she', 'pro:sub'), ('just', 'adv:int'), ('turned', 'part'), ('two', 'det:num'),
  86. ('a', 'det'), ('month', 'n'), ('ago', 'adv')]]
  87. When the argument *stem* is true, the word stems (e.g., 'is' -> 'be-3PS') are
  88. used instread of the original words.
  89. >>> valian.words('Valian/01a.xml')[:30]
  90. ['at', 'Parent', "Lastname's", 'house', 'with', 'Child', 'Lastname', 'and', 'it', 'is', ...
  91. >>> valian.words('Valian/01a.xml',stem=True)[:30]
  92. ['at', 'Parent', 'Lastname', 's', 'house', 'with', 'Child', 'Lastname', 'and', 'it', 'be-3S', ...
  93. When the argument *replace* is true, the replaced words are used instread of
  94. the original words.
  95. >>> valian.words('Valian/01a.xml',speaker='CHI')[247]
  96. 'tikteat'
  97. >>> valian.words('Valian/01a.xml',speaker='CHI',replace=True)[247]
  98. 'trick'
  99. When the argument *relation* is true, the relational relationships in the
  100. sentence are returned. See Sagae et al. (2010) for details of the relational
  101. structure adopted in the CHILDES.
  102. >>> valian.words('Valian/01a.xml',relation=True)[:10]
  103. [[('at', 'prep', '1|0|ROOT'), ('Parent', 'n', '2|5|VOC'), ('Lastname', 'n', '3|5|MOD'), ('s', 'poss', '4|5|MOD'), ('house', 'n', '5|1|POBJ'), ('with', 'prep', '6|1|JCT'), ('Child', 'n', '7|8|NAME'), ('Lastname', 'n', '8|6|POBJ'), ('and', 'coord', '9|8|COORD'), ('it', 'pro', '10|11|SUBJ'), ('be-3S', 'v', '11|9|COMP'), ('March', 'n', '12|11|PRED'), ('fourth', 'adj', '13|12|MOD'), ('I', 'pro', '15|16|SUBJ'), ('believe', 'v', '16|14|ROOT'), ('and', 'coord', '18|17|ROOT'), ('when', 'adv', '19|20|PRED'), ('be-PAST', 'v', '20|18|COMP'), ('Parent', 'n', '21|23|MOD'), ('s', 'poss', '22|23|MOD'), ('birth', 'n', '23|20|SUBJ')], [('Child', 'n', '1|2|MOD'), ('s', 'poss', '2|0|ROOT')], [('oh', 'co', '1|4|COM'), ('I', 'pro', '3|4|SUBJ'), ('be', 'v', '4|0|ROOT'), ('sorry', 'adj', '5|4|PRED')], [('that', 'pro', '1|2|SUBJ'), ('be', 'v', '2|0|ROOT'), ('okay', 'adj', '3|2|PRED')], [('February', 'n', '1|6|VOC'), ('first', 'adj', '2|6|ENUM'), ('nineteen', 'det', '4|6|ENUM'), ('eighty', 'det', '5|6|ENUM'), ('four', 'det', '6|0|ROOT')], [('great', 'adj', '1|0|ROOT')], [('and', 'coord', '1|0|ROOT'), ('she', 'pro', '2|1|ROOT'), ('be', 'aux', '3|5|AUX'), ('two', 'det', '4|5|QUANT'), ('year-PL', 'n', '5|2|ROOT'), ('old', 'adj', '6|5|MOD')], [('correct', 'adj', '1|0|ROOT')], [('okay', 'co', '1|0|ROOT')], [('she', 'pro', '1|0|ROOT'), ('just', 'adv', '2|3|JCT'), ('turn-PERF', 'part', '3|1|XCOMP'), ('two', 'det', '4|6|QUANT'), ('a', 'det', '5|6|DET'), ('month', 'n', '6|3|OBJ'), ('ago', 'adv', '7|3|JCT')]]
  104. Printing age. When the argument *month* is true, the age information in
  105. the CHILDES format is converted into the number of months.
  106. >>> valian.age()
  107. ['P2Y1M3D', 'P2Y1M12D', 'P1Y9M21D', 'P1Y9M28D', 'P2Y1M23D', ...
  108. >>> valian.age('Valian/01a.xml')
  109. ['P2Y1M3D']
  110. >>> valian.age('Valian/01a.xml',month=True)
  111. [25]
  112. Printing MLU. The criteria for the MLU computation is broadly based on
  113. Brown (1973).
  114. >>> valian.MLU()
  115. [2.3574660633484..., 2.292682926829..., 3.492857142857..., 2.961783439490...,
  116. 2.0842696629213..., 3.169811320754..., 3.137404580152..., 3.0578034682080...,
  117. 4.090163934426..., 3.488372093023..., 2.8773584905660..., 3.4792899408284...,
  118. 4.0111940298507..., 3.456790123456..., 4.487603305785..., 4.007936507936...,
  119. 5.25, 5.154696132596..., ...]
  120. >>> valian.MLU('Valian/01a.xml')
  121. [2.35746606334...]
  122. Basic stuff
  123. ==============================
  124. Count the number of words and sentences of each file.
  125. >>> valian = CHILDESCorpusReader(corpus_root, 'Valian/.*.xml')
  126. >>> for this_file in valian.fileids()[:6]:
  127. ... print(valian.corpus(this_file)[0]['Corpus'], valian.corpus(this_file)[0]['Id'])
  128. ... print("num of words: %i" % len(valian.words(this_file)))
  129. ... print("num of sents: %i" % len(valian.sents(this_file)))
  130. valian 01a
  131. num of words: 3606
  132. num of sents: 1027
  133. valian 01b
  134. num of words: 4376
  135. num of sents: 1274
  136. valian 02a
  137. num of words: 2673
  138. num of sents: 801
  139. valian 02b
  140. num of words: 5020
  141. num of sents: 1583
  142. valian 03a
  143. num of words: 2743
  144. num of sents: 988
  145. valian 03b
  146. num of words: 4409
  147. num of sents: 1397