nltk 3.2__zip → 3.3__zip

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (406) hide show
  1. {nltk-3.2 → nltk-3.3}/INSTALL.txt +6 -6
  2. {nltk-3.2 → nltk-3.3}/LICENSE.txt +13 -13
  3. {nltk-3.2 → nltk-3.3}/MANIFEST.in +6 -6
  4. {nltk-3.2/nltk.egg-info → nltk-3.3}/PKG-INFO +13 -5
  5. nltk-3.2/README.txt → nltk-3.3/README.md +49 -48
  6. nltk-3.3/nltk/VERSION +1 -0
  7. {nltk-3.2 → nltk-3.3}/nltk/__init__.py +185 -185
  8. {nltk-3.2 → nltk-3.3}/nltk/app/__init__.py +52 -53
  9. {nltk-3.2 → nltk-3.3}/nltk/app/chartparser_app.py +2276 -2276
  10. {nltk-3.2 → nltk-3.3}/nltk/app/chunkparser_app.py +1262 -1264
  11. {nltk-3.2 → nltk-3.3}/nltk/app/collocations_app.py +347 -350
  12. {nltk-3.2 → nltk-3.3}/nltk/app/concordance_app.py +568 -570
  13. {nltk-3.2 → nltk-3.3}/nltk/app/nemo_app.py +156 -155
  14. {nltk-3.2 → nltk-3.3}/nltk/app/rdparser_app.py +892 -894
  15. {nltk-3.2 → nltk-3.3}/nltk/app/srparser_app.py +809 -809
  16. {nltk-3.2 → nltk-3.3}/nltk/app/wordfreq_app.py +32 -32
  17. {nltk-3.2 → nltk-3.3}/nltk/app/wordnet_app.py +968 -968
  18. {nltk-3.2 → nltk-3.3}/nltk/book.py +91 -90
  19. {nltk-3.2 → nltk-3.3}/nltk/ccg/__init__.py +22 -22
  20. {nltk-3.2 → nltk-3.3}/nltk/ccg/api.py +359 -335
  21. {nltk-3.2 → nltk-3.3}/nltk/ccg/chart.py +394 -361
  22. {nltk-3.2 → nltk-3.3}/nltk/ccg/combinator.py +343 -312
  23. {nltk-3.2 → nltk-3.3}/nltk/ccg/lexicon.py +328 -290
  24. nltk-3.3/nltk/ccg/logic.py +46 -0
  25. {nltk-3.2 → nltk-3.3}/nltk/chat/__init__.py +49 -49
  26. {nltk-3.2 → nltk-3.3}/nltk/chat/eliza.py +244 -244
  27. {nltk-3.2 → nltk-3.3}/nltk/chat/iesha.py +140 -140
  28. {nltk-3.2 → nltk-3.3}/nltk/chat/rude.py +92 -92
  29. {nltk-3.2 → nltk-3.3}/nltk/chat/suntsu.py +117 -117
  30. {nltk-3.2 → nltk-3.3}/nltk/chat/util.py +122 -120
  31. {nltk-3.2 → nltk-3.3}/nltk/chat/zen.py +282 -282
  32. {nltk-3.2 → nltk-3.3}/nltk/chunk/__init__.py +190 -190
  33. {nltk-3.2 → nltk-3.3}/nltk/chunk/api.py +51 -51
  34. {nltk-3.2 → nltk-3.3}/nltk/chunk/named_entity.py +331 -331
  35. {nltk-3.2 → nltk-3.3}/nltk/chunk/regexp.py +1390 -1384
  36. {nltk-3.2 → nltk-3.3}/nltk/chunk/util.py +600 -600
  37. {nltk-3.2 → nltk-3.3}/nltk/classify/__init__.py +98 -98
  38. {nltk-3.2 → nltk-3.3}/nltk/classify/api.py +193 -193
  39. {nltk-3.2 → nltk-3.3}/nltk/classify/decisiontree.py +295 -295
  40. {nltk-3.2 → nltk-3.3}/nltk/classify/maxent.py +1510 -1492
  41. {nltk-3.2 → nltk-3.3}/nltk/classify/megam.py +179 -178
  42. {nltk-3.2 → nltk-3.3}/nltk/classify/naivebayes.py +245 -242
  43. {nltk-3.2 → nltk-3.3}/nltk/classify/positivenaivebayes.py +170 -170
  44. {nltk-3.2 → nltk-3.3}/nltk/classify/rte_classify.py +160 -191
  45. {nltk-3.2 → nltk-3.3}/nltk/classify/scikitlearn.py +153 -151
  46. {nltk-3.2 → nltk-3.3}/nltk/classify/senna.py +183 -181
  47. {nltk-3.2 → nltk-3.3}/nltk/classify/svm.py +15 -15
  48. {nltk-3.2 → nltk-3.3}/nltk/classify/tadm.py +113 -112
  49. {nltk-3.2 → nltk-3.3}/nltk/classify/textcat.py +193 -193
  50. {nltk-3.2 → nltk-3.3}/nltk/classify/util.py +324 -312
  51. {nltk-3.2 → nltk-3.3}/nltk/classify/weka.py +346 -345
  52. {nltk-3.2 → nltk-3.3}/nltk/cluster/__init__.py +86 -86
  53. {nltk-3.2 → nltk-3.3}/nltk/cluster/api.py +74 -70
  54. {nltk-3.2 → nltk-3.3}/nltk/cluster/em.py +247 -247
  55. {nltk-3.2 → nltk-3.3}/nltk/cluster/gaac.py +168 -168
  56. {nltk-3.2 → nltk-3.3}/nltk/cluster/kmeans.py +221 -221
  57. {nltk-3.2 → nltk-3.3}/nltk/cluster/util.py +304 -289
  58. nltk-3.3/nltk/collections.py +640 -0
  59. {nltk-3.2 → nltk-3.3}/nltk/collocations.py +388 -388
  60. nltk-3.3/nltk/compat.py +365 -0
  61. {nltk-3.2 → nltk-3.3}/nltk/corpus/__init__.py +341 -320
  62. {nltk-3.2 → nltk-3.3}/nltk/corpus/europarl_raw.py +44 -44
  63. {nltk-3.2 → nltk-3.3}/nltk/corpus/reader/__init__.py +147 -145
  64. {nltk-3.2 → nltk-3.3}/nltk/corpus/reader/aligned.py +115 -114
  65. {nltk-3.2 → nltk-3.3}/nltk/corpus/reader/api.py +448 -446
  66. {nltk-3.2 → nltk-3.3}/nltk/corpus/reader/bnc.py +252 -252
  67. {nltk-3.2 → nltk-3.3}/nltk/corpus/reader/bracket_parse.py +227 -227
  68. {nltk-3.2 → nltk-3.3}/nltk/corpus/reader/categorized_sents.py +179 -178
  69. {nltk-3.2 → nltk-3.3}/nltk/corpus/reader/chasen.py +140 -139
  70. {nltk-3.2 → nltk-3.3}/nltk/corpus/reader/childes.py +522 -494
  71. {nltk-3.2 → nltk-3.3}/nltk/corpus/reader/chunked.py +212 -212
  72. {nltk-3.2 → nltk-3.3}/nltk/corpus/reader/cmudict.py +97 -95
  73. {nltk-3.2 → nltk-3.3}/nltk/corpus/reader/comparative_sents.py +280 -278
  74. {nltk-3.2 → nltk-3.3}/nltk/corpus/reader/conll.py +528 -521
  75. {nltk-3.2 → nltk-3.3}/nltk/corpus/reader/crubadan.py +116 -116
  76. {nltk-3.2 → nltk-3.3}/nltk/corpus/reader/dependency.py +101 -101
  77. {nltk-3.2 → nltk-3.3}/nltk/corpus/reader/framenet.py +3059 -2093
  78. {nltk-3.2 → nltk-3.3}/nltk/corpus/reader/ieer.py +112 -111
  79. {nltk-3.2 → nltk-3.3}/nltk/corpus/reader/indian.py +85 -86
  80. {nltk-3.2 → nltk-3.3}/nltk/corpus/reader/ipipan.py +332 -331
  81. {nltk-3.2 → nltk-3.3}/nltk/corpus/reader/knbc.py +187 -187
  82. {nltk-3.2 → nltk-3.3}/nltk/corpus/reader/lin.py +156 -156
  83. {nltk-3.2 → nltk-3.3}/nltk/corpus/reader/mte.py +311 -310
  84. {nltk-3.2 → nltk-3.3}/nltk/corpus/reader/nkjp.py +429 -428
  85. {nltk-3.2 → nltk-3.3}/nltk/corpus/reader/nombank.py +421 -421
  86. {nltk-3.2 → nltk-3.3}/nltk/corpus/reader/nps_chat.py +73 -73
  87. {nltk-3.2 → nltk-3.3}/nltk/corpus/reader/opinion_lexicon.py +115 -115
  88. {nltk-3.2 → nltk-3.3}/nltk/corpus/reader/panlex_lite.py +165 -165
  89. nltk-3.3/nltk/corpus/reader/pl196x.py +292 -0
  90. {nltk-3.2 → nltk-3.3}/nltk/corpus/reader/plaintext.py +232 -233
  91. {nltk-3.2 → nltk-3.3}/nltk/corpus/reader/ppattach.py +96 -95
  92. {nltk-3.2 → nltk-3.3}/nltk/corpus/reader/propbank.py +479 -481
  93. {nltk-3.2 → nltk-3.3}/nltk/corpus/reader/pros_cons.py +128 -126
  94. {nltk-3.2 → nltk-3.3}/nltk/corpus/reader/reviews.py +330 -328
  95. {nltk-3.2 → nltk-3.3}/nltk/corpus/reader/rte.py +144 -146
  96. {nltk-3.2 → nltk-3.3}/nltk/corpus/reader/semcor.py +256 -256
  97. {nltk-3.2 → nltk-3.3}/nltk/corpus/reader/senseval.py +203 -201
  98. {nltk-3.2 → nltk-3.3}/nltk/corpus/reader/sentiwordnet.py +137 -137
  99. {nltk-3.2 → nltk-3.3}/nltk/corpus/reader/sinica_treebank.py +75 -75
  100. {nltk-3.2 → nltk-3.3}/nltk/corpus/reader/string_category.py +60 -59
  101. {nltk-3.2 → nltk-3.3}/nltk/corpus/reader/switchboard.py +119 -119
  102. {nltk-3.2 → nltk-3.3}/nltk/corpus/reader/tagged.py +295 -294
  103. {nltk-3.2 → nltk-3.3}/nltk/corpus/reader/timit.py +452 -450
  104. {nltk-3.2 → nltk-3.3}/nltk/corpus/reader/toolbox.py +68 -68
  105. {nltk-3.2 → nltk-3.3}/nltk/corpus/reader/twitter.py +155 -157
  106. {nltk-3.2 → nltk-3.3}/nltk/corpus/reader/udhr.py +80 -80
  107. {nltk-3.2 → nltk-3.3}/nltk/corpus/reader/util.py +804 -800
  108. nltk-3.3/nltk/corpus/reader/verbnet.py +595 -0
  109. nltk-3.3/nltk/corpus/reader/wordlist.py +134 -0
  110. {nltk-3.2 → nltk-3.3}/nltk/corpus/reader/wordnet.py +2108 -2036
  111. {nltk-3.2 → nltk-3.3}/nltk/corpus/reader/xmldocs.py +392 -387
  112. {nltk-3.2 → nltk-3.3}/nltk/corpus/reader/ycoe.py +243 -242
  113. {nltk-3.2 → nltk-3.3}/nltk/corpus/util.py +144 -127
  114. {nltk-3.2 → nltk-3.3}/nltk/data.py +1487 -1451
  115. {nltk-3.2 → nltk-3.3}/nltk/decorators.py +219 -215
  116. {nltk-3.2 → nltk-3.3}/nltk/downloader.py +2278 -2274
  117. {nltk-3.2 → nltk-3.3}/nltk/draw/__init__.py +27 -27
  118. {nltk-3.2 → nltk-3.3}/nltk/draw/cfg.py +774 -777
  119. {nltk-3.2 → nltk-3.3}/nltk/draw/dispersion.py +58 -58
  120. {nltk-3.2 → nltk-3.3}/nltk/draw/table.py +1098 -1098
  121. {nltk-3.2 → nltk-3.3}/nltk/draw/tree.py +963 -966
  122. {nltk-3.2 → nltk-3.3}/nltk/draw/util.py +2356 -2356
  123. {nltk-3.2 → nltk-3.3}/nltk/featstruct.py +2617 -2500
  124. {nltk-3.2 → nltk-3.3}/nltk/grammar.py +1559 -1516
  125. {nltk-3.2 → nltk-3.3}/nltk/help.py +56 -56
  126. {nltk-3.2 → nltk-3.3}/nltk/inference/__init__.py +20 -20
  127. {nltk-3.2 → nltk-3.3}/nltk/inference/api.py +597 -589
  128. {nltk-3.2 → nltk-3.3}/nltk/inference/discourse.py +612 -608
  129. {nltk-3.2 → nltk-3.3}/nltk/inference/mace.py +311 -311
  130. {nltk-3.2 → nltk-3.3}/nltk/inference/nonmonotonic.py +509 -509
  131. {nltk-3.2 → nltk-3.3}/nltk/inference/prover9.py +431 -431
  132. {nltk-3.2 → nltk-3.3}/nltk/inference/resolution.py +687 -687
  133. {nltk-3.2 → nltk-3.3}/nltk/inference/tableau.py +607 -607
  134. {nltk-3.2 → nltk-3.3}/nltk/internals.py +982 -982
  135. {nltk-3.2 → nltk-3.3}/nltk/jsontags.py +63 -63
  136. {nltk-3.2 → nltk-3.3}/nltk/lazyimport.py +142 -142
  137. {nltk-3.2 → nltk-3.3}/nltk/metrics/__init__.py +30 -29
  138. {nltk-3.2 → nltk-3.3}/nltk/metrics/agreement.py +421 -401
  139. nltk-3.3/nltk/metrics/aline.py +607 -0
  140. {nltk-3.2 → nltk-3.3}/nltk/metrics/association.py +414 -410
  141. {nltk-3.2 → nltk-3.3}/nltk/metrics/confusionmatrix.py +206 -206
  142. {nltk-3.2 → nltk-3.3}/nltk/metrics/distance.py +207 -202
  143. {nltk-3.2 → nltk-3.3}/nltk/metrics/paice.py +381 -381
  144. {nltk-3.2 → nltk-3.3}/nltk/metrics/scores.py +228 -228
  145. {nltk-3.2 → nltk-3.3}/nltk/metrics/segmentation.py +231 -232
  146. {nltk-3.2 → nltk-3.3}/nltk/metrics/spearman.py +69 -69
  147. {nltk-3.2 → nltk-3.3}/nltk/misc/__init__.py +11 -11
  148. {nltk-3.2 → nltk-3.3}/nltk/misc/babelfish.py +10 -10
  149. {nltk-3.2 → nltk-3.3}/nltk/misc/chomsky.py +135 -133
  150. {nltk-3.2 → nltk-3.3}/nltk/misc/minimalset.py +83 -83
  151. {nltk-3.2 → nltk-3.3}/nltk/misc/sort.py +157 -157
  152. {nltk-3.2 → nltk-3.3}/nltk/misc/wordfinder.py +129 -129
  153. {nltk-3.2 → nltk-3.3}/nltk/parse/__init__.py +81 -80
  154. {nltk-3.2 → nltk-3.3}/nltk/parse/api.py +66 -66
  155. {nltk-3.2 → nltk-3.3}/nltk/parse/bllip.py +282 -282
  156. {nltk-3.2 → nltk-3.3}/nltk/parse/chart.py +1682 -1681
  157. nltk-3.3/nltk/parse/corenlp.py +780 -0
  158. {nltk-3.2 → nltk-3.3}/nltk/parse/dependencygraph.py +760 -758
  159. {nltk-3.2 → nltk-3.3}/nltk/parse/earleychart.py +452 -451
  160. {nltk-3.2 → nltk-3.3}/nltk/parse/evaluate.py +129 -129
  161. {nltk-3.2 → nltk-3.3}/nltk/parse/featurechart.py +580 -578
  162. {nltk-3.2 → nltk-3.3}/nltk/parse/generate.py +87 -76
  163. {nltk-3.2 → nltk-3.3}/nltk/parse/malt.py +356 -356
  164. {nltk-3.2 → nltk-3.3}/nltk/parse/nonprojectivedependencyparser.py +783 -783
  165. {nltk-3.2 → nltk-3.3}/nltk/parse/pchart.py +522 -522
  166. {nltk-3.2 → nltk-3.3}/nltk/parse/projectivedependencyparser.py +579 -578
  167. {nltk-3.2 → nltk-3.3}/nltk/parse/recursivedescent.py +655 -656
  168. {nltk-3.2 → nltk-3.3}/nltk/parse/shiftreduce.py +458 -459
  169. {nltk-3.2 → nltk-3.3}/nltk/parse/stanford.py +431 -406
  170. {nltk-3.2 → nltk-3.3}/nltk/parse/transitionparser.py +774 -772
  171. {nltk-3.2 → nltk-3.3}/nltk/parse/util.py +231 -231
  172. {nltk-3.2 → nltk-3.3}/nltk/parse/viterbi.py +401 -401
  173. {nltk-3.2 → nltk-3.3}/nltk/probability.py +2410 -2355
  174. {nltk-3.2 → nltk-3.3}/nltk/sem/__init__.py +61 -61
  175. {nltk-3.2 → nltk-3.3}/nltk/sem/boxer.py +1261 -1261
  176. {nltk-3.2 → nltk-3.3}/nltk/sem/chat80.py +782 -782
  177. {nltk-3.2 → nltk-3.3}/nltk/sem/cooper_storage.py +118 -118
  178. {nltk-3.2 → nltk-3.3}/nltk/sem/drt.py +1258 -1257
  179. {nltk-3.2 → nltk-3.3}/nltk/sem/drt_glue_demo.py +485 -489
  180. {nltk-3.2 → nltk-3.3}/nltk/sem/evaluate.py +790 -784
  181. {nltk-3.2 → nltk-3.3}/nltk/sem/glue.py +696 -694
  182. {nltk-3.2 → nltk-3.3}/nltk/sem/hole.py +386 -384
  183. {nltk-3.2 → nltk-3.3}/nltk/sem/lfg.py +210 -208
  184. {nltk-3.2 → nltk-3.3}/nltk/sem/linearlogic.py +451 -449
  185. {nltk-3.2 → nltk-3.3}/nltk/sem/logic.py +1910 -1910
  186. {nltk-3.2 → nltk-3.3}/nltk/sem/relextract.py +476 -483
  187. {nltk-3.2 → nltk-3.3}/nltk/sem/skolemize.py +101 -101
  188. {nltk-3.2 → nltk-3.3}/nltk/sem/util.py +249 -249
  189. {nltk-3.2 → nltk-3.3}/nltk/sentiment/__init__.py +14 -14
  190. {nltk-3.2 → nltk-3.3}/nltk/sentiment/sentiment_analyzer.py +229 -229
  191. {nltk-3.2 → nltk-3.3}/nltk/sentiment/util.py +762 -752
  192. {nltk-3.2 → nltk-3.3}/nltk/sentiment/vader.py +443 -445
  193. {nltk-3.2 → nltk-3.3}/nltk/stem/__init__.py +31 -33
  194. {nltk-3.2 → nltk-3.3}/nltk/stem/api.py +28 -25
  195. nltk-3.3/nltk/stem/arlstem.py +369 -0
  196. {nltk-3.2 → nltk-3.3}/nltk/stem/isri.py +345 -345
  197. {nltk-3.2 → nltk-3.3}/nltk/stem/lancaster.py +337 -310
  198. nltk-3.3/nltk/stem/porter.py +710 -0
  199. {nltk-3.2 → nltk-3.3}/nltk/stem/regexp.py +61 -61
  200. {nltk-3.2 → nltk-3.3}/nltk/stem/rslp.py +142 -142
  201. {nltk-3.2 → nltk-3.3}/nltk/stem/snowball.py +4236 -3712
  202. {nltk-3.2 → nltk-3.3}/nltk/stem/util.py +22 -12
  203. {nltk-3.2 → nltk-3.3}/nltk/stem/wordnet.py +51 -51
  204. {nltk-3.2 → nltk-3.3}/nltk/tag/__init__.py +152 -127
  205. {nltk-3.2 → nltk-3.3}/nltk/tag/api.py +86 -81
  206. {nltk-3.2 → nltk-3.3}/nltk/tag/brill.py +424 -426
  207. {nltk-3.2 → nltk-3.3}/nltk/tag/brill_trainer.py +608 -608
  208. {nltk-3.2 → nltk-3.3}/nltk/tag/crf.py +204 -200
  209. {nltk-3.2 → nltk-3.3}/nltk/tag/hmm.py +1277 -1277
  210. {nltk-3.2 → nltk-3.3}/nltk/tag/hunpos.py +132 -132
  211. {nltk-3.2 → nltk-3.3}/nltk/tag/mapping.py +101 -101
  212. {nltk-3.2 → nltk-3.3}/nltk/tag/perceptron.py +329 -314
  213. {nltk-3.2 → nltk-3.3}/nltk/tag/senna.py +147 -145
  214. {nltk-3.2 → nltk-3.3}/nltk/tag/sequential.py +740 -736
  215. {nltk-3.2 → nltk-3.3}/nltk/tag/stanford.py +219 -196
  216. {nltk-3.2 → nltk-3.3}/nltk/tag/tnt.py +595 -604
  217. {nltk-3.2 → nltk-3.3}/nltk/tag/util.py +72 -72
  218. {nltk-3.2 → nltk-3.3}/nltk/tbl/__init__.py +28 -28
  219. {nltk-3.2 → nltk-3.3}/nltk/tbl/api.py +1 -1
  220. {nltk-3.2 → nltk-3.3}/nltk/tbl/demo.py +366 -366
  221. {nltk-3.2 → nltk-3.3}/nltk/tbl/erroranalysis.py +39 -39
  222. {nltk-3.2 → nltk-3.3}/nltk/tbl/feature.py +259 -261
  223. {nltk-3.2 → nltk-3.3}/nltk/tbl/rule.py +321 -321
  224. {nltk-3.2 → nltk-3.3}/nltk/tbl/template.py +303 -308
  225. {nltk-3.2 → nltk-3.3}/nltk/test/__init__.py +18 -18
  226. {nltk-3.2 → nltk-3.3}/nltk/test/all.py +24 -23
  227. {nltk-3.2 → nltk-3.3}/nltk/test/bleu.doctest +14 -14
  228. {nltk-3.2 → nltk-3.3}/nltk/test/bnc.doctest +60 -60
  229. {nltk-3.2 → nltk-3.3}/nltk/test/ccg.doctest +376 -395
  230. nltk-3.3/nltk/test/ccg_semantics.doctest +553 -0
  231. {nltk-3.2 → nltk-3.3}/nltk/test/chat80.doctest +234 -234
  232. {nltk-3.2 → nltk-3.3}/nltk/test/childes.doctest +184 -184
  233. {nltk-3.2 → nltk-3.3}/nltk/test/childes_fixt.py +14 -13
  234. {nltk-3.2 → nltk-3.3}/nltk/test/chunk.doctest +373 -373
  235. {nltk-3.2 → nltk-3.3}/nltk/test/classify.doctest +185 -185
  236. {nltk-3.2 → nltk-3.3}/nltk/test/classify_fixt.py +11 -10
  237. nltk-3.3/nltk/test/collections.doctest +20 -0
  238. {nltk-3.2 → nltk-3.3}/nltk/test/collocations.doctest +276 -276
  239. {nltk-3.2 → nltk-3.3}/nltk/test/compat.doctest +134 -138
  240. {nltk-3.2 → nltk-3.3}/nltk/test/compat_fixt.py +9 -8
  241. nltk-3.3/nltk/test/concordance.doctest +55 -0
  242. {nltk-3.2 → nltk-3.3}/nltk/test/corpus.doctest +2200 -2212
  243. {nltk-3.2 → nltk-3.3}/nltk/test/corpus_fixt.py +3 -3
  244. {nltk-3.2 → nltk-3.3}/nltk/test/crubadan.doctest +65 -65
  245. {nltk-3.2 → nltk-3.3}/nltk/test/data.doctest +379 -379
  246. {nltk-3.2 → nltk-3.3}/nltk/test/dependency.doctest +241 -241
  247. {nltk-3.2 → nltk-3.3}/nltk/test/discourse.doctest +546 -546
  248. {nltk-3.2 → nltk-3.3}/nltk/test/discourse_fixt.py +14 -13
  249. {nltk-3.2 → nltk-3.3}/nltk/test/doctest_nose_plugin.py +159 -156
  250. {nltk-3.2 → nltk-3.3}/nltk/test/drt.doctest +517 -517
  251. {nltk-3.2 → nltk-3.3}/nltk/test/featgram.doctest +607 -607
  252. {nltk-3.2 → nltk-3.3}/nltk/test/featstruct.doctest +1229 -1230
  253. {nltk-3.2 → nltk-3.3}/nltk/test/framenet.doctest +288 -239
  254. {nltk-3.2 → nltk-3.3}/nltk/test/generate.doctest +67 -67
  255. {nltk-3.2 → nltk-3.3}/nltk/test/gensim.doctest +140 -141
  256. {nltk-3.2 → nltk-3.3}/nltk/test/gensim_fixt.py +10 -9
  257. {nltk-3.2 → nltk-3.3}/nltk/test/gluesemantics.doctest +384 -386
  258. {nltk-3.2 → nltk-3.3}/nltk/test/gluesemantics_malt.doctest +68 -68
  259. {nltk-3.2 → nltk-3.3}/nltk/test/gluesemantics_malt_fixt.py +12 -11
  260. {nltk-3.2 → nltk-3.3}/nltk/test/grammar.doctest +48 -48
  261. {nltk-3.2 → nltk-3.3}/nltk/test/grammartestsuites.doctest +109 -109
  262. {nltk-3.2 → nltk-3.3}/nltk/test/index.doctest +100 -100
  263. {nltk-3.2 → nltk-3.3}/nltk/test/inference.doctest +534 -534
  264. {nltk-3.2 → nltk-3.3}/nltk/test/inference_fixt.py +12 -11
  265. {nltk-3.2 → nltk-3.3}/nltk/test/internals.doctest +140 -140
  266. {nltk-3.2 → nltk-3.3}/nltk/test/japanese.doctest +48 -48
  267. {nltk-3.2 → nltk-3.3}/nltk/test/logic.doctest +1098 -1098
  268. {nltk-3.2 → nltk-3.3}/nltk/test/metrics.doctest +270 -270
  269. {nltk-3.2 → nltk-3.3}/nltk/test/misc.doctest +118 -118
  270. {nltk-3.2 → nltk-3.3}/nltk/test/nonmonotonic.doctest +286 -286
  271. {nltk-3.2 → nltk-3.3}/nltk/test/nonmonotonic_fixt.py +12 -11
  272. {nltk-3.2 → nltk-3.3}/nltk/test/paice.doctest +35 -35
  273. {nltk-3.2 → nltk-3.3}/nltk/test/parse.doctest +884 -884
  274. {nltk-3.2 → nltk-3.3}/nltk/test/portuguese_en.doctest +565 -565
  275. {nltk-3.2 → nltk-3.3}/nltk/test/portuguese_en_fixt.py +16 -16
  276. {nltk-3.2 → nltk-3.3}/nltk/test/probability.doctest +304 -304
  277. {nltk-3.2 → nltk-3.3}/nltk/test/probability_fixt.py +13 -12
  278. {nltk-3.2 → nltk-3.3}/nltk/test/propbank.doctest +176 -176
  279. {nltk-3.2 → nltk-3.3}/nltk/test/relextract.doctest +263 -263
  280. {nltk-3.2 → nltk-3.3}/nltk/test/resolution.doctest +221 -221
  281. {nltk-3.2 → nltk-3.3}/nltk/test/runtests.py +86 -64
  282. {nltk-3.2 → nltk-3.3}/nltk/test/segmentation_fixt.py +10 -10
  283. {nltk-3.2 → nltk-3.3}/nltk/test/semantics.doctest +665 -665
  284. {nltk-3.2 → nltk-3.3}/nltk/test/semantics_fixt.py +7 -7
  285. {nltk-3.2 → nltk-3.3}/nltk/test/sentiment.doctest +233 -233
  286. {nltk-3.2 → nltk-3.3}/nltk/test/sentiwordnet.doctest +41 -41
  287. {nltk-3.2 → nltk-3.3}/nltk/test/simple.doctest +85 -85
  288. {nltk-3.2 → nltk-3.3}/nltk/test/stem.doctest +78 -78
  289. {nltk-3.2 → nltk-3.3}/nltk/test/tag.doctest +33 -33
  290. {nltk-3.2 → nltk-3.3}/nltk/test/tokenize.doctest +273 -166
  291. {nltk-3.2 → nltk-3.3}/nltk/test/toolbox.doctest +307 -307
  292. {nltk-3.2 → nltk-3.3}/nltk/test/translate.doctest +242 -242
  293. {nltk-3.2 → nltk-3.3}/nltk/test/translate_fixt.py +3 -3
  294. {nltk-3.2 → nltk-3.3}/nltk/test/tree.doctest +1101 -1101
  295. {nltk-3.2 → nltk-3.3}/nltk/test/treeprettyprinter.doctest +127 -127
  296. {nltk-3.2 → nltk-3.3}/nltk/test/treetransforms.doctest +156 -156
  297. nltk-3.3/nltk/test/unit/test_2x_compat.py +63 -0
  298. nltk-3.3/nltk/test/unit/test_aline.py +52 -0
  299. nltk-3.3/nltk/test/unit/test_chunk.py +56 -0
  300. {nltk-3.2 → nltk-3.3}/nltk/test/unit/test_classify.py +55 -53
  301. {nltk-3.2 → nltk-3.3}/nltk/test/unit/test_collocations.py +88 -79
  302. nltk-3.3/nltk/test/unit/test_concordance.py +98 -0
  303. nltk-3.3/nltk/test/unit/test_corenlp.py +1000 -0
  304. {nltk-3.2 → nltk-3.3}/nltk/test/unit/test_corpora.py +200 -183
  305. {nltk-3.2 → nltk-3.3}/nltk/test/unit/test_corpus_views.py +45 -45
  306. {nltk-3.2 → nltk-3.3}/nltk/test/unit/test_hmm.py +88 -88
  307. {nltk-3.2 → nltk-3.3}/nltk/test/unit/test_json2csv_corpus.py +188 -187
  308. {nltk-3.2 → nltk-3.3}/nltk/test/unit/test_naivebayes.py +26 -26
  309. nltk-3.3/nltk/test/unit/test_rte_classify.py +86 -0
  310. {nltk-3.2 → nltk-3.3}/nltk/test/unit/test_seekable_unicode_stream_reader.py +124 -124
  311. nltk-3.3/nltk/test/unit/test_senna.py +74 -0
  312. nltk-3.3/nltk/test/unit/test_stem.py +121 -0
  313. {nltk-3.2 → nltk-3.3}/nltk/test/unit/test_tag.py +20 -20
  314. {nltk-3.2 → nltk-3.3}/nltk/test/unit/test_tgrep.py +628 -626
  315. nltk-3.3/nltk/test/unit/test_tokenize.py +168 -0
  316. {nltk-3.2 → nltk-3.3}/nltk/test/unit/test_twitter_auth.py +185 -185
  317. nltk-3.3/nltk/test/unit/test_wordnet.py +134 -0
  318. nltk-3.3/nltk/test/unit/translate/test_bleu.py +244 -0
  319. nltk-3.3/nltk/test/unit/translate/test_gdfa.py +57 -0
  320. {nltk-3.2 → nltk-3.3}/nltk/test/unit/translate/test_ibm1.py +75 -75
  321. {nltk-3.2 → nltk-3.3}/nltk/test/unit/translate/test_ibm2.py +87 -87
  322. {nltk-3.2 → nltk-3.3}/nltk/test/unit/translate/test_ibm3.py +105 -105
  323. {nltk-3.2 → nltk-3.3}/nltk/test/unit/translate/test_ibm4.py +126 -126
  324. {nltk-3.2 → nltk-3.3}/nltk/test/unit/translate/test_ibm5.py +166 -166
  325. {nltk-3.2 → nltk-3.3}/nltk/test/unit/translate/test_ibm_model.py +270 -270
  326. nltk-3.3/nltk/test/unit/translate/test_nist.py +37 -0
  327. {nltk-3.2 → nltk-3.3}/nltk/test/unit/translate/test_stack_decoder.py +300 -300
  328. {nltk-3.2 → nltk-3.3}/nltk/test/unit/utils.py +41 -41
  329. {nltk-3.2 → nltk-3.3}/nltk/test/util.doctest +48 -48
  330. {nltk-3.2 → nltk-3.3}/nltk/test/wordnet.doctest +595 -581
  331. {nltk-3.2 → nltk-3.3}/nltk/test/wordnet_fixt.py +7 -6
  332. {nltk-3.2 → nltk-3.3}/nltk/test/wordnet_lch.doctest +53 -53
  333. {nltk-3.2 → nltk-3.3}/nltk/test/wsd.doctest +68 -68
  334. {nltk-3.2 → nltk-3.3}/nltk/text.py +663 -610
  335. {nltk-3.2 → nltk-3.3}/nltk/tgrep.py +938 -937
  336. {nltk-3.2 → nltk-3.3}/nltk/tokenize/__init__.py +130 -107
  337. {nltk-3.2 → nltk-3.3}/nltk/tokenize/api.py +77 -78
  338. {nltk-3.2 → nltk-3.3}/nltk/tokenize/casual.py +343 -342
  339. {nltk-3.2 → nltk-3.3}/nltk/tokenize/mwe.py +120 -120
  340. nltk-3.3/nltk/tokenize/nist.py +173 -0
  341. {nltk-3.2 → nltk-3.3}/nltk/tokenize/punkt.py +1620 -1601
  342. {nltk-3.2 → nltk-3.3}/nltk/tokenize/regexp.py +208 -202
  343. nltk-3.3/nltk/tokenize/repp.py +151 -0
  344. {nltk-3.2 → nltk-3.3}/nltk/tokenize/sexpr.py +140 -142
  345. {nltk-3.2 → nltk-3.3}/nltk/tokenize/simple.py +138 -136
  346. {nltk-3.2 → nltk-3.3}/nltk/tokenize/stanford.py +117 -113
  347. nltk-3.3/nltk/tokenize/stanford_segmenter.py +246 -0
  348. {nltk-3.2 → nltk-3.3}/nltk/tokenize/texttiling.py +458 -460
  349. nltk-3.3/nltk/tokenize/toktok.py +155 -0
  350. nltk-3.3/nltk/tokenize/treebank.py +363 -0
  351. nltk-3.3/nltk/tokenize/util.py +261 -0
  352. {nltk-3.2 → nltk-3.3}/nltk/toolbox.py +499 -497
  353. {nltk-3.2 → nltk-3.3}/nltk/translate/__init__.py +24 -24
  354. {nltk-3.2 → nltk-3.3}/nltk/translate/api.py +331 -321
  355. {nltk-3.2 → nltk-3.3}/nltk/translate/bleu_score.py +615 -556
  356. nltk-3.3/nltk/translate/chrf_score.py +137 -0
  357. {nltk-3.2 → nltk-3.3}/nltk/translate/gale_church.py +248 -235
  358. {nltk-3.2 → nltk-3.3}/nltk/translate/gdfa.py +135 -131
  359. nltk-3.3/nltk/translate/gleu_score.py +193 -0
  360. {nltk-3.2 → nltk-3.3}/nltk/translate/ibm1.py +250 -241
  361. {nltk-3.2 → nltk-3.3}/nltk/translate/ibm2.py +308 -308
  362. {nltk-3.2 → nltk-3.3}/nltk/translate/ibm3.py +337 -337
  363. {nltk-3.2 → nltk-3.3}/nltk/translate/ibm4.py +467 -467
  364. {nltk-3.2 → nltk-3.3}/nltk/translate/ibm5.py +639 -639
  365. {nltk-3.2 → nltk-3.3}/nltk/translate/ibm_model.py +536 -533
  366. {nltk-3.2 → nltk-3.3}/nltk/translate/metrics.py +40 -40
  367. nltk-3.3/nltk/translate/nist_score.py +178 -0
  368. {nltk-3.2 → nltk-3.3}/nltk/translate/phrase_based.py +175 -175
  369. {nltk-3.2 → nltk-3.3}/nltk/translate/ribes_score.py +325 -325
  370. {nltk-3.2 → nltk-3.3}/nltk/translate/stack_decoder.py +499 -495
  371. {nltk-3.2 → nltk-3.3}/nltk/tree.py +1605 -1603
  372. {nltk-3.2 → nltk-3.3}/nltk/treeprettyprinter.py +564 -564
  373. {nltk-3.2 → nltk-3.3}/nltk/treetransforms.py +309 -309
  374. {nltk-3.2 → nltk-3.3}/nltk/twitter/__init__.py +28 -28
  375. {nltk-3.2 → nltk-3.3}/nltk/twitter/api.py +141 -136
  376. {nltk-3.2 → nltk-3.3}/nltk/twitter/common.py +264 -258
  377. {nltk-3.2 → nltk-3.3}/nltk/twitter/twitter_demo.py +288 -288
  378. {nltk-3.2 → nltk-3.3}/nltk/twitter/twitterclient.py +530 -530
  379. {nltk-3.2 → nltk-3.3}/nltk/twitter/util.py +147 -146
  380. nltk-3.3/nltk/util.py +751 -0
  381. {nltk-3.2 → nltk-3.3}/nltk/wsd.py +53 -53
  382. {nltk-3.2 → nltk-3.3/nltk.egg-info}/PKG-INFO +13 -5
  383. {nltk-3.2 → nltk-3.3}/nltk.egg-info/SOURCES.txt +26 -3
  384. nltk-3.3/nltk.egg-info/requires.txt +31 -0
  385. {nltk-3.2 → nltk-3.3}/setup.cfg +0 -1
  386. {nltk-3.2 → nltk-3.3}/setup.py +102 -75
  387. nltk-3.2/nltk/VERSION +0 -1
  388. nltk-3.2/nltk/compat.py +0 -680
  389. nltk-3.2/nltk/corpus/reader/pl196x.py +0 -278
  390. nltk-3.2/nltk/corpus/reader/verbnet.py +0 -408
  391. nltk-3.2/nltk/corpus/reader/wordlist.py +0 -37
  392. nltk-3.2/nltk/sentiment/vader_lexicon.txt +0 -7517
  393. nltk-3.2/nltk/six.py +0 -868
  394. nltk-3.2/nltk/stem/porter.py +0 -692
  395. nltk-3.2/nltk/test/unit/test_2x_compat.py +0 -26
  396. nltk-3.2/nltk/test/unit/test_stem.py +0 -36
  397. nltk-3.2/nltk/test/unit/test_tokenize.py +0 -23
  398. nltk-3.2/nltk/test/unit/translate/test_bleu.py +0 -84
  399. nltk-3.2/nltk/tokenize/treebank.py +0 -124
  400. nltk-3.2/nltk/tokenize/util.py +0 -90
  401. nltk-3.2/nltk/util.py +0 -1364
  402. {nltk-3.2 → nltk-3.3}/nltk/test/unit/__init__.py +0 -0
  403. {nltk-3.2 → nltk-3.3}/nltk/test/unit/translate/__init__.py +0 -0
  404. {nltk-3.2 → nltk-3.3}/nltk.egg-info/dependency_links.txt +0 -0
  405. {nltk-3.2 → nltk-3.3}/nltk.egg-info/not-zip-safe +0 -0
  406. {nltk-3.2 → nltk-3.3}/nltk.egg-info/top_level.txt +0 -0
@@ -1,521 +1,528 @@
1
- # Natural Language Toolkit: CONLL Corpus Reader
2
- #
3
- # Copyright (C) 2001-2015 NLTK Project
4
- # Author: Steven Bird <stevenbird1@gmail.com>
5
- # Edward Loper <edloper@gmail.com>
6
- # URL: <http://nltk.org/>
7
- # For license information, see LICENSE.TXT
8
-
9
- """
10
- Read CoNLL-style chunk fileids.
11
- """
12
-
13
- from __future__ import unicode_literals
14
-
15
- import os
16
- import codecs
17
- import textwrap
18
-
19
- from nltk import compat
20
- from nltk.tree import Tree
21
- from nltk.util import LazyMap, LazyConcatenation
22
- from nltk.tag import map_tag
23
-
24
- from nltk.corpus.reader.util import *
25
- from nltk.corpus.reader.api import *
26
-
27
- class ConllCorpusReader(CorpusReader):
28
- """
29
- A corpus reader for CoNLL-style files. These files consist of a
30
- series of sentences, separated by blank lines. Each sentence is
31
- encoded using a table (or "grid") of values, where each line
32
- corresponds to a single word, and each column corresponds to an
33
- annotation type. The set of columns used by CoNLL-style files can
34
- vary from corpus to corpus; the ``ConllCorpusReader`` constructor
35
- therefore takes an argument, ``columntypes``, which is used to
36
- specify the columns that are used by a given corpus.
37
-
38
- @todo: Add support for reading from corpora where different
39
- parallel files contain different columns.
40
- @todo: Possibly add caching of the grid corpus view? This would
41
- allow the same grid view to be used by different data access
42
- methods (eg words() and parsed_sents() could both share the
43
- same grid corpus view object).
44
- @todo: Better support for -DOCSTART-. Currently, we just ignore
45
- it, but it could be used to define methods that retrieve a
46
- document at a time (eg parsed_documents()).
47
- """
48
-
49
- #/////////////////////////////////////////////////////////////////
50
- # Column Types
51
- #/////////////////////////////////////////////////////////////////
52
-
53
- WORDS = 'words' #: column type for words
54
- POS = 'pos' #: column type for part-of-speech tags
55
- TREE = 'tree' #: column type for parse trees
56
- CHUNK = 'chunk' #: column type for chunk structures
57
- NE = 'ne' #: column type for named entities
58
- SRL = 'srl' #: column type for semantic role labels
59
- IGNORE = 'ignore' #: column type for column that should be ignored
60
-
61
- #: A list of all column types supported by the conll corpus reader.
62
- COLUMN_TYPES = (WORDS, POS, TREE, CHUNK, NE, SRL, IGNORE)
63
-
64
- #/////////////////////////////////////////////////////////////////
65
- # Constructor
66
- #/////////////////////////////////////////////////////////////////
67
-
68
- def __init__(self, root, fileids, columntypes,
69
- chunk_types=None, root_label='S', pos_in_tree=False,
70
- srl_includes_roleset=True, encoding='utf8',
71
- tree_class=Tree, tagset=None):
72
- for columntype in columntypes:
73
- if columntype not in self.COLUMN_TYPES:
74
- raise ValueError('Bad column type %r' % columntype)
75
- if isinstance(chunk_types, compat.string_types):
76
- chunk_types = [chunk_types]
77
- self._chunk_types = chunk_types
78
- self._colmap = dict((c,i) for (i,c) in enumerate(columntypes))
79
- self._pos_in_tree = pos_in_tree
80
- self._root_label = root_label # for chunks
81
- self._srl_includes_roleset = srl_includes_roleset
82
- self._tree_class = tree_class
83
- CorpusReader.__init__(self, root, fileids, encoding)
84
- self._tagset = tagset
85
-
86
- #/////////////////////////////////////////////////////////////////
87
- # Data Access Methods
88
- #/////////////////////////////////////////////////////////////////
89
-
90
- def raw(self, fileids=None):
91
- if fileids is None: fileids = self._fileids
92
- elif isinstance(fileids, compat.string_types): fileids = [fileids]
93
- return concat([self.open(f).read() for f in fileids])
94
-
95
- def words(self, fileids=None):
96
- self._require(self.WORDS)
97
- return LazyConcatenation(LazyMap(self._get_words, self._grids(fileids)))
98
-
99
- def sents(self, fileids=None):
100
- self._require(self.WORDS)
101
- return LazyMap(self._get_words, self._grids(fileids))
102
-
103
- def tagged_words(self, fileids=None, tagset=None):
104
- self._require(self.WORDS, self.POS)
105
- def get_tagged_words(grid):
106
- return self._get_tagged_words(grid, tagset)
107
- return LazyConcatenation(LazyMap(get_tagged_words,
108
- self._grids(fileids)))
109
-
110
- def tagged_sents(self, fileids=None, tagset=None):
111
- self._require(self.WORDS, self.POS)
112
- def get_tagged_words(grid):
113
- return self._get_tagged_words(grid, tagset)
114
- return LazyMap(get_tagged_words, self._grids(fileids))
115
-
116
- def chunked_words(self, fileids=None, chunk_types=None,
117
- tagset=None):
118
- self._require(self.WORDS, self.POS, self.CHUNK)
119
- if chunk_types is None: chunk_types = self._chunk_types
120
- def get_chunked_words(grid): # capture chunk_types as local var
121
- return self._get_chunked_words(grid, chunk_types, tagset)
122
- return LazyConcatenation(LazyMap(get_chunked_words,
123
- self._grids(fileids)))
124
-
125
- def chunked_sents(self, fileids=None, chunk_types=None,
126
- tagset=None):
127
- self._require(self.WORDS, self.POS, self.CHUNK)
128
- if chunk_types is None: chunk_types = self._chunk_types
129
- def get_chunked_words(grid): # capture chunk_types as local var
130
- return self._get_chunked_words(grid, chunk_types, tagset)
131
- return LazyMap(get_chunked_words, self._grids(fileids))
132
-
133
- def parsed_sents(self, fileids=None, pos_in_tree=None, tagset=None):
134
- self._require(self.WORDS, self.POS, self.TREE)
135
- if pos_in_tree is None: pos_in_tree = self._pos_in_tree
136
- def get_parsed_sent(grid): # capture pos_in_tree as local var
137
- return self._get_parsed_sent(grid, pos_in_tree, tagset)
138
- return LazyMap(get_parsed_sent, self._grids(fileids))
139
-
140
- def srl_spans(self, fileids=None):
141
- self._require(self.SRL)
142
- return LazyMap(self._get_srl_spans, self._grids(fileids))
143
-
144
- def srl_instances(self, fileids=None, pos_in_tree=None, flatten=True):
145
- self._require(self.WORDS, self.POS, self.TREE, self.SRL)
146
- if pos_in_tree is None: pos_in_tree = self._pos_in_tree
147
- def get_srl_instances(grid): # capture pos_in_tree as local var
148
- return self._get_srl_instances(grid, pos_in_tree)
149
- result = LazyMap(get_srl_instances, self._grids(fileids))
150
- if flatten: result = LazyConcatenation(result)
151
- return result
152
-
153
- def iob_words(self, fileids=None, tagset=None):
154
- """
155
- :return: a list of word/tag/IOB tuples
156
- :rtype: list(tuple)
157
- :param fileids: the list of fileids that make up this corpus
158
- :type fileids: None or str or list
159
- """
160
- self._require(self.WORDS, self.POS, self.CHUNK)
161
- def get_iob_words(grid):
162
- return self._get_iob_words(grid, tagset)
163
- return LazyConcatenation(LazyMap(get_iob_words, self._grids(fileids)))
164
-
165
- def iob_sents(self, fileids=None, tagset=None):
166
- """
167
- :return: a list of lists of word/tag/IOB tuples
168
- :rtype: list(list)
169
- :param fileids: the list of fileids that make up this corpus
170
- :type fileids: None or str or list
171
- """
172
- self._require(self.WORDS, self.POS, self.CHUNK)
173
- def get_iob_words(grid):
174
- return self._get_iob_words(grid, tagset)
175
- return LazyMap(get_iob_words, self._grids(fileids))
176
-
177
- #/////////////////////////////////////////////////////////////////
178
- # Grid Reading
179
- #/////////////////////////////////////////////////////////////////
180
-
181
- def _grids(self, fileids=None):
182
- # n.b.: we could cache the object returned here (keyed on
183
- # fileids), which would let us reuse the same corpus view for
184
- # different things (eg srl and parse trees).
185
- return concat([StreamBackedCorpusView(fileid, self._read_grid_block,
186
- encoding=enc)
187
- for (fileid, enc) in self.abspaths(fileids, True)])
188
-
189
- def _read_grid_block(self, stream):
190
- grids = []
191
- for block in read_blankline_block(stream):
192
- block = block.strip()
193
- if not block: continue
194
-
195
- grid = [line.split() for line in block.split('\n')]
196
-
197
- # If there's a docstart row, then discard. ([xx] eventually it
198
- # would be good to actually use it)
199
- if grid[0][self._colmap.get('words', 0)] == '-DOCSTART-':
200
- del grid[0]
201
-
202
- # Check that the grid is consistent.
203
- for row in grid:
204
- if len(row) != len(grid[0]):
205
- raise ValueError('Inconsistent number of columns:\n%s'
206
- % block)
207
- grids.append(grid)
208
- return grids
209
-
210
- #/////////////////////////////////////////////////////////////////
211
- # Transforms
212
- #/////////////////////////////////////////////////////////////////
213
- # given a grid, transform it into some representation (e.g.,
214
- # a list of words or a parse tree).
215
-
216
- def _get_words(self, grid):
217
- return self._get_column(grid, self._colmap['words'])
218
-
219
- def _get_tagged_words(self, grid, tagset=None):
220
- pos_tags = self._get_column(grid, self._colmap['pos'])
221
- if tagset and tagset != self._tagset:
222
- pos_tags = [map_tag(self._tagset, tagset, t) for t in pos_tags]
223
- return list(zip(self._get_column(grid, self._colmap['words']), pos_tags))
224
-
225
- def _get_iob_words(self, grid, tagset=None):
226
- pos_tags = self._get_column(grid, self._colmap['pos'])
227
- if tagset and tagset != self._tagset:
228
- pos_tags = [map_tag(self._tagset, tagset, t) for t in pos_tags]
229
- return list(zip(self._get_column(grid, self._colmap['words']), pos_tags,
230
- self._get_column(grid, self._colmap['chunk'])))
231
-
232
- def _get_chunked_words(self, grid, chunk_types, tagset=None):
233
- # n.b.: this method is very similar to conllstr2tree.
234
- words = self._get_column(grid, self._colmap['words'])
235
- pos_tags = self._get_column(grid, self._colmap['pos'])
236
- if tagset and tagset != self._tagset:
237
- pos_tags = [map_tag(self._tagset, tagset, t) for t in pos_tags]
238
- chunk_tags = self._get_column(grid, self._colmap['chunk'])
239
-
240
- stack = [Tree(self._root_label, [])]
241
-
242
- for (word, pos_tag, chunk_tag) in zip(words, pos_tags, chunk_tags):
243
- if chunk_tag == 'O':
244
- state, chunk_type = 'O', ''
245
- else:
246
- (state, chunk_type) = chunk_tag.split('-')
247
- # If it's a chunk we don't care about, treat it as O.
248
- if chunk_types is not None and chunk_type not in chunk_types:
249
- state = 'O'
250
- # Treat a mismatching I like a B.
251
- if state == 'I' and chunk_type != stack[-1].label():
252
- state = 'B'
253
- # For B or I: close any open chunks
254
- if state in 'BO' and len(stack) == 2:
255
- stack.pop()
256
- # For B: start a new chunk.
257
- if state == 'B':
258
- new_chunk = Tree(chunk_type, [])
259
- stack[-1].append(new_chunk)
260
- stack.append(new_chunk)
261
- # Add the word token.
262
- stack[-1].append((word, pos_tag))
263
-
264
- return stack[0]
265
-
266
- def _get_parsed_sent(self, grid, pos_in_tree, tagset=None):
267
- words = self._get_column(grid, self._colmap['words'])
268
- pos_tags = self._get_column(grid, self._colmap['pos'])
269
- if tagset and tagset != self._tagset:
270
- pos_tags = [map_tag(self._tagset, tagset, t) for t in pos_tags]
271
- parse_tags = self._get_column(grid, self._colmap['tree'])
272
-
273
- treestr = ''
274
- for (word, pos_tag, parse_tag) in zip(words, pos_tags, parse_tags):
275
- if word == '(': word = '-LRB-'
276
- if word == ')': word = '-RRB-'
277
- if pos_tag == '(': pos_tag = '-LRB-'
278
- if pos_tag == ')': pos_tag = '-RRB-'
279
- (left, right) = parse_tag.split('*')
280
- right = right.count(')')*')' # only keep ')'.
281
- treestr += '%s (%s %s) %s' % (left, pos_tag, word, right)
282
- try:
283
- tree = self._tree_class.parse(treestr)
284
- except (ValueError, IndexError):
285
- tree = self._tree_class.parse('(%s %s)' %
286
- (self._root_label, treestr))
287
-
288
- if not pos_in_tree:
289
- for subtree in tree.subtrees():
290
- for i, child in enumerate(subtree):
291
- if (isinstance(child, Tree) and len(child)==1 and
292
- isinstance(child[0], compat.string_types)):
293
- subtree[i] = (child[0], child.label())
294
-
295
- return tree
296
-
297
- def _get_srl_spans(self, grid):
298
- """
299
- list of list of (start, end), tag) tuples
300
- """
301
- if self._srl_includes_roleset:
302
- predicates = self._get_column(grid, self._colmap['srl']+1)
303
- start_col = self._colmap['srl']+2
304
- else:
305
- predicates = self._get_column(grid, self._colmap['srl'])
306
- start_col = self._colmap['srl']+1
307
-
308
- # Count how many predicates there are. This tells us how many
309
- # columns to expect for SRL data.
310
- num_preds = len([p for p in predicates if p != '-'])
311
-
312
- spanlists = []
313
- for i in range(num_preds):
314
- col = self._get_column(grid, start_col+i)
315
- spanlist = []
316
- stack = []
317
- for wordnum, srl_tag in enumerate(col):
318
- (left, right) = srl_tag.split('*')
319
- for tag in left.split('('):
320
- if tag:
321
- stack.append((tag, wordnum))
322
- for i in range(right.count(')')):
323
- (tag, start) = stack.pop()
324
- spanlist.append( ((start, wordnum+1), tag) )
325
- spanlists.append(spanlist)
326
-
327
- return spanlists
328
-
329
- def _get_srl_instances(self, grid, pos_in_tree):
330
- tree = self._get_parsed_sent(grid, pos_in_tree)
331
- spanlists = self._get_srl_spans(grid)
332
- if self._srl_includes_roleset:
333
- predicates = self._get_column(grid, self._colmap['srl']+1)
334
- rolesets = self._get_column(grid, self._colmap['srl'])
335
- else:
336
- predicates = self._get_column(grid, self._colmap['srl'])
337
- rolesets = [None] * len(predicates)
338
-
339
- instances = ConllSRLInstanceList(tree)
340
- for wordnum, predicate in enumerate(predicates):
341
- if predicate == '-': continue
342
- # Decide which spanlist to use. Don't assume that they're
343
- # sorted in the same order as the predicates (even though
344
- # they usually are).
345
- for spanlist in spanlists:
346
- for (start, end), tag in spanlist:
347
- if wordnum in range(start,end) and tag in ('V', 'C-V'):
348
- break
349
- else: continue
350
- break
351
- else:
352
- raise ValueError('No srl column found for %r' % predicate)
353
- instances.append(ConllSRLInstance(tree, wordnum, predicate,
354
- rolesets[wordnum], spanlist))
355
-
356
- return instances
357
-
358
- #/////////////////////////////////////////////////////////////////
359
- # Helper Methods
360
- #/////////////////////////////////////////////////////////////////
361
-
362
- def _require(self, *columntypes):
363
- for columntype in columntypes:
364
- if columntype not in self._colmap:
365
- raise ValueError('This corpus does not contain a %s '
366
- 'column.' % columntype)
367
-
368
- @staticmethod
369
- def _get_column(grid, column_index):
370
- return [grid[i][column_index] for i in range(len(grid))]
371
-
372
-
373
- @compat.python_2_unicode_compatible
374
- class ConllSRLInstance(object):
375
- """
376
- An SRL instance from a CoNLL corpus, which identifies and
377
- providing labels for the arguments of a single verb.
378
- """
379
- # [xx] add inst.core_arguments, inst.argm_arguments?
380
-
381
- def __init__(self, tree, verb_head, verb_stem, roleset, tagged_spans):
382
- self.verb = []
383
- """A list of the word indices of the words that compose the
384
- verb whose arguments are identified by this instance.
385
- This will contain multiple word indices when multi-word
386
- verbs are used (e.g. 'turn on')."""
387
-
388
- self.verb_head = verb_head
389
- """The word index of the head word of the verb whose arguments
390
- are identified by this instance. E.g., for a sentence that
391
- uses the verb 'turn on,' ``verb_head`` will be the word index
392
- of the word 'turn'."""
393
-
394
- self.verb_stem = verb_stem
395
-
396
- self.roleset = roleset
397
-
398
- self.arguments = []
399
- """A list of ``(argspan, argid)`` tuples, specifying the location
400
- and type for each of the arguments identified by this
401
- instance. ``argspan`` is a tuple ``start, end``, indicating
402
- that the argument consists of the ``words[start:end]``."""
403
-
404
- self.tagged_spans = tagged_spans
405
- """A list of ``(span, id)`` tuples, specifying the location and
406
- type for each of the arguments, as well as the verb pieces,
407
- that make up this instance."""
408
-
409
- self.tree = tree
410
- """The parse tree for the sentence containing this instance."""
411
-
412
- self.words = tree.leaves()
413
- """A list of the words in the sentence containing this
414
- instance."""
415
-
416
- # Fill in the self.verb and self.arguments values.
417
- for (start, end), tag in tagged_spans:
418
- if tag in ('V', 'C-V'):
419
- self.verb += list(range(start, end))
420
- else:
421
- self.arguments.append( ((start, end), tag) )
422
-
423
- def __repr__(self):
424
- plural = len(self.arguments)!=1 and 's' or ''
425
- return '<ConllSRLInstance for %r with %d argument%s>' % (
426
- (self.verb_stem, len(self.arguments), plural))
427
-
428
- def pprint(self):
429
- verbstr = ' '.join(self.words[i][0] for i in self.verb)
430
- hdr = 'SRL for %r (stem=%r):\n' % (verbstr, self.verb_stem)
431
- s = ''
432
- for i, word in enumerate(self.words):
433
- if isinstance(word, tuple): word = word[0]
434
- for (start, end), argid in self.arguments:
435
- if i == start: s += '[%s ' % argid
436
- if i == end: s += '] '
437
- if i in self.verb: word = '<<%s>>' % word
438
- s += word + ' '
439
- return hdr + textwrap.fill(s.replace(' ]', ']'),
440
- initial_indent=' ',
441
- subsequent_indent=' ')
442
-
443
- @compat.python_2_unicode_compatible
444
- class ConllSRLInstanceList(list):
445
- """
446
- Set of instances for a single sentence
447
- """
448
- def __init__(self, tree, instances=()):
449
- self.tree = tree
450
- list.__init__(self, instances)
451
-
452
- def __str__(self):
453
- return self.pprint()
454
-
455
- def pprint(self, include_tree=False):
456
- # Sanity check: trees should be the same
457
- for inst in self:
458
- if inst.tree != self.tree:
459
- raise ValueError('Tree mismatch!')
460
-
461
- # If desired, add trees:
462
- if include_tree:
463
- words = self.tree.leaves()
464
- pos = [None] * len(words)
465
- synt = ['*'] * len(words)
466
- self._tree2conll(self.tree, 0, words, pos, synt)
467
-
468
- s = ''
469
- for i in range(len(words)):
470
- # optional tree columns
471
- if include_tree:
472
- s += '%-20s ' % words[i]
473
- s += '%-8s ' % pos[i]
474
- s += '%15s*%-8s ' % tuple(synt[i].split('*'))
475
-
476
- # verb head column
477
- for inst in self:
478
- if i == inst.verb_head:
479
- s += '%-20s ' % inst.verb_stem
480
- break
481
- else:
482
- s += '%-20s ' % '-'
483
- # Remaining columns: self
484
- for inst in self:
485
- argstr = '*'
486
- for (start, end), argid in inst.tagged_spans:
487
- if i==start: argstr = '(%s%s' % (argid, argstr)
488
- if i==(end-1): argstr += ')'
489
- s += '%-12s ' % argstr
490
- s += '\n'
491
- return s
492
-
493
- def _tree2conll(self, tree, wordnum, words, pos, synt):
494
- assert isinstance(tree, Tree)
495
- if len(tree) == 1 and isinstance(tree[0], compat.string_types):
496
- pos[wordnum] = tree.label()
497
- assert words[wordnum] == tree[0]
498
- return wordnum+1
499
- elif len(tree) == 1 and isinstance(tree[0], tuple):
500
- assert len(tree[0]) == 2
501
- pos[wordnum], pos[wordnum] = tree[0]
502
- return wordnum+1
503
- else:
504
- synt[wordnum] = '(%s%s' % (tree.label(), synt[wordnum])
505
- for child in tree:
506
- wordnum = self._tree2conll(child, wordnum, words,
507
- pos, synt)
508
- synt[wordnum-1] += ')'
509
- return wordnum
510
-
511
- class ConllChunkCorpusReader(ConllCorpusReader):
512
- """
513
- A ConllCorpusReader whose data file contains three columns: words,
514
- pos, and chunk.
515
- """
516
- def __init__(self, root, fileids, chunk_types, encoding='utf8',
517
- tagset=None):
518
- ConllCorpusReader.__init__(
519
- self, root, fileids, ('words', 'pos', 'chunk'),
520
- chunk_types=chunk_types, encoding=encoding,
521
- tagset=tagset)
1
+ # Natural Language Toolkit: CONLL Corpus Reader
2
+ #
3
+ # Copyright (C) 2001-2018 NLTK Project
4
+ # Author: Steven Bird <stevenbird1@gmail.com>
5
+ # Edward Loper <edloper@gmail.com>
6
+ # URL: <http://nltk.org/>
7
+ # For license information, see LICENSE.TXT
8
+
9
+ """
10
+ Read CoNLL-style chunk fileids.
11
+ """
12
+
13
+ from __future__ import unicode_literals
14
+
15
+ import os
16
+ import codecs
17
+ import textwrap
18
+
19
+ from six import string_types
20
+
21
+ from nltk import compat
22
+ from nltk.tree import Tree
23
+ from nltk.util import LazyMap, LazyConcatenation
24
+ from nltk.tag import map_tag
25
+
26
+ from nltk.corpus.reader.util import *
27
+ from nltk.corpus.reader.api import *
28
+
29
+ class ConllCorpusReader(CorpusReader):
30
+ """
31
+ A corpus reader for CoNLL-style files. These files consist of a
32
+ series of sentences, separated by blank lines. Each sentence is
33
+ encoded using a table (or "grid") of values, where each line
34
+ corresponds to a single word, and each column corresponds to an
35
+ annotation type. The set of columns used by CoNLL-style files can
36
+ vary from corpus to corpus; the ``ConllCorpusReader`` constructor
37
+ therefore takes an argument, ``columntypes``, which is used to
38
+ specify the columns that are used by a given corpus. By default
39
+ columns are split by consecutive whitespaces, with the
40
+ ``separator`` argument you can set a string to split by (e.g.
41
+ ``\'\t\'``).
42
+
43
+
44
+ @todo: Add support for reading from corpora where different
45
+ parallel files contain different columns.
46
+ @todo: Possibly add caching of the grid corpus view? This would
47
+ allow the same grid view to be used by different data access
48
+ methods (eg words() and parsed_sents() could both share the
49
+ same grid corpus view object).
50
+ @todo: Better support for -DOCSTART-. Currently, we just ignore
51
+ it, but it could be used to define methods that retrieve a
52
+ document at a time (eg parsed_documents()).
53
+ """
54
+
55
+ #/////////////////////////////////////////////////////////////////
56
+ # Column Types
57
+ #/////////////////////////////////////////////////////////////////
58
+
59
+ WORDS = 'words' #: column type for words
60
+ POS = 'pos' #: column type for part-of-speech tags
61
+ TREE = 'tree' #: column type for parse trees
62
+ CHUNK = 'chunk' #: column type for chunk structures
63
+ NE = 'ne' #: column type for named entities
64
+ SRL = 'srl' #: column type for semantic role labels
65
+ IGNORE = 'ignore' #: column type for column that should be ignored
66
+
67
+ #: A list of all column types supported by the conll corpus reader.
68
+ COLUMN_TYPES = (WORDS, POS, TREE, CHUNK, NE, SRL, IGNORE)
69
+
70
+ #/////////////////////////////////////////////////////////////////
71
+ # Constructor
72
+ #/////////////////////////////////////////////////////////////////
73
+
74
+ def __init__(self, root, fileids, columntypes,
75
+ chunk_types=None, root_label='S', pos_in_tree=False,
76
+ srl_includes_roleset=True, encoding='utf8',
77
+ tree_class=Tree, tagset=None, separator=None):
78
+ for columntype in columntypes:
79
+ if columntype not in self.COLUMN_TYPES:
80
+ raise ValueError('Bad column type %r' % columntype)
81
+ if isinstance(chunk_types, string_types):
82
+ chunk_types = [chunk_types]
83
+ self._chunk_types = chunk_types
84
+ self._colmap = dict((c,i) for (i,c) in enumerate(columntypes))
85
+ self._pos_in_tree = pos_in_tree
86
+ self._root_label = root_label # for chunks
87
+ self._srl_includes_roleset = srl_includes_roleset
88
+ self._tree_class = tree_class
89
+ CorpusReader.__init__(self, root, fileids, encoding)
90
+ self._tagset = tagset
91
+ self.sep = separator
92
+
93
+ #/////////////////////////////////////////////////////////////////
94
+ # Data Access Methods
95
+ #/////////////////////////////////////////////////////////////////
96
+
97
+ def raw(self, fileids=None):
98
+ if fileids is None: fileids = self._fileids
99
+ elif isinstance(fileids, string_types): fileids = [fileids]
100
+ return concat([self.open(f).read() for f in fileids])
101
+
102
+ def words(self, fileids=None):
103
+ self._require(self.WORDS)
104
+ return LazyConcatenation(LazyMap(self._get_words, self._grids(fileids)))
105
+
106
+ def sents(self, fileids=None):
107
+ self._require(self.WORDS)
108
+ return LazyMap(self._get_words, self._grids(fileids))
109
+
110
+ def tagged_words(self, fileids=None, tagset=None):
111
+ self._require(self.WORDS, self.POS)
112
+ def get_tagged_words(grid):
113
+ return self._get_tagged_words(grid, tagset)
114
+ return LazyConcatenation(LazyMap(get_tagged_words,
115
+ self._grids(fileids)))
116
+
117
+ def tagged_sents(self, fileids=None, tagset=None):
118
+ self._require(self.WORDS, self.POS)
119
+ def get_tagged_words(grid):
120
+ return self._get_tagged_words(grid, tagset)
121
+ return LazyMap(get_tagged_words, self._grids(fileids))
122
+
123
+ def chunked_words(self, fileids=None, chunk_types=None,
124
+ tagset=None):
125
+ self._require(self.WORDS, self.POS, self.CHUNK)
126
+ if chunk_types is None: chunk_types = self._chunk_types
127
+ def get_chunked_words(grid): # capture chunk_types as local var
128
+ return self._get_chunked_words(grid, chunk_types, tagset)
129
+ return LazyConcatenation(LazyMap(get_chunked_words,
130
+ self._grids(fileids)))
131
+
132
+ def chunked_sents(self, fileids=None, chunk_types=None,
133
+ tagset=None):
134
+ self._require(self.WORDS, self.POS, self.CHUNK)
135
+ if chunk_types is None: chunk_types = self._chunk_types
136
+ def get_chunked_words(grid): # capture chunk_types as local var
137
+ return self._get_chunked_words(grid, chunk_types, tagset)
138
+ return LazyMap(get_chunked_words, self._grids(fileids))
139
+
140
+ def parsed_sents(self, fileids=None, pos_in_tree=None, tagset=None):
141
+ self._require(self.WORDS, self.POS, self.TREE)
142
+ if pos_in_tree is None: pos_in_tree = self._pos_in_tree
143
+ def get_parsed_sent(grid): # capture pos_in_tree as local var
144
+ return self._get_parsed_sent(grid, pos_in_tree, tagset)
145
+ return LazyMap(get_parsed_sent, self._grids(fileids))
146
+
147
+ def srl_spans(self, fileids=None):
148
+ self._require(self.SRL)
149
+ return LazyMap(self._get_srl_spans, self._grids(fileids))
150
+
151
+ def srl_instances(self, fileids=None, pos_in_tree=None, flatten=True):
152
+ self._require(self.WORDS, self.POS, self.TREE, self.SRL)
153
+ if pos_in_tree is None: pos_in_tree = self._pos_in_tree
154
+ def get_srl_instances(grid): # capture pos_in_tree as local var
155
+ return self._get_srl_instances(grid, pos_in_tree)
156
+ result = LazyMap(get_srl_instances, self._grids(fileids))
157
+ if flatten: result = LazyConcatenation(result)
158
+ return result
159
+
160
+ def iob_words(self, fileids=None, tagset=None):
161
+ """
162
+ :return: a list of word/tag/IOB tuples
163
+ :rtype: list(tuple)
164
+ :param fileids: the list of fileids that make up this corpus
165
+ :type fileids: None or str or list
166
+ """
167
+ self._require(self.WORDS, self.POS, self.CHUNK)
168
+ def get_iob_words(grid):
169
+ return self._get_iob_words(grid, tagset)
170
+ return LazyConcatenation(LazyMap(get_iob_words, self._grids(fileids)))
171
+
172
+ def iob_sents(self, fileids=None, tagset=None):
173
+ """
174
+ :return: a list of lists of word/tag/IOB tuples
175
+ :rtype: list(list)
176
+ :param fileids: the list of fileids that make up this corpus
177
+ :type fileids: None or str or list
178
+ """
179
+ self._require(self.WORDS, self.POS, self.CHUNK)
180
+ def get_iob_words(grid):
181
+ return self._get_iob_words(grid, tagset)
182
+ return LazyMap(get_iob_words, self._grids(fileids))
183
+
184
+ #/////////////////////////////////////////////////////////////////
185
+ # Grid Reading
186
+ #/////////////////////////////////////////////////////////////////
187
+
188
+ def _grids(self, fileids=None):
189
+ # n.b.: we could cache the object returned here (keyed on
190
+ # fileids), which would let us reuse the same corpus view for
191
+ # different things (eg srl and parse trees).
192
+ return concat([StreamBackedCorpusView(fileid, self._read_grid_block,
193
+ encoding=enc)
194
+ for (fileid, enc) in self.abspaths(fileids, True)])
195
+
196
+ def _read_grid_block(self, stream):
197
+ grids = []
198
+ for block in read_blankline_block(stream):
199
+ block = block.strip()
200
+ if not block: continue
201
+
202
+ grid = [line.split(self.sep) for line in block.split('\n')]
203
+
204
+ # If there's a docstart row, then discard. ([xx] eventually it
205
+ # would be good to actually use it)
206
+ if grid[0][self._colmap.get('words', 0)] == '-DOCSTART-':
207
+ del grid[0]
208
+
209
+ # Check that the grid is consistent.
210
+ for row in grid:
211
+ if len(row) != len(grid[0]):
212
+ raise ValueError('Inconsistent number of columns:\n%s'
213
+ % block)
214
+ grids.append(grid)
215
+ return grids
216
+
217
+ #/////////////////////////////////////////////////////////////////
218
+ # Transforms
219
+ #/////////////////////////////////////////////////////////////////
220
+ # given a grid, transform it into some representation (e.g.,
221
+ # a list of words or a parse tree).
222
+
223
+ def _get_words(self, grid):
224
+ return self._get_column(grid, self._colmap['words'])
225
+
226
+ def _get_tagged_words(self, grid, tagset=None):
227
+ pos_tags = self._get_column(grid, self._colmap['pos'])
228
+ if tagset and tagset != self._tagset:
229
+ pos_tags = [map_tag(self._tagset, tagset, t) for t in pos_tags]
230
+ return list(zip(self._get_column(grid, self._colmap['words']), pos_tags))
231
+
232
+ def _get_iob_words(self, grid, tagset=None):
233
+ pos_tags = self._get_column(grid, self._colmap['pos'])
234
+ if tagset and tagset != self._tagset:
235
+ pos_tags = [map_tag(self._tagset, tagset, t) for t in pos_tags]
236
+ return list(zip(self._get_column(grid, self._colmap['words']), pos_tags,
237
+ self._get_column(grid, self._colmap['chunk'])))
238
+
239
+ def _get_chunked_words(self, grid, chunk_types, tagset=None):
240
+ # n.b.: this method is very similar to conllstr2tree.
241
+ words = self._get_column(grid, self._colmap['words'])
242
+ pos_tags = self._get_column(grid, self._colmap['pos'])
243
+ if tagset and tagset != self._tagset:
244
+ pos_tags = [map_tag(self._tagset, tagset, t) for t in pos_tags]
245
+ chunk_tags = self._get_column(grid, self._colmap['chunk'])
246
+
247
+ stack = [Tree(self._root_label, [])]
248
+
249
+ for (word, pos_tag, chunk_tag) in zip(words, pos_tags, chunk_tags):
250
+ if chunk_tag == 'O':
251
+ state, chunk_type = 'O', ''
252
+ else:
253
+ (state, chunk_type) = chunk_tag.split('-')
254
+ # If it's a chunk we don't care about, treat it as O.
255
+ if chunk_types is not None and chunk_type not in chunk_types:
256
+ state = 'O'
257
+ # Treat a mismatching I like a B.
258
+ if state == 'I' and chunk_type != stack[-1].label():
259
+ state = 'B'
260
+ # For B or I: close any open chunks
261
+ if state in 'BO' and len(stack) == 2:
262
+ stack.pop()
263
+ # For B: start a new chunk.
264
+ if state == 'B':
265
+ new_chunk = Tree(chunk_type, [])
266
+ stack[-1].append(new_chunk)
267
+ stack.append(new_chunk)
268
+ # Add the word token.
269
+ stack[-1].append((word, pos_tag))
270
+
271
+ return stack[0]
272
+
273
+ def _get_parsed_sent(self, grid, pos_in_tree, tagset=None):
274
+ words = self._get_column(grid, self._colmap['words'])
275
+ pos_tags = self._get_column(grid, self._colmap['pos'])
276
+ if tagset and tagset != self._tagset:
277
+ pos_tags = [map_tag(self._tagset, tagset, t) for t in pos_tags]
278
+ parse_tags = self._get_column(grid, self._colmap['tree'])
279
+
280
+ treestr = ''
281
+ for (word, pos_tag, parse_tag) in zip(words, pos_tags, parse_tags):
282
+ if word == '(': word = '-LRB-'
283
+ if word == ')': word = '-RRB-'
284
+ if pos_tag == '(': pos_tag = '-LRB-'
285
+ if pos_tag == ')': pos_tag = '-RRB-'
286
+ (left, right) = parse_tag.split('*')
287
+ right = right.count(')')*')' # only keep ')'.
288
+ treestr += '%s (%s %s) %s' % (left, pos_tag, word, right)
289
+ try:
290
+ tree = self._tree_class.fromstring(treestr)
291
+ except (ValueError, IndexError):
292
+ tree = self._tree_class.fromstring('(%s %s)' %
293
+ (self._root_label, treestr))
294
+
295
+ if not pos_in_tree:
296
+ for subtree in tree.subtrees():
297
+ for i, child in enumerate(subtree):
298
+ if (isinstance(child, Tree) and len(child)==1 and
299
+ isinstance(child[0], string_types)):
300
+ subtree[i] = (child[0], child.label())
301
+
302
+ return tree
303
+
304
+ def _get_srl_spans(self, grid):
305
+ """
306
+ list of list of (start, end), tag) tuples
307
+ """
308
+ if self._srl_includes_roleset:
309
+ predicates = self._get_column(grid, self._colmap['srl']+1)
310
+ start_col = self._colmap['srl']+2
311
+ else:
312
+ predicates = self._get_column(grid, self._colmap['srl'])
313
+ start_col = self._colmap['srl']+1
314
+
315
+ # Count how many predicates there are. This tells us how many
316
+ # columns to expect for SRL data.
317
+ num_preds = len([p for p in predicates if p != '-'])
318
+
319
+ spanlists = []
320
+ for i in range(num_preds):
321
+ col = self._get_column(grid, start_col+i)
322
+ spanlist = []
323
+ stack = []
324
+ for wordnum, srl_tag in enumerate(col):
325
+ (left, right) = srl_tag.split('*')
326
+ for tag in left.split('('):
327
+ if tag:
328
+ stack.append((tag, wordnum))
329
+ for i in range(right.count(')')):
330
+ (tag, start) = stack.pop()
331
+ spanlist.append( ((start, wordnum+1), tag) )
332
+ spanlists.append(spanlist)
333
+
334
+ return spanlists
335
+
336
+ def _get_srl_instances(self, grid, pos_in_tree):
337
+ tree = self._get_parsed_sent(grid, pos_in_tree)
338
+ spanlists = self._get_srl_spans(grid)
339
+ if self._srl_includes_roleset:
340
+ predicates = self._get_column(grid, self._colmap['srl']+1)
341
+ rolesets = self._get_column(grid, self._colmap['srl'])
342
+ else:
343
+ predicates = self._get_column(grid, self._colmap['srl'])
344
+ rolesets = [None] * len(predicates)
345
+
346
+ instances = ConllSRLInstanceList(tree)
347
+ for wordnum, predicate in enumerate(predicates):
348
+ if predicate == '-': continue
349
+ # Decide which spanlist to use. Don't assume that they're
350
+ # sorted in the same order as the predicates (even though
351
+ # they usually are).
352
+ for spanlist in spanlists:
353
+ for (start, end), tag in spanlist:
354
+ if wordnum in range(start,end) and tag in ('V', 'C-V'):
355
+ break
356
+ else: continue
357
+ break
358
+ else:
359
+ raise ValueError('No srl column found for %r' % predicate)
360
+ instances.append(ConllSRLInstance(tree, wordnum, predicate,
361
+ rolesets[wordnum], spanlist))
362
+
363
+ return instances
364
+
365
+ #/////////////////////////////////////////////////////////////////
366
+ # Helper Methods
367
+ #/////////////////////////////////////////////////////////////////
368
+
369
+ def _require(self, *columntypes):
370
+ for columntype in columntypes:
371
+ if columntype not in self._colmap:
372
+ raise ValueError('This corpus does not contain a %s '
373
+ 'column.' % columntype)
374
+
375
+ @staticmethod
376
+ def _get_column(grid, column_index):
377
+ return [grid[i][column_index] for i in range(len(grid))]
378
+
379
+
380
+ @compat.python_2_unicode_compatible
381
+ class ConllSRLInstance(object):
382
+ """
383
+ An SRL instance from a CoNLL corpus, which identifies and
384
+ providing labels for the arguments of a single verb.
385
+ """
386
+ # [xx] add inst.core_arguments, inst.argm_arguments?
387
+
388
+ def __init__(self, tree, verb_head, verb_stem, roleset, tagged_spans):
389
+ self.verb = []
390
+ """A list of the word indices of the words that compose the
391
+ verb whose arguments are identified by this instance.
392
+ This will contain multiple word indices when multi-word
393
+ verbs are used (e.g. 'turn on')."""
394
+
395
+ self.verb_head = verb_head
396
+ """The word index of the head word of the verb whose arguments
397
+ are identified by this instance. E.g., for a sentence that
398
+ uses the verb 'turn on,' ``verb_head`` will be the word index
399
+ of the word 'turn'."""
400
+
401
+ self.verb_stem = verb_stem
402
+
403
+ self.roleset = roleset
404
+
405
+ self.arguments = []
406
+ """A list of ``(argspan, argid)`` tuples, specifying the location
407
+ and type for each of the arguments identified by this
408
+ instance. ``argspan`` is a tuple ``start, end``, indicating
409
+ that the argument consists of the ``words[start:end]``."""
410
+
411
+ self.tagged_spans = tagged_spans
412
+ """A list of ``(span, id)`` tuples, specifying the location and
413
+ type for each of the arguments, as well as the verb pieces,
414
+ that make up this instance."""
415
+
416
+ self.tree = tree
417
+ """The parse tree for the sentence containing this instance."""
418
+
419
+ self.words = tree.leaves()
420
+ """A list of the words in the sentence containing this
421
+ instance."""
422
+
423
+ # Fill in the self.verb and self.arguments values.
424
+ for (start, end), tag in tagged_spans:
425
+ if tag in ('V', 'C-V'):
426
+ self.verb += list(range(start, end))
427
+ else:
428
+ self.arguments.append( ((start, end), tag) )
429
+
430
+ def __repr__(self):
431
+ plural = len(self.arguments)!=1 and 's' or ''
432
+ return '<ConllSRLInstance for %r with %d argument%s>' % (
433
+ (self.verb_stem, len(self.arguments), plural))
434
+
435
+ def pprint(self):
436
+ verbstr = ' '.join(self.words[i][0] for i in self.verb)
437
+ hdr = 'SRL for %r (stem=%r):\n' % (verbstr, self.verb_stem)
438
+ s = ''
439
+ for i, word in enumerate(self.words):
440
+ if isinstance(word, tuple): word = word[0]
441
+ for (start, end), argid in self.arguments:
442
+ if i == start: s += '[%s ' % argid
443
+ if i == end: s += '] '
444
+ if i in self.verb: word = '<<%s>>' % word
445
+ s += word + ' '
446
+ return hdr + textwrap.fill(s.replace(' ]', ']'),
447
+ initial_indent=' ',
448
+ subsequent_indent=' ')
449
+
450
+ @compat.python_2_unicode_compatible
451
+ class ConllSRLInstanceList(list):
452
+ """
453
+ Set of instances for a single sentence
454
+ """
455
+ def __init__(self, tree, instances=()):
456
+ self.tree = tree
457
+ list.__init__(self, instances)
458
+
459
+ def __str__(self):
460
+ return self.pprint()
461
+
462
+ def pprint(self, include_tree=False):
463
+ # Sanity check: trees should be the same
464
+ for inst in self:
465
+ if inst.tree != self.tree:
466
+ raise ValueError('Tree mismatch!')
467
+
468
+ # If desired, add trees:
469
+ if include_tree:
470
+ words = self.tree.leaves()
471
+ pos = [None] * len(words)
472
+ synt = ['*'] * len(words)
473
+ self._tree2conll(self.tree, 0, words, pos, synt)
474
+
475
+ s = ''
476
+ for i in range(len(words)):
477
+ # optional tree columns
478
+ if include_tree:
479
+ s += '%-20s ' % words[i]
480
+ s += '%-8s ' % pos[i]
481
+ s += '%15s*%-8s ' % tuple(synt[i].split('*'))
482
+
483
+ # verb head column
484
+ for inst in self:
485
+ if i == inst.verb_head:
486
+ s += '%-20s ' % inst.verb_stem
487
+ break
488
+ else:
489
+ s += '%-20s ' % '-'
490
+ # Remaining columns: self
491
+ for inst in self:
492
+ argstr = '*'
493
+ for (start, end), argid in inst.tagged_spans:
494
+ if i==start: argstr = '(%s%s' % (argid, argstr)
495
+ if i==(end-1): argstr += ')'
496
+ s += '%-12s ' % argstr
497
+ s += '\n'
498
+ return s
499
+
500
+ def _tree2conll(self, tree, wordnum, words, pos, synt):
501
+ assert isinstance(tree, Tree)
502
+ if len(tree) == 1 and isinstance(tree[0], string_types):
503
+ pos[wordnum] = tree.label()
504
+ assert words[wordnum] == tree[0]
505
+ return wordnum+1
506
+ elif len(tree) == 1 and isinstance(tree[0], tuple):
507
+ assert len(tree[0]) == 2
508
+ pos[wordnum], pos[wordnum] = tree[0]
509
+ return wordnum+1
510
+ else:
511
+ synt[wordnum] = '(%s%s' % (tree.label(), synt[wordnum])
512
+ for child in tree:
513
+ wordnum = self._tree2conll(child, wordnum, words,
514
+ pos, synt)
515
+ synt[wordnum-1] += ')'
516
+ return wordnum
517
+
518
+ class ConllChunkCorpusReader(ConllCorpusReader):
519
+ """
520
+ A ConllCorpusReader whose data file contains three columns: words,
521
+ pos, and chunk.
522
+ """
523
+ def __init__(self, root, fileids, chunk_types, encoding='utf8',
524
+ tagset=None, separator=None):
525
+ ConllCorpusReader.__init__(
526
+ self, root, fileids, ('words', 'pos', 'chunk'),
527
+ chunk_types=chunk_types, encoding=encoding,
528
+ tagset=tagset, separator=separator)