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,884 +1,884 @@
1
- .. Copyright (C) 2001-2015 NLTK Project
2
- .. For license information, see LICENSE.TXT
3
-
4
- =========
5
- Parsing
6
- =========
7
-
8
- Unit tests for the Context Free Grammar class
9
- ---------------------------------------------
10
-
11
- >>> from nltk import Nonterminal, nonterminals, Production, CFG
12
-
13
- >>> nt1 = Nonterminal('NP')
14
- >>> nt2 = Nonterminal('VP')
15
-
16
- >>> nt1.symbol()
17
- 'NP'
18
-
19
- >>> nt1 == Nonterminal('NP')
20
- True
21
-
22
- >>> nt1 == nt2
23
- False
24
-
25
- >>> S, NP, VP, PP = nonterminals('S, NP, VP, PP')
26
- >>> N, V, P, DT = nonterminals('N, V, P, DT')
27
-
28
- >>> prod1 = Production(S, [NP, VP])
29
- >>> prod2 = Production(NP, [DT, NP])
30
-
31
- >>> prod1.lhs()
32
- S
33
-
34
- >>> prod1.rhs()
35
- (NP, VP)
36
-
37
- >>> prod1 == Production(S, [NP, VP])
38
- True
39
-
40
- >>> prod1 == prod2
41
- False
42
-
43
- >>> grammar = CFG.fromstring("""
44
- ... S -> NP VP
45
- ... PP -> P NP
46
- ... NP -> 'the' N | N PP | 'the' N PP
47
- ... VP -> V NP | V PP | V NP PP
48
- ... N -> 'cat'
49
- ... N -> 'dog'
50
- ... N -> 'rug'
51
- ... V -> 'chased'
52
- ... V -> 'sat'
53
- ... P -> 'in'
54
- ... P -> 'on'
55
- ... """)
56
-
57
- Unit tests for the rd (Recursive Descent Parser) class
58
- ------------------------------------------------------
59
-
60
- Create and run a recursive descent parser over both a syntactically ambiguous
61
- and unambiguous sentence.
62
-
63
- >>> from nltk.parse import RecursiveDescentParser
64
- >>> rd = RecursiveDescentParser(grammar)
65
-
66
- >>> sentence1 = 'the cat chased the dog'.split()
67
- >>> sentence2 = 'the cat chased the dog on the rug'.split()
68
-
69
- >>> for t in rd.parse(sentence1):
70
- ... print(t)
71
- (S (NP the (N cat)) (VP (V chased) (NP the (N dog))))
72
-
73
- >>> for t in rd.parse(sentence2):
74
- ... print(t)
75
- (S
76
- (NP the (N cat))
77
- (VP (V chased) (NP the (N dog) (PP (P on) (NP the (N rug))))))
78
- (S
79
- (NP the (N cat))
80
- (VP (V chased) (NP the (N dog)) (PP (P on) (NP the (N rug)))))
81
-
82
-
83
- (dolist (expr doctest-font-lock-keywords)
84
- (add-to-list 'font-lock-keywords expr))
85
-
86
- font-lock-keywords
87
- (add-to-list 'font-lock-keywords
88
- (car doctest-font-lock-keywords))
89
-
90
-
91
- Unit tests for the sr (Shift Reduce Parser) class
92
- -------------------------------------------------
93
-
94
- Create and run a shift reduce parser over both a syntactically ambiguous
95
- and unambiguous sentence. Note that unlike the recursive descent parser, one
96
- and only one parse is ever returned.
97
-
98
- >>> from nltk.parse import ShiftReduceParser
99
- >>> sr = ShiftReduceParser(grammar)
100
-
101
- >>> sentence1 = 'the cat chased the dog'.split()
102
- >>> sentence2 = 'the cat chased the dog on the rug'.split()
103
-
104
- >>> for t in sr.parse(sentence1):
105
- ... print(t)
106
- (S (NP the (N cat)) (VP (V chased) (NP the (N dog))))
107
-
108
-
109
- The shift reduce parser uses heuristics to decide what to do when there are
110
- multiple possible shift or reduce operations available - for the supplied
111
- grammar clearly the wrong operation is selected.
112
-
113
- >>> for t in sr.parse(sentence2):
114
- ... print(t)
115
-
116
-
117
- Unit tests for the Chart Parser class
118
- -------------------------------------
119
-
120
- We use the demo() function for testing.
121
- We must turn off showing of times.
122
-
123
- >>> import nltk
124
-
125
- First we test tracing with a short sentence
126
-
127
- >>> nltk.parse.chart.demo(2, print_times=False, trace=1,
128
- ... sent='I saw a dog', numparses=1)
129
- * Sentence:
130
- I saw a dog
131
- ['I', 'saw', 'a', 'dog']
132
- <BLANKLINE>
133
- * Strategy: Bottom-up
134
- <BLANKLINE>
135
- |. I . saw . a . dog .|
136
- |[---------] . . .| [0:1] 'I'
137
- |. [---------] . .| [1:2] 'saw'
138
- |. . [---------] .| [2:3] 'a'
139
- |. . . [---------]| [3:4] 'dog'
140
- |> . . . .| [0:0] NP -> * 'I'
141
- |[---------] . . .| [0:1] NP -> 'I' *
142
- |> . . . .| [0:0] S -> * NP VP
143
- |> . . . .| [0:0] NP -> * NP PP
144
- |[---------> . . .| [0:1] S -> NP * VP
145
- |[---------> . . .| [0:1] NP -> NP * PP
146
- |. > . . .| [1:1] Verb -> * 'saw'
147
- |. [---------] . .| [1:2] Verb -> 'saw' *
148
- |. > . . .| [1:1] VP -> * Verb NP
149
- |. > . . .| [1:1] VP -> * Verb
150
- |. [---------> . .| [1:2] VP -> Verb * NP
151
- |. [---------] . .| [1:2] VP -> Verb *
152
- |. > . . .| [1:1] VP -> * VP PP
153
- |[-------------------] . .| [0:2] S -> NP VP *
154
- |. [---------> . .| [1:2] VP -> VP * PP
155
- |. . > . .| [2:2] Det -> * 'a'
156
- |. . [---------] .| [2:3] Det -> 'a' *
157
- |. . > . .| [2:2] NP -> * Det Noun
158
- |. . [---------> .| [2:3] NP -> Det * Noun
159
- |. . . > .| [3:3] Noun -> * 'dog'
160
- |. . . [---------]| [3:4] Noun -> 'dog' *
161
- |. . [-------------------]| [2:4] NP -> Det Noun *
162
- |. . > . .| [2:2] S -> * NP VP
163
- |. . > . .| [2:2] NP -> * NP PP
164
- |. [-----------------------------]| [1:4] VP -> Verb NP *
165
- |. . [------------------->| [2:4] S -> NP * VP
166
- |. . [------------------->| [2:4] NP -> NP * PP
167
- |[=======================================]| [0:4] S -> NP VP *
168
- |. [----------------------------->| [1:4] VP -> VP * PP
169
- Nr edges in chart: 33
170
- (S (NP I) (VP (Verb saw) (NP (Det a) (Noun dog))))
171
- <BLANKLINE>
172
-
173
- Then we test the different parsing Strategies.
174
- Note that the number of edges differ between the strategies.
175
-
176
- Top-down
177
-
178
- >>> nltk.parse.chart.demo(1, print_times=False, trace=0,
179
- ... sent='I saw John with a dog', numparses=2)
180
- * Sentence:
181
- I saw John with a dog
182
- ['I', 'saw', 'John', 'with', 'a', 'dog']
183
- <BLANKLINE>
184
- * Strategy: Top-down
185
- <BLANKLINE>
186
- Nr edges in chart: 48
187
- (S
188
- (NP I)
189
- (VP (Verb saw) (NP (NP John) (PP with (NP (Det a) (Noun dog))))))
190
- (S
191
- (NP I)
192
- (VP (VP (Verb saw) (NP John)) (PP with (NP (Det a) (Noun dog)))))
193
- <BLANKLINE>
194
-
195
- Bottom-up
196
-
197
- >>> nltk.parse.chart.demo(2, print_times=False, trace=0,
198
- ... sent='I saw John with a dog', numparses=2)
199
- * Sentence:
200
- I saw John with a dog
201
- ['I', 'saw', 'John', 'with', 'a', 'dog']
202
- <BLANKLINE>
203
- * Strategy: Bottom-up
204
- <BLANKLINE>
205
- Nr edges in chart: 53
206
- (S
207
- (NP I)
208
- (VP (VP (Verb saw) (NP John)) (PP with (NP (Det a) (Noun dog)))))
209
- (S
210
- (NP I)
211
- (VP (Verb saw) (NP (NP John) (PP with (NP (Det a) (Noun dog))))))
212
- <BLANKLINE>
213
-
214
- Bottom-up Left-Corner
215
-
216
- >>> nltk.parse.chart.demo(3, print_times=False, trace=0,
217
- ... sent='I saw John with a dog', numparses=2)
218
- * Sentence:
219
- I saw John with a dog
220
- ['I', 'saw', 'John', 'with', 'a', 'dog']
221
- <BLANKLINE>
222
- * Strategy: Bottom-up left-corner
223
- <BLANKLINE>
224
- Nr edges in chart: 36
225
- (S
226
- (NP I)
227
- (VP (VP (Verb saw) (NP John)) (PP with (NP (Det a) (Noun dog)))))
228
- (S
229
- (NP I)
230
- (VP (Verb saw) (NP (NP John) (PP with (NP (Det a) (Noun dog))))))
231
- <BLANKLINE>
232
-
233
- Left-Corner with Bottom-Up Filter
234
-
235
- >>> nltk.parse.chart.demo(4, print_times=False, trace=0,
236
- ... sent='I saw John with a dog', numparses=2)
237
- * Sentence:
238
- I saw John with a dog
239
- ['I', 'saw', 'John', 'with', 'a', 'dog']
240
- <BLANKLINE>
241
- * Strategy: Filtered left-corner
242
- <BLANKLINE>
243
- Nr edges in chart: 28
244
- (S
245
- (NP I)
246
- (VP (VP (Verb saw) (NP John)) (PP with (NP (Det a) (Noun dog)))))
247
- (S
248
- (NP I)
249
- (VP (Verb saw) (NP (NP John) (PP with (NP (Det a) (Noun dog))))))
250
- <BLANKLINE>
251
-
252
- The stepping chart parser
253
-
254
- >>> nltk.parse.chart.demo(5, print_times=False, trace=1,
255
- ... sent='I saw John with a dog', numparses=2)
256
- * Sentence:
257
- I saw John with a dog
258
- ['I', 'saw', 'John', 'with', 'a', 'dog']
259
- <BLANKLINE>
260
- * Strategy: Stepping (top-down vs bottom-up)
261
- <BLANKLINE>
262
- *** SWITCH TO TOP DOWN
263
- |[------] . . . . .| [0:1] 'I'
264
- |. [------] . . . .| [1:2] 'saw'
265
- |. . [------] . . .| [2:3] 'John'
266
- |. . . [------] . .| [3:4] 'with'
267
- |. . . . [------] .| [4:5] 'a'
268
- |. . . . . [------]| [5:6] 'dog'
269
- |> . . . . . .| [0:0] S -> * NP VP
270
- |> . . . . . .| [0:0] NP -> * NP PP
271
- |> . . . . . .| [0:0] NP -> * Det Noun
272
- |> . . . . . .| [0:0] NP -> * 'I'
273
- |[------] . . . . .| [0:1] NP -> 'I' *
274
- |[------> . . . . .| [0:1] S -> NP * VP
275
- |[------> . . . . .| [0:1] NP -> NP * PP
276
- |. > . . . . .| [1:1] VP -> * VP PP
277
- |. > . . . . .| [1:1] VP -> * Verb NP
278
- |. > . . . . .| [1:1] VP -> * Verb
279
- |. > . . . . .| [1:1] Verb -> * 'saw'
280
- |. [------] . . . .| [1:2] Verb -> 'saw' *
281
- |. [------> . . . .| [1:2] VP -> Verb * NP
282
- |. [------] . . . .| [1:2] VP -> Verb *
283
- |[-------------] . . . .| [0:2] S -> NP VP *
284
- |. [------> . . . .| [1:2] VP -> VP * PP
285
- *** SWITCH TO BOTTOM UP
286
- |. . > . . . .| [2:2] NP -> * 'John'
287
- |. . . > . . .| [3:3] PP -> * 'with' NP
288
- |. . . > . . .| [3:3] Prep -> * 'with'
289
- |. . . . > . .| [4:4] Det -> * 'a'
290
- |. . . . . > .| [5:5] Noun -> * 'dog'
291
- |. . [------] . . .| [2:3] NP -> 'John' *
292
- |. . . [------> . .| [3:4] PP -> 'with' * NP
293
- |. . . [------] . .| [3:4] Prep -> 'with' *
294
- |. . . . [------] .| [4:5] Det -> 'a' *
295
- |. . . . . [------]| [5:6] Noun -> 'dog' *
296
- |. [-------------] . . .| [1:3] VP -> Verb NP *
297
- |[--------------------] . . .| [0:3] S -> NP VP *
298
- |. [-------------> . . .| [1:3] VP -> VP * PP
299
- |. . > . . . .| [2:2] S -> * NP VP
300
- |. . > . . . .| [2:2] NP -> * NP PP
301
- |. . . . > . .| [4:4] NP -> * Det Noun
302
- |. . [------> . . .| [2:3] S -> NP * VP
303
- |. . [------> . . .| [2:3] NP -> NP * PP
304
- |. . . . [------> .| [4:5] NP -> Det * Noun
305
- |. . . . [-------------]| [4:6] NP -> Det Noun *
306
- |. . . [--------------------]| [3:6] PP -> 'with' NP *
307
- |. [----------------------------------]| [1:6] VP -> VP PP *
308
- *** SWITCH TO TOP DOWN
309
- |. . > . . . .| [2:2] NP -> * Det Noun
310
- |. . . . > . .| [4:4] NP -> * NP PP
311
- |. . . > . . .| [3:3] VP -> * VP PP
312
- |. . . > . . .| [3:3] VP -> * Verb NP
313
- |. . . > . . .| [3:3] VP -> * Verb
314
- |[=========================================]| [0:6] S -> NP VP *
315
- |. [---------------------------------->| [1:6] VP -> VP * PP
316
- |. . [---------------------------]| [2:6] NP -> NP PP *
317
- |. . . . [------------->| [4:6] NP -> NP * PP
318
- |. [----------------------------------]| [1:6] VP -> Verb NP *
319
- |. . [--------------------------->| [2:6] S -> NP * VP
320
- |. . [--------------------------->| [2:6] NP -> NP * PP
321
- |[=========================================]| [0:6] S -> NP VP *
322
- |. [---------------------------------->| [1:6] VP -> VP * PP
323
- |. . . . . . >| [6:6] VP -> * VP PP
324
- |. . . . . . >| [6:6] VP -> * Verb NP
325
- |. . . . . . >| [6:6] VP -> * Verb
326
- *** SWITCH TO BOTTOM UP
327
- |. . . . > . .| [4:4] S -> * NP VP
328
- |. . . . [------------->| [4:6] S -> NP * VP
329
- *** SWITCH TO TOP DOWN
330
- *** SWITCH TO BOTTOM UP
331
- *** SWITCH TO TOP DOWN
332
- *** SWITCH TO BOTTOM UP
333
- *** SWITCH TO TOP DOWN
334
- *** SWITCH TO BOTTOM UP
335
- Nr edges in chart: 61
336
- (S
337
- (NP I)
338
- (VP (VP (Verb saw) (NP John)) (PP with (NP (Det a) (Noun dog)))))
339
- (S
340
- (NP I)
341
- (VP (Verb saw) (NP (NP John) (PP with (NP (Det a) (Noun dog))))))
342
- <BLANKLINE>
343
-
344
-
345
- Unit tests for the Incremental Chart Parser class
346
- -------------------------------------------------
347
-
348
- The incremental chart parsers are defined in earleychart.py.
349
- We use the demo() function for testing. We must turn off showing of times.
350
-
351
- >>> import nltk
352
-
353
- Earley Chart Parser
354
-
355
- >>> nltk.parse.earleychart.demo(print_times=False, trace=1,
356
- ... sent='I saw John with a dog', numparses=2)
357
- * Sentence:
358
- I saw John with a dog
359
- ['I', 'saw', 'John', 'with', 'a', 'dog']
360
- <BLANKLINE>
361
- |. I . saw . John . with . a . dog .|
362
- |[------] . . . . .| [0:1] 'I'
363
- |. [------] . . . .| [1:2] 'saw'
364
- |. . [------] . . .| [2:3] 'John'
365
- |. . . [------] . .| [3:4] 'with'
366
- |. . . . [------] .| [4:5] 'a'
367
- |. . . . . [------]| [5:6] 'dog'
368
- |> . . . . . .| [0:0] S -> * NP VP
369
- |> . . . . . .| [0:0] NP -> * NP PP
370
- |> . . . . . .| [0:0] NP -> * Det Noun
371
- |> . . . . . .| [0:0] NP -> * 'I'
372
- |[------] . . . . .| [0:1] NP -> 'I' *
373
- |[------> . . . . .| [0:1] S -> NP * VP
374
- |[------> . . . . .| [0:1] NP -> NP * PP
375
- |. > . . . . .| [1:1] VP -> * VP PP
376
- |. > . . . . .| [1:1] VP -> * Verb NP
377
- |. > . . . . .| [1:1] VP -> * Verb
378
- |. > . . . . .| [1:1] Verb -> * 'saw'
379
- |. [------] . . . .| [1:2] Verb -> 'saw' *
380
- |. [------> . . . .| [1:2] VP -> Verb * NP
381
- |. [------] . . . .| [1:2] VP -> Verb *
382
- |[-------------] . . . .| [0:2] S -> NP VP *
383
- |. [------> . . . .| [1:2] VP -> VP * PP
384
- |. . > . . . .| [2:2] NP -> * NP PP
385
- |. . > . . . .| [2:2] NP -> * Det Noun
386
- |. . > . . . .| [2:2] NP -> * 'John'
387
- |. . [------] . . .| [2:3] NP -> 'John' *
388
- |. [-------------] . . .| [1:3] VP -> Verb NP *
389
- |. . [------> . . .| [2:3] NP -> NP * PP
390
- |. . . > . . .| [3:3] PP -> * 'with' NP
391
- |[--------------------] . . .| [0:3] S -> NP VP *
392
- |. [-------------> . . .| [1:3] VP -> VP * PP
393
- |. . . [------> . .| [3:4] PP -> 'with' * NP
394
- |. . . . > . .| [4:4] NP -> * NP PP
395
- |. . . . > . .| [4:4] NP -> * Det Noun
396
- |. . . . > . .| [4:4] Det -> * 'a'
397
- |. . . . [------] .| [4:5] Det -> 'a' *
398
- |. . . . [------> .| [4:5] NP -> Det * Noun
399
- |. . . . . > .| [5:5] Noun -> * 'dog'
400
- |. . . . . [------]| [5:6] Noun -> 'dog' *
401
- |. . . . [-------------]| [4:6] NP -> Det Noun *
402
- |. . . [--------------------]| [3:6] PP -> 'with' NP *
403
- |. . . . [------------->| [4:6] NP -> NP * PP
404
- |. . [---------------------------]| [2:6] NP -> NP PP *
405
- |. [----------------------------------]| [1:6] VP -> VP PP *
406
- |[=========================================]| [0:6] S -> NP VP *
407
- |. [---------------------------------->| [1:6] VP -> VP * PP
408
- |. [----------------------------------]| [1:6] VP -> Verb NP *
409
- |. . [--------------------------->| [2:6] NP -> NP * PP
410
- |[=========================================]| [0:6] S -> NP VP *
411
- |. [---------------------------------->| [1:6] VP -> VP * PP
412
- (S
413
- (NP I)
414
- (VP (VP (Verb saw) (NP John)) (PP with (NP (Det a) (Noun dog)))))
415
- (S
416
- (NP I)
417
- (VP (Verb saw) (NP (NP John) (PP with (NP (Det a) (Noun dog))))))
418
-
419
-
420
- Unit tests for LARGE context-free grammars
421
- ------------------------------------------
422
-
423
- Reading the ATIS grammar.
424
-
425
- >>> grammar = nltk.data.load('grammars/large_grammars/atis.cfg')
426
- >>> grammar
427
- <Grammar with 5517 productions>
428
-
429
- Reading the test sentences.
430
-
431
- >>> sentences = nltk.data.load('grammars/large_grammars/atis_sentences.txt')
432
- >>> sentences = nltk.parse.util.extract_test_sentences(sentences)
433
- >>> len(sentences)
434
- 98
435
- >>> testsentence = sentences[22]
436
- >>> testsentence[0]
437
- ['show', 'me', 'northwest', 'flights', 'to', 'detroit', '.']
438
- >>> testsentence[1]
439
- 17
440
- >>> sentence = testsentence[0]
441
-
442
- Now we test all different parsing strategies.
443
- Note that the number of edges differ between the strategies.
444
-
445
- Bottom-up parsing.
446
-
447
- >>> parser = nltk.parse.BottomUpChartParser(grammar)
448
- >>> chart = parser.chart_parse(sentence)
449
- >>> print((chart.num_edges()))
450
- 7661
451
- >>> print((len(list(chart.parses(grammar.start())))))
452
- 17
453
-
454
- Bottom-up Left-corner parsing.
455
-
456
- >>> parser = nltk.parse.BottomUpLeftCornerChartParser(grammar)
457
- >>> chart = parser.chart_parse(sentence)
458
- >>> print((chart.num_edges()))
459
- 4986
460
- >>> print((len(list(chart.parses(grammar.start())))))
461
- 17
462
-
463
- Left-corner parsing with bottom-up filter.
464
-
465
- >>> parser = nltk.parse.LeftCornerChartParser(grammar)
466
- >>> chart = parser.chart_parse(sentence)
467
- >>> print((chart.num_edges()))
468
- 1342
469
- >>> print((len(list(chart.parses(grammar.start())))))
470
- 17
471
-
472
- Top-down parsing.
473
-
474
- >>> parser = nltk.parse.TopDownChartParser(grammar)
475
- >>> chart = parser.chart_parse(sentence)
476
- >>> print((chart.num_edges()))
477
- 28352
478
- >>> print((len(list(chart.parses(grammar.start())))))
479
- 17
480
-
481
- Incremental Bottom-up parsing.
482
-
483
- >>> parser = nltk.parse.IncrementalBottomUpChartParser(grammar)
484
- >>> chart = parser.chart_parse(sentence)
485
- >>> print((chart.num_edges()))
486
- 7661
487
- >>> print((len(list(chart.parses(grammar.start())))))
488
- 17
489
-
490
- Incremental Bottom-up Left-corner parsing.
491
-
492
- >>> parser = nltk.parse.IncrementalBottomUpLeftCornerChartParser(grammar)
493
- >>> chart = parser.chart_parse(sentence)
494
- >>> print((chart.num_edges()))
495
- 4986
496
- >>> print((len(list(chart.parses(grammar.start())))))
497
- 17
498
-
499
- Incremental Left-corner parsing with bottom-up filter.
500
-
501
- >>> parser = nltk.parse.IncrementalLeftCornerChartParser(grammar)
502
- >>> chart = parser.chart_parse(sentence)
503
- >>> print((chart.num_edges()))
504
- 1342
505
- >>> print((len(list(chart.parses(grammar.start())))))
506
- 17
507
-
508
- Incremental Top-down parsing.
509
-
510
- >>> parser = nltk.parse.IncrementalTopDownChartParser(grammar)
511
- >>> chart = parser.chart_parse(sentence)
512
- >>> print((chart.num_edges()))
513
- 28352
514
- >>> print((len(list(chart.parses(grammar.start())))))
515
- 17
516
-
517
- Earley parsing. This is similar to the incremental top-down algorithm.
518
-
519
- >>> parser = nltk.parse.EarleyChartParser(grammar)
520
- >>> chart = parser.chart_parse(sentence)
521
- >>> print((chart.num_edges()))
522
- 28352
523
- >>> print((len(list(chart.parses(grammar.start())))))
524
- 17
525
-
526
-
527
- Unit tests for the Probabilistic CFG class
528
- ------------------------------------------
529
-
530
- >>> from nltk.corpus import treebank
531
- >>> from itertools import islice
532
- >>> from nltk.grammar import PCFG, induce_pcfg, toy_pcfg1, toy_pcfg2
533
-
534
- Create a set of PCFG productions.
535
-
536
- >>> grammar = PCFG.fromstring("""
537
- ... A -> B B [.3] | C B C [.7]
538
- ... B -> B D [.5] | C [.5]
539
- ... C -> 'a' [.1] | 'b' [0.9]
540
- ... D -> 'b' [1.0]
541
- ... """)
542
- >>> prod = grammar.productions()[0]
543
- >>> prod
544
- A -> B B [0.3]
545
-
546
- >>> prod.lhs()
547
- A
548
-
549
- >>> prod.rhs()
550
- (B, B)
551
-
552
- >>> print((prod.prob()))
553
- 0.3
554
-
555
- >>> grammar.start()
556
- A
557
-
558
- >>> grammar.productions()
559
- [A -> B B [0.3], A -> C B C [0.7], B -> B D [0.5], B -> C [0.5], C -> 'a' [0.1], C -> 'b' [0.9], D -> 'b' [1.0]]
560
-
561
- Induce some productions using parsed Treebank data.
562
-
563
- >>> productions = []
564
- >>> for fileid in treebank.fileids()[:2]:
565
- ... for t in treebank.parsed_sents(fileid):
566
- ... productions += t.productions()
567
-
568
- >>> grammar = induce_pcfg(S, productions)
569
- >>> grammar
570
- <Grammar with 71 productions>
571
-
572
- >>> sorted(grammar.productions(lhs=Nonterminal('PP')))[:2]
573
- [PP -> IN NP [1.0]]
574
- >>> sorted(grammar.productions(lhs=Nonterminal('NNP')))[:2]
575
- [NNP -> 'Agnew' [0.0714286], NNP -> 'Consolidated' [0.0714286]]
576
- >>> sorted(grammar.productions(lhs=Nonterminal('JJ')))[:2]
577
- [JJ -> 'British' [0.142857], JJ -> 'former' [0.142857]]
578
- >>> sorted(grammar.productions(lhs=Nonterminal('NP')))[:2]
579
- [NP -> CD NNS [0.133333], NP -> DT JJ JJ NN [0.0666667]]
580
-
581
- Unit tests for the Probabilistic Chart Parse classes
582
- ----------------------------------------------------
583
-
584
- >>> tokens = "Jack saw Bob with my cookie".split()
585
- >>> grammar = toy_pcfg2
586
- >>> print(grammar)
587
- Grammar with 23 productions (start state = S)
588
- S -> NP VP [1.0]
589
- VP -> V NP [0.59]
590
- VP -> V [0.4]
591
- VP -> VP PP [0.01]
592
- NP -> Det N [0.41]
593
- NP -> Name [0.28]
594
- NP -> NP PP [0.31]
595
- PP -> P NP [1.0]
596
- V -> 'saw' [0.21]
597
- V -> 'ate' [0.51]
598
- V -> 'ran' [0.28]
599
- N -> 'boy' [0.11]
600
- N -> 'cookie' [0.12]
601
- N -> 'table' [0.13]
602
- N -> 'telescope' [0.14]
603
- N -> 'hill' [0.5]
604
- Name -> 'Jack' [0.52]
605
- Name -> 'Bob' [0.48]
606
- P -> 'with' [0.61]
607
- P -> 'under' [0.39]
608
- Det -> 'the' [0.41]
609
- Det -> 'a' [0.31]
610
- Det -> 'my' [0.28]
611
-
612
- Create several parsers using different queuing strategies and show the
613
- resulting parses.
614
-
615
- >>> from nltk.parse import pchart
616
-
617
- >>> parser = pchart.InsideChartParser(grammar)
618
- >>> for t in parser.parse(tokens):
619
- ... print(t)
620
- (S
621
- (NP (Name Jack))
622
- (VP
623
- (V saw)
624
- (NP
625
- (NP (Name Bob))
626
- (PP (P with) (NP (Det my) (N cookie)))))) (p=6.31607e-06)
627
- (S
628
- (NP (Name Jack))
629
- (VP
630
- (VP (V saw) (NP (Name Bob)))
631
- (PP (P with) (NP (Det my) (N cookie))))) (p=2.03744e-07)
632
-
633
- >>> parser = pchart.RandomChartParser(grammar)
634
- >>> for t in parser.parse(tokens):
635
- ... print(t)
636
- (S
637
- (NP (Name Jack))
638
- (VP
639
- (V saw)
640
- (NP
641
- (NP (Name Bob))
642
- (PP (P with) (NP (Det my) (N cookie)))))) (p=6.31607e-06)
643
- (S
644
- (NP (Name Jack))
645
- (VP
646
- (VP (V saw) (NP (Name Bob)))
647
- (PP (P with) (NP (Det my) (N cookie))))) (p=2.03744e-07)
648
-
649
- >>> parser = pchart.UnsortedChartParser(grammar)
650
- >>> for t in parser.parse(tokens):
651
- ... print(t)
652
- (S
653
- (NP (Name Jack))
654
- (VP
655
- (V saw)
656
- (NP
657
- (NP (Name Bob))
658
- (PP (P with) (NP (Det my) (N cookie)))))) (p=6.31607e-06)
659
- (S
660
- (NP (Name Jack))
661
- (VP
662
- (VP (V saw) (NP (Name Bob)))
663
- (PP (P with) (NP (Det my) (N cookie))))) (p=2.03744e-07)
664
-
665
- >>> parser = pchart.LongestChartParser(grammar)
666
- >>> for t in parser.parse(tokens):
667
- ... print(t)
668
- (S
669
- (NP (Name Jack))
670
- (VP
671
- (V saw)
672
- (NP
673
- (NP (Name Bob))
674
- (PP (P with) (NP (Det my) (N cookie)))))) (p=6.31607e-06)
675
- (S
676
- (NP (Name Jack))
677
- (VP
678
- (VP (V saw) (NP (Name Bob)))
679
- (PP (P with) (NP (Det my) (N cookie))))) (p=2.03744e-07)
680
-
681
- >>> parser = pchart.InsideChartParser(grammar, beam_size = len(tokens)+1)
682
- >>> for t in parser.parse(tokens):
683
- ... print(t)
684
-
685
-
686
- Unit tests for the Viterbi Parse classes
687
- ----------------------------------------
688
-
689
- >>> from nltk.parse import ViterbiParser
690
- >>> tokens = "Jack saw Bob with my cookie".split()
691
- >>> grammar = toy_pcfg2
692
-
693
- Parse the tokenized sentence.
694
-
695
- >>> parser = ViterbiParser(grammar)
696
- >>> for t in parser.parse(tokens):
697
- ... print(t)
698
- (S
699
- (NP (Name Jack))
700
- (VP
701
- (V saw)
702
- (NP
703
- (NP (Name Bob))
704
- (PP (P with) (NP (Det my) (N cookie)))))) (p=6.31607e-06)
705
-
706
-
707
- Unit tests for the FeatStructNonterminal class
708
- ----------------------------------------------
709
-
710
- >>> from nltk.grammar import FeatStructNonterminal
711
- >>> FeatStructNonterminal(
712
- ... pos='n', agr=FeatStructNonterminal(number='pl', gender='f'))
713
- [agr=[gender='f', number='pl'], pos='n']
714
-
715
- >>> FeatStructNonterminal('VP[+fin]/NP[+pl]')
716
- VP[+fin]/NP[+pl]
717
-
718
-
719
- Tracing the Feature Chart Parser
720
- --------------------------------
721
-
722
- We use the featurechart.demo() function for tracing the Feature Chart Parser.
723
-
724
- >>> nltk.parse.featurechart.demo(print_times=False,
725
- ... print_grammar=True,
726
- ... parser=nltk.parse.featurechart.FeatureChartParser,
727
- ... sent='I saw John with a dog')
728
- <BLANKLINE>
729
- Grammar with 18 productions (start state = S[])
730
- S[] -> NP[] VP[]
731
- PP[] -> Prep[] NP[]
732
- NP[] -> NP[] PP[]
733
- VP[] -> VP[] PP[]
734
- VP[] -> Verb[] NP[]
735
- VP[] -> Verb[]
736
- NP[] -> Det[pl=?x] Noun[pl=?x]
737
- NP[] -> 'John'
738
- NP[] -> 'I'
739
- Det[] -> 'the'
740
- Det[] -> 'my'
741
- Det[-pl] -> 'a'
742
- Noun[-pl] -> 'dog'
743
- Noun[-pl] -> 'cookie'
744
- Verb[] -> 'ate'
745
- Verb[] -> 'saw'
746
- Prep[] -> 'with'
747
- Prep[] -> 'under'
748
- <BLANKLINE>
749
- * FeatureChartParser
750
- Sentence: I saw John with a dog
751
- |.I.s.J.w.a.d.|
752
- |[-] . . . . .| [0:1] 'I'
753
- |. [-] . . . .| [1:2] 'saw'
754
- |. . [-] . . .| [2:3] 'John'
755
- |. . . [-] . .| [3:4] 'with'
756
- |. . . . [-] .| [4:5] 'a'
757
- |. . . . . [-]| [5:6] 'dog'
758
- |[-] . . . . .| [0:1] NP[] -> 'I' *
759
- |[-> . . . . .| [0:1] S[] -> NP[] * VP[] {}
760
- |[-> . . . . .| [0:1] NP[] -> NP[] * PP[] {}
761
- |. [-] . . . .| [1:2] Verb[] -> 'saw' *
762
- |. [-> . . . .| [1:2] VP[] -> Verb[] * NP[] {}
763
- |. [-] . . . .| [1:2] VP[] -> Verb[] *
764
- |. [-> . . . .| [1:2] VP[] -> VP[] * PP[] {}
765
- |[---] . . . .| [0:2] S[] -> NP[] VP[] *
766
- |. . [-] . . .| [2:3] NP[] -> 'John' *
767
- |. . [-> . . .| [2:3] S[] -> NP[] * VP[] {}
768
- |. . [-> . . .| [2:3] NP[] -> NP[] * PP[] {}
769
- |. [---] . . .| [1:3] VP[] -> Verb[] NP[] *
770
- |. [---> . . .| [1:3] VP[] -> VP[] * PP[] {}
771
- |[-----] . . .| [0:3] S[] -> NP[] VP[] *
772
- |. . . [-] . .| [3:4] Prep[] -> 'with' *
773
- |. . . [-> . .| [3:4] PP[] -> Prep[] * NP[] {}
774
- |. . . . [-] .| [4:5] Det[-pl] -> 'a' *
775
- |. . . . [-> .| [4:5] NP[] -> Det[pl=?x] * Noun[pl=?x] {?x: False}
776
- |. . . . . [-]| [5:6] Noun[-pl] -> 'dog' *
777
- |. . . . [---]| [4:6] NP[] -> Det[-pl] Noun[-pl] *
778
- |. . . . [--->| [4:6] S[] -> NP[] * VP[] {}
779
- |. . . . [--->| [4:6] NP[] -> NP[] * PP[] {}
780
- |. . . [-----]| [3:6] PP[] -> Prep[] NP[] *
781
- |. . [-------]| [2:6] NP[] -> NP[] PP[] *
782
- |. [---------]| [1:6] VP[] -> VP[] PP[] *
783
- |. [--------->| [1:6] VP[] -> VP[] * PP[] {}
784
- |[===========]| [0:6] S[] -> NP[] VP[] *
785
- |. . [------->| [2:6] S[] -> NP[] * VP[] {}
786
- |. . [------->| [2:6] NP[] -> NP[] * PP[] {}
787
- |. [---------]| [1:6] VP[] -> Verb[] NP[] *
788
- |. [--------->| [1:6] VP[] -> VP[] * PP[] {}
789
- |[===========]| [0:6] S[] -> NP[] VP[] *
790
- (S[]
791
- (NP[] I)
792
- (VP[]
793
- (VP[] (Verb[] saw) (NP[] John))
794
- (PP[] (Prep[] with) (NP[] (Det[-pl] a) (Noun[-pl] dog)))))
795
- (S[]
796
- (NP[] I)
797
- (VP[]
798
- (Verb[] saw)
799
- (NP[]
800
- (NP[] John)
801
- (PP[] (Prep[] with) (NP[] (Det[-pl] a) (Noun[-pl] dog))))))
802
-
803
-
804
- Unit tests for the Feature Chart Parser classes
805
- -----------------------------------------------
806
-
807
- The list of parsers we want to test.
808
-
809
- >>> parsers = [nltk.parse.featurechart.FeatureChartParser,
810
- ... nltk.parse.featurechart.FeatureTopDownChartParser,
811
- ... nltk.parse.featurechart.FeatureBottomUpChartParser,
812
- ... nltk.parse.featurechart.FeatureBottomUpLeftCornerChartParser,
813
- ... nltk.parse.earleychart.FeatureIncrementalChartParser,
814
- ... nltk.parse.earleychart.FeatureEarleyChartParser,
815
- ... nltk.parse.earleychart.FeatureIncrementalTopDownChartParser,
816
- ... nltk.parse.earleychart.FeatureIncrementalBottomUpChartParser,
817
- ... nltk.parse.earleychart.FeatureIncrementalBottomUpLeftCornerChartParser,
818
- ... ]
819
-
820
- A helper function that tests each parser on the given grammar and sentence.
821
- We check that the number of trees are correct, and that all parsers
822
- return the same trees. Otherwise an error is printed.
823
-
824
- >>> def unittest(grammar, sentence, nr_trees):
825
- ... sentence = sentence.split()
826
- ... trees = None
827
- ... for P in parsers:
828
- ... result = P(grammar).parse(sentence)
829
- ... result = set(tree.freeze() for tree in result)
830
- ... if len(result) != nr_trees:
831
- ... print("Wrong nr of trees:", len(result))
832
- ... elif trees is None:
833
- ... trees = result
834
- ... elif result != trees:
835
- ... print("Trees differ for parser:", P.__name__)
836
-
837
- The demo grammar from before, with an ambiguous sentence.
838
-
839
- >>> isawjohn = nltk.parse.featurechart.demo_grammar()
840
- >>> unittest(isawjohn, "I saw John with a dog with my cookie", 5)
841
-
842
- This grammar tests that variables in different grammar rules are renamed
843
- before unification. (The problematic variable is in this case ?X).
844
-
845
- >>> whatwasthat = nltk.grammar.FeatureGrammar.fromstring('''
846
- ... S[] -> NP[num=?N] VP[num=?N, slash=?X]
847
- ... NP[num=?X] -> "what"
848
- ... NP[num=?X] -> "that"
849
- ... VP[num=?P, slash=none] -> V[num=?P] NP[]
850
- ... V[num=sg] -> "was"
851
- ... ''')
852
- >>> unittest(whatwasthat, "what was that", 1)
853
-
854
- This grammar tests that the same rule can be used in different places
855
- in another rule, and that the variables are properly renamed.
856
-
857
- >>> thislovesthat = nltk.grammar.FeatureGrammar.fromstring('''
858
- ... S[] -> NP[case=nom] V[] NP[case=acc]
859
- ... NP[case=?X] -> Pron[case=?X]
860
- ... Pron[] -> "this"
861
- ... Pron[] -> "that"
862
- ... V[] -> "loves"
863
- ... ''')
864
- >>> unittest(thislovesthat, "this loves that", 1)
865
-
866
-
867
- Tests for loading feature grammar files
868
- ---------------------------------------
869
-
870
- Alternative 1: first load the grammar, then create the parser.
871
-
872
- >>> fcfg = nltk.data.load('grammars/book_grammars/feat0.fcfg')
873
- >>> fcp1 = nltk.parse.FeatureChartParser(fcfg)
874
- >>> print((type(fcp1)))
875
- <class 'nltk.parse.featurechart.FeatureChartParser'>
876
-
877
- Alternative 2: directly load the parser.
878
-
879
- >>> fcp2 = nltk.parse.load_parser('grammars/book_grammars/feat0.fcfg')
880
- >>> print((type(fcp2)))
881
- <class 'nltk.parse.featurechart.FeatureChartParser'>
882
-
883
-
884
-
1
+ .. Copyright (C) 2001-2018 NLTK Project
2
+ .. For license information, see LICENSE.TXT
3
+
4
+ =========
5
+ Parsing
6
+ =========
7
+
8
+ Unit tests for the Context Free Grammar class
9
+ ---------------------------------------------
10
+
11
+ >>> from nltk import Nonterminal, nonterminals, Production, CFG
12
+
13
+ >>> nt1 = Nonterminal('NP')
14
+ >>> nt2 = Nonterminal('VP')
15
+
16
+ >>> nt1.symbol()
17
+ 'NP'
18
+
19
+ >>> nt1 == Nonterminal('NP')
20
+ True
21
+
22
+ >>> nt1 == nt2
23
+ False
24
+
25
+ >>> S, NP, VP, PP = nonterminals('S, NP, VP, PP')
26
+ >>> N, V, P, DT = nonterminals('N, V, P, DT')
27
+
28
+ >>> prod1 = Production(S, [NP, VP])
29
+ >>> prod2 = Production(NP, [DT, NP])
30
+
31
+ >>> prod1.lhs()
32
+ S
33
+
34
+ >>> prod1.rhs()
35
+ (NP, VP)
36
+
37
+ >>> prod1 == Production(S, [NP, VP])
38
+ True
39
+
40
+ >>> prod1 == prod2
41
+ False
42
+
43
+ >>> grammar = CFG.fromstring("""
44
+ ... S -> NP VP
45
+ ... PP -> P NP
46
+ ... NP -> 'the' N | N PP | 'the' N PP
47
+ ... VP -> V NP | V PP | V NP PP
48
+ ... N -> 'cat'
49
+ ... N -> 'dog'
50
+ ... N -> 'rug'
51
+ ... V -> 'chased'
52
+ ... V -> 'sat'
53
+ ... P -> 'in'
54
+ ... P -> 'on'
55
+ ... """)
56
+
57
+ Unit tests for the rd (Recursive Descent Parser) class
58
+ ------------------------------------------------------
59
+
60
+ Create and run a recursive descent parser over both a syntactically ambiguous
61
+ and unambiguous sentence.
62
+
63
+ >>> from nltk.parse import RecursiveDescentParser
64
+ >>> rd = RecursiveDescentParser(grammar)
65
+
66
+ >>> sentence1 = 'the cat chased the dog'.split()
67
+ >>> sentence2 = 'the cat chased the dog on the rug'.split()
68
+
69
+ >>> for t in rd.parse(sentence1):
70
+ ... print(t)
71
+ (S (NP the (N cat)) (VP (V chased) (NP the (N dog))))
72
+
73
+ >>> for t in rd.parse(sentence2):
74
+ ... print(t)
75
+ (S
76
+ (NP the (N cat))
77
+ (VP (V chased) (NP the (N dog) (PP (P on) (NP the (N rug))))))
78
+ (S
79
+ (NP the (N cat))
80
+ (VP (V chased) (NP the (N dog)) (PP (P on) (NP the (N rug)))))
81
+
82
+
83
+ (dolist (expr doctest-font-lock-keywords)
84
+ (add-to-list 'font-lock-keywords expr))
85
+
86
+ font-lock-keywords
87
+ (add-to-list 'font-lock-keywords
88
+ (car doctest-font-lock-keywords))
89
+
90
+
91
+ Unit tests for the sr (Shift Reduce Parser) class
92
+ -------------------------------------------------
93
+
94
+ Create and run a shift reduce parser over both a syntactically ambiguous
95
+ and unambiguous sentence. Note that unlike the recursive descent parser, one
96
+ and only one parse is ever returned.
97
+
98
+ >>> from nltk.parse import ShiftReduceParser
99
+ >>> sr = ShiftReduceParser(grammar)
100
+
101
+ >>> sentence1 = 'the cat chased the dog'.split()
102
+ >>> sentence2 = 'the cat chased the dog on the rug'.split()
103
+
104
+ >>> for t in sr.parse(sentence1):
105
+ ... print(t)
106
+ (S (NP the (N cat)) (VP (V chased) (NP the (N dog))))
107
+
108
+
109
+ The shift reduce parser uses heuristics to decide what to do when there are
110
+ multiple possible shift or reduce operations available - for the supplied
111
+ grammar clearly the wrong operation is selected.
112
+
113
+ >>> for t in sr.parse(sentence2):
114
+ ... print(t)
115
+
116
+
117
+ Unit tests for the Chart Parser class
118
+ -------------------------------------
119
+
120
+ We use the demo() function for testing.
121
+ We must turn off showing of times.
122
+
123
+ >>> import nltk
124
+
125
+ First we test tracing with a short sentence
126
+
127
+ >>> nltk.parse.chart.demo(2, print_times=False, trace=1,
128
+ ... sent='I saw a dog', numparses=1)
129
+ * Sentence:
130
+ I saw a dog
131
+ ['I', 'saw', 'a', 'dog']
132
+ <BLANKLINE>
133
+ * Strategy: Bottom-up
134
+ <BLANKLINE>
135
+ |. I . saw . a . dog .|
136
+ |[---------] . . .| [0:1] 'I'
137
+ |. [---------] . .| [1:2] 'saw'
138
+ |. . [---------] .| [2:3] 'a'
139
+ |. . . [---------]| [3:4] 'dog'
140
+ |> . . . .| [0:0] NP -> * 'I'
141
+ |[---------] . . .| [0:1] NP -> 'I' *
142
+ |> . . . .| [0:0] S -> * NP VP
143
+ |> . . . .| [0:0] NP -> * NP PP
144
+ |[---------> . . .| [0:1] S -> NP * VP
145
+ |[---------> . . .| [0:1] NP -> NP * PP
146
+ |. > . . .| [1:1] Verb -> * 'saw'
147
+ |. [---------] . .| [1:2] Verb -> 'saw' *
148
+ |. > . . .| [1:1] VP -> * Verb NP
149
+ |. > . . .| [1:1] VP -> * Verb
150
+ |. [---------> . .| [1:2] VP -> Verb * NP
151
+ |. [---------] . .| [1:2] VP -> Verb *
152
+ |. > . . .| [1:1] VP -> * VP PP
153
+ |[-------------------] . .| [0:2] S -> NP VP *
154
+ |. [---------> . .| [1:2] VP -> VP * PP
155
+ |. . > . .| [2:2] Det -> * 'a'
156
+ |. . [---------] .| [2:3] Det -> 'a' *
157
+ |. . > . .| [2:2] NP -> * Det Noun
158
+ |. . [---------> .| [2:3] NP -> Det * Noun
159
+ |. . . > .| [3:3] Noun -> * 'dog'
160
+ |. . . [---------]| [3:4] Noun -> 'dog' *
161
+ |. . [-------------------]| [2:4] NP -> Det Noun *
162
+ |. . > . .| [2:2] S -> * NP VP
163
+ |. . > . .| [2:2] NP -> * NP PP
164
+ |. [-----------------------------]| [1:4] VP -> Verb NP *
165
+ |. . [------------------->| [2:4] S -> NP * VP
166
+ |. . [------------------->| [2:4] NP -> NP * PP
167
+ |[=======================================]| [0:4] S -> NP VP *
168
+ |. [----------------------------->| [1:4] VP -> VP * PP
169
+ Nr edges in chart: 33
170
+ (S (NP I) (VP (Verb saw) (NP (Det a) (Noun dog))))
171
+ <BLANKLINE>
172
+
173
+ Then we test the different parsing Strategies.
174
+ Note that the number of edges differ between the strategies.
175
+
176
+ Top-down
177
+
178
+ >>> nltk.parse.chart.demo(1, print_times=False, trace=0,
179
+ ... sent='I saw John with a dog', numparses=2)
180
+ * Sentence:
181
+ I saw John with a dog
182
+ ['I', 'saw', 'John', 'with', 'a', 'dog']
183
+ <BLANKLINE>
184
+ * Strategy: Top-down
185
+ <BLANKLINE>
186
+ Nr edges in chart: 48
187
+ (S
188
+ (NP I)
189
+ (VP (Verb saw) (NP (NP John) (PP with (NP (Det a) (Noun dog))))))
190
+ (S
191
+ (NP I)
192
+ (VP (VP (Verb saw) (NP John)) (PP with (NP (Det a) (Noun dog)))))
193
+ <BLANKLINE>
194
+
195
+ Bottom-up
196
+
197
+ >>> nltk.parse.chart.demo(2, print_times=False, trace=0,
198
+ ... sent='I saw John with a dog', numparses=2)
199
+ * Sentence:
200
+ I saw John with a dog
201
+ ['I', 'saw', 'John', 'with', 'a', 'dog']
202
+ <BLANKLINE>
203
+ * Strategy: Bottom-up
204
+ <BLANKLINE>
205
+ Nr edges in chart: 53
206
+ (S
207
+ (NP I)
208
+ (VP (VP (Verb saw) (NP John)) (PP with (NP (Det a) (Noun dog)))))
209
+ (S
210
+ (NP I)
211
+ (VP (Verb saw) (NP (NP John) (PP with (NP (Det a) (Noun dog))))))
212
+ <BLANKLINE>
213
+
214
+ Bottom-up Left-Corner
215
+
216
+ >>> nltk.parse.chart.demo(3, print_times=False, trace=0,
217
+ ... sent='I saw John with a dog', numparses=2)
218
+ * Sentence:
219
+ I saw John with a dog
220
+ ['I', 'saw', 'John', 'with', 'a', 'dog']
221
+ <BLANKLINE>
222
+ * Strategy: Bottom-up left-corner
223
+ <BLANKLINE>
224
+ Nr edges in chart: 36
225
+ (S
226
+ (NP I)
227
+ (VP (VP (Verb saw) (NP John)) (PP with (NP (Det a) (Noun dog)))))
228
+ (S
229
+ (NP I)
230
+ (VP (Verb saw) (NP (NP John) (PP with (NP (Det a) (Noun dog))))))
231
+ <BLANKLINE>
232
+
233
+ Left-Corner with Bottom-Up Filter
234
+
235
+ >>> nltk.parse.chart.demo(4, print_times=False, trace=0,
236
+ ... sent='I saw John with a dog', numparses=2)
237
+ * Sentence:
238
+ I saw John with a dog
239
+ ['I', 'saw', 'John', 'with', 'a', 'dog']
240
+ <BLANKLINE>
241
+ * Strategy: Filtered left-corner
242
+ <BLANKLINE>
243
+ Nr edges in chart: 28
244
+ (S
245
+ (NP I)
246
+ (VP (VP (Verb saw) (NP John)) (PP with (NP (Det a) (Noun dog)))))
247
+ (S
248
+ (NP I)
249
+ (VP (Verb saw) (NP (NP John) (PP with (NP (Det a) (Noun dog))))))
250
+ <BLANKLINE>
251
+
252
+ The stepping chart parser
253
+
254
+ >>> nltk.parse.chart.demo(5, print_times=False, trace=1,
255
+ ... sent='I saw John with a dog', numparses=2)
256
+ * Sentence:
257
+ I saw John with a dog
258
+ ['I', 'saw', 'John', 'with', 'a', 'dog']
259
+ <BLANKLINE>
260
+ * Strategy: Stepping (top-down vs bottom-up)
261
+ <BLANKLINE>
262
+ *** SWITCH TO TOP DOWN
263
+ |[------] . . . . .| [0:1] 'I'
264
+ |. [------] . . . .| [1:2] 'saw'
265
+ |. . [------] . . .| [2:3] 'John'
266
+ |. . . [------] . .| [3:4] 'with'
267
+ |. . . . [------] .| [4:5] 'a'
268
+ |. . . . . [------]| [5:6] 'dog'
269
+ |> . . . . . .| [0:0] S -> * NP VP
270
+ |> . . . . . .| [0:0] NP -> * NP PP
271
+ |> . . . . . .| [0:0] NP -> * Det Noun
272
+ |> . . . . . .| [0:0] NP -> * 'I'
273
+ |[------] . . . . .| [0:1] NP -> 'I' *
274
+ |[------> . . . . .| [0:1] S -> NP * VP
275
+ |[------> . . . . .| [0:1] NP -> NP * PP
276
+ |. > . . . . .| [1:1] VP -> * VP PP
277
+ |. > . . . . .| [1:1] VP -> * Verb NP
278
+ |. > . . . . .| [1:1] VP -> * Verb
279
+ |. > . . . . .| [1:1] Verb -> * 'saw'
280
+ |. [------] . . . .| [1:2] Verb -> 'saw' *
281
+ |. [------> . . . .| [1:2] VP -> Verb * NP
282
+ |. [------] . . . .| [1:2] VP -> Verb *
283
+ |[-------------] . . . .| [0:2] S -> NP VP *
284
+ |. [------> . . . .| [1:2] VP -> VP * PP
285
+ *** SWITCH TO BOTTOM UP
286
+ |. . > . . . .| [2:2] NP -> * 'John'
287
+ |. . . > . . .| [3:3] PP -> * 'with' NP
288
+ |. . . > . . .| [3:3] Prep -> * 'with'
289
+ |. . . . > . .| [4:4] Det -> * 'a'
290
+ |. . . . . > .| [5:5] Noun -> * 'dog'
291
+ |. . [------] . . .| [2:3] NP -> 'John' *
292
+ |. . . [------> . .| [3:4] PP -> 'with' * NP
293
+ |. . . [------] . .| [3:4] Prep -> 'with' *
294
+ |. . . . [------] .| [4:5] Det -> 'a' *
295
+ |. . . . . [------]| [5:6] Noun -> 'dog' *
296
+ |. [-------------] . . .| [1:3] VP -> Verb NP *
297
+ |[--------------------] . . .| [0:3] S -> NP VP *
298
+ |. [-------------> . . .| [1:3] VP -> VP * PP
299
+ |. . > . . . .| [2:2] S -> * NP VP
300
+ |. . > . . . .| [2:2] NP -> * NP PP
301
+ |. . . . > . .| [4:4] NP -> * Det Noun
302
+ |. . [------> . . .| [2:3] S -> NP * VP
303
+ |. . [------> . . .| [2:3] NP -> NP * PP
304
+ |. . . . [------> .| [4:5] NP -> Det * Noun
305
+ |. . . . [-------------]| [4:6] NP -> Det Noun *
306
+ |. . . [--------------------]| [3:6] PP -> 'with' NP *
307
+ |. [----------------------------------]| [1:6] VP -> VP PP *
308
+ *** SWITCH TO TOP DOWN
309
+ |. . > . . . .| [2:2] NP -> * Det Noun
310
+ |. . . . > . .| [4:4] NP -> * NP PP
311
+ |. . . > . . .| [3:3] VP -> * VP PP
312
+ |. . . > . . .| [3:3] VP -> * Verb NP
313
+ |. . . > . . .| [3:3] VP -> * Verb
314
+ |[=========================================]| [0:6] S -> NP VP *
315
+ |. [---------------------------------->| [1:6] VP -> VP * PP
316
+ |. . [---------------------------]| [2:6] NP -> NP PP *
317
+ |. . . . [------------->| [4:6] NP -> NP * PP
318
+ |. [----------------------------------]| [1:6] VP -> Verb NP *
319
+ |. . [--------------------------->| [2:6] S -> NP * VP
320
+ |. . [--------------------------->| [2:6] NP -> NP * PP
321
+ |[=========================================]| [0:6] S -> NP VP *
322
+ |. [---------------------------------->| [1:6] VP -> VP * PP
323
+ |. . . . . . >| [6:6] VP -> * VP PP
324
+ |. . . . . . >| [6:6] VP -> * Verb NP
325
+ |. . . . . . >| [6:6] VP -> * Verb
326
+ *** SWITCH TO BOTTOM UP
327
+ |. . . . > . .| [4:4] S -> * NP VP
328
+ |. . . . [------------->| [4:6] S -> NP * VP
329
+ *** SWITCH TO TOP DOWN
330
+ *** SWITCH TO BOTTOM UP
331
+ *** SWITCH TO TOP DOWN
332
+ *** SWITCH TO BOTTOM UP
333
+ *** SWITCH TO TOP DOWN
334
+ *** SWITCH TO BOTTOM UP
335
+ Nr edges in chart: 61
336
+ (S
337
+ (NP I)
338
+ (VP (VP (Verb saw) (NP John)) (PP with (NP (Det a) (Noun dog)))))
339
+ (S
340
+ (NP I)
341
+ (VP (Verb saw) (NP (NP John) (PP with (NP (Det a) (Noun dog))))))
342
+ <BLANKLINE>
343
+
344
+
345
+ Unit tests for the Incremental Chart Parser class
346
+ -------------------------------------------------
347
+
348
+ The incremental chart parsers are defined in earleychart.py.
349
+ We use the demo() function for testing. We must turn off showing of times.
350
+
351
+ >>> import nltk
352
+
353
+ Earley Chart Parser
354
+
355
+ >>> nltk.parse.earleychart.demo(print_times=False, trace=1,
356
+ ... sent='I saw John with a dog', numparses=2)
357
+ * Sentence:
358
+ I saw John with a dog
359
+ ['I', 'saw', 'John', 'with', 'a', 'dog']
360
+ <BLANKLINE>
361
+ |. I . saw . John . with . a . dog .|
362
+ |[------] . . . . .| [0:1] 'I'
363
+ |. [------] . . . .| [1:2] 'saw'
364
+ |. . [------] . . .| [2:3] 'John'
365
+ |. . . [------] . .| [3:4] 'with'
366
+ |. . . . [------] .| [4:5] 'a'
367
+ |. . . . . [------]| [5:6] 'dog'
368
+ |> . . . . . .| [0:0] S -> * NP VP
369
+ |> . . . . . .| [0:0] NP -> * NP PP
370
+ |> . . . . . .| [0:0] NP -> * Det Noun
371
+ |> . . . . . .| [0:0] NP -> * 'I'
372
+ |[------] . . . . .| [0:1] NP -> 'I' *
373
+ |[------> . . . . .| [0:1] S -> NP * VP
374
+ |[------> . . . . .| [0:1] NP -> NP * PP
375
+ |. > . . . . .| [1:1] VP -> * VP PP
376
+ |. > . . . . .| [1:1] VP -> * Verb NP
377
+ |. > . . . . .| [1:1] VP -> * Verb
378
+ |. > . . . . .| [1:1] Verb -> * 'saw'
379
+ |. [------] . . . .| [1:2] Verb -> 'saw' *
380
+ |. [------> . . . .| [1:2] VP -> Verb * NP
381
+ |. [------] . . . .| [1:2] VP -> Verb *
382
+ |[-------------] . . . .| [0:2] S -> NP VP *
383
+ |. [------> . . . .| [1:2] VP -> VP * PP
384
+ |. . > . . . .| [2:2] NP -> * NP PP
385
+ |. . > . . . .| [2:2] NP -> * Det Noun
386
+ |. . > . . . .| [2:2] NP -> * 'John'
387
+ |. . [------] . . .| [2:3] NP -> 'John' *
388
+ |. [-------------] . . .| [1:3] VP -> Verb NP *
389
+ |. . [------> . . .| [2:3] NP -> NP * PP
390
+ |. . . > . . .| [3:3] PP -> * 'with' NP
391
+ |[--------------------] . . .| [0:3] S -> NP VP *
392
+ |. [-------------> . . .| [1:3] VP -> VP * PP
393
+ |. . . [------> . .| [3:4] PP -> 'with' * NP
394
+ |. . . . > . .| [4:4] NP -> * NP PP
395
+ |. . . . > . .| [4:4] NP -> * Det Noun
396
+ |. . . . > . .| [4:4] Det -> * 'a'
397
+ |. . . . [------] .| [4:5] Det -> 'a' *
398
+ |. . . . [------> .| [4:5] NP -> Det * Noun
399
+ |. . . . . > .| [5:5] Noun -> * 'dog'
400
+ |. . . . . [------]| [5:6] Noun -> 'dog' *
401
+ |. . . . [-------------]| [4:6] NP -> Det Noun *
402
+ |. . . [--------------------]| [3:6] PP -> 'with' NP *
403
+ |. . . . [------------->| [4:6] NP -> NP * PP
404
+ |. . [---------------------------]| [2:6] NP -> NP PP *
405
+ |. [----------------------------------]| [1:6] VP -> VP PP *
406
+ |[=========================================]| [0:6] S -> NP VP *
407
+ |. [---------------------------------->| [1:6] VP -> VP * PP
408
+ |. [----------------------------------]| [1:6] VP -> Verb NP *
409
+ |. . [--------------------------->| [2:6] NP -> NP * PP
410
+ |[=========================================]| [0:6] S -> NP VP *
411
+ |. [---------------------------------->| [1:6] VP -> VP * PP
412
+ (S
413
+ (NP I)
414
+ (VP (VP (Verb saw) (NP John)) (PP with (NP (Det a) (Noun dog)))))
415
+ (S
416
+ (NP I)
417
+ (VP (Verb saw) (NP (NP John) (PP with (NP (Det a) (Noun dog))))))
418
+
419
+
420
+ Unit tests for LARGE context-free grammars
421
+ ------------------------------------------
422
+
423
+ Reading the ATIS grammar.
424
+
425
+ >>> grammar = nltk.data.load('grammars/large_grammars/atis.cfg')
426
+ >>> grammar
427
+ <Grammar with 5517 productions>
428
+
429
+ Reading the test sentences.
430
+
431
+ >>> sentences = nltk.data.load('grammars/large_grammars/atis_sentences.txt')
432
+ >>> sentences = nltk.parse.util.extract_test_sentences(sentences)
433
+ >>> len(sentences)
434
+ 98
435
+ >>> testsentence = sentences[22]
436
+ >>> testsentence[0]
437
+ ['show', 'me', 'northwest', 'flights', 'to', 'detroit', '.']
438
+ >>> testsentence[1]
439
+ 17
440
+ >>> sentence = testsentence[0]
441
+
442
+ Now we test all different parsing strategies.
443
+ Note that the number of edges differ between the strategies.
444
+
445
+ Bottom-up parsing.
446
+
447
+ >>> parser = nltk.parse.BottomUpChartParser(grammar)
448
+ >>> chart = parser.chart_parse(sentence)
449
+ >>> print((chart.num_edges()))
450
+ 7661
451
+ >>> print((len(list(chart.parses(grammar.start())))))
452
+ 17
453
+
454
+ Bottom-up Left-corner parsing.
455
+
456
+ >>> parser = nltk.parse.BottomUpLeftCornerChartParser(grammar)
457
+ >>> chart = parser.chart_parse(sentence)
458
+ >>> print((chart.num_edges()))
459
+ 4986
460
+ >>> print((len(list(chart.parses(grammar.start())))))
461
+ 17
462
+
463
+ Left-corner parsing with bottom-up filter.
464
+
465
+ >>> parser = nltk.parse.LeftCornerChartParser(grammar)
466
+ >>> chart = parser.chart_parse(sentence)
467
+ >>> print((chart.num_edges()))
468
+ 1342
469
+ >>> print((len(list(chart.parses(grammar.start())))))
470
+ 17
471
+
472
+ Top-down parsing.
473
+
474
+ >>> parser = nltk.parse.TopDownChartParser(grammar)
475
+ >>> chart = parser.chart_parse(sentence)
476
+ >>> print((chart.num_edges()))
477
+ 28352
478
+ >>> print((len(list(chart.parses(grammar.start())))))
479
+ 17
480
+
481
+ Incremental Bottom-up parsing.
482
+
483
+ >>> parser = nltk.parse.IncrementalBottomUpChartParser(grammar)
484
+ >>> chart = parser.chart_parse(sentence)
485
+ >>> print((chart.num_edges()))
486
+ 7661
487
+ >>> print((len(list(chart.parses(grammar.start())))))
488
+ 17
489
+
490
+ Incremental Bottom-up Left-corner parsing.
491
+
492
+ >>> parser = nltk.parse.IncrementalBottomUpLeftCornerChartParser(grammar)
493
+ >>> chart = parser.chart_parse(sentence)
494
+ >>> print((chart.num_edges()))
495
+ 4986
496
+ >>> print((len(list(chart.parses(grammar.start())))))
497
+ 17
498
+
499
+ Incremental Left-corner parsing with bottom-up filter.
500
+
501
+ >>> parser = nltk.parse.IncrementalLeftCornerChartParser(grammar)
502
+ >>> chart = parser.chart_parse(sentence)
503
+ >>> print((chart.num_edges()))
504
+ 1342
505
+ >>> print((len(list(chart.parses(grammar.start())))))
506
+ 17
507
+
508
+ Incremental Top-down parsing.
509
+
510
+ >>> parser = nltk.parse.IncrementalTopDownChartParser(grammar)
511
+ >>> chart = parser.chart_parse(sentence)
512
+ >>> print((chart.num_edges()))
513
+ 28352
514
+ >>> print((len(list(chart.parses(grammar.start())))))
515
+ 17
516
+
517
+ Earley parsing. This is similar to the incremental top-down algorithm.
518
+
519
+ >>> parser = nltk.parse.EarleyChartParser(grammar)
520
+ >>> chart = parser.chart_parse(sentence)
521
+ >>> print((chart.num_edges()))
522
+ 28352
523
+ >>> print((len(list(chart.parses(grammar.start())))))
524
+ 17
525
+
526
+
527
+ Unit tests for the Probabilistic CFG class
528
+ ------------------------------------------
529
+
530
+ >>> from nltk.corpus import treebank
531
+ >>> from itertools import islice
532
+ >>> from nltk.grammar import PCFG, induce_pcfg, toy_pcfg1, toy_pcfg2
533
+
534
+ Create a set of PCFG productions.
535
+
536
+ >>> grammar = PCFG.fromstring("""
537
+ ... A -> B B [.3] | C B C [.7]
538
+ ... B -> B D [.5] | C [.5]
539
+ ... C -> 'a' [.1] | 'b' [0.9]
540
+ ... D -> 'b' [1.0]
541
+ ... """)
542
+ >>> prod = grammar.productions()[0]
543
+ >>> prod
544
+ A -> B B [0.3]
545
+
546
+ >>> prod.lhs()
547
+ A
548
+
549
+ >>> prod.rhs()
550
+ (B, B)
551
+
552
+ >>> print((prod.prob()))
553
+ 0.3
554
+
555
+ >>> grammar.start()
556
+ A
557
+
558
+ >>> grammar.productions()
559
+ [A -> B B [0.3], A -> C B C [0.7], B -> B D [0.5], B -> C [0.5], C -> 'a' [0.1], C -> 'b' [0.9], D -> 'b' [1.0]]
560
+
561
+ Induce some productions using parsed Treebank data.
562
+
563
+ >>> productions = []
564
+ >>> for fileid in treebank.fileids()[:2]:
565
+ ... for t in treebank.parsed_sents(fileid):
566
+ ... productions += t.productions()
567
+
568
+ >>> grammar = induce_pcfg(S, productions)
569
+ >>> grammar
570
+ <Grammar with 71 productions>
571
+
572
+ >>> sorted(grammar.productions(lhs=Nonterminal('PP')))[:2]
573
+ [PP -> IN NP [1.0]]
574
+ >>> sorted(grammar.productions(lhs=Nonterminal('NNP')))[:2]
575
+ [NNP -> 'Agnew' [0.0714286], NNP -> 'Consolidated' [0.0714286]]
576
+ >>> sorted(grammar.productions(lhs=Nonterminal('JJ')))[:2]
577
+ [JJ -> 'British' [0.142857], JJ -> 'former' [0.142857]]
578
+ >>> sorted(grammar.productions(lhs=Nonterminal('NP')))[:2]
579
+ [NP -> CD NNS [0.133333], NP -> DT JJ JJ NN [0.0666667]]
580
+
581
+ Unit tests for the Probabilistic Chart Parse classes
582
+ ----------------------------------------------------
583
+
584
+ >>> tokens = "Jack saw Bob with my cookie".split()
585
+ >>> grammar = toy_pcfg2
586
+ >>> print(grammar)
587
+ Grammar with 23 productions (start state = S)
588
+ S -> NP VP [1.0]
589
+ VP -> V NP [0.59]
590
+ VP -> V [0.4]
591
+ VP -> VP PP [0.01]
592
+ NP -> Det N [0.41]
593
+ NP -> Name [0.28]
594
+ NP -> NP PP [0.31]
595
+ PP -> P NP [1.0]
596
+ V -> 'saw' [0.21]
597
+ V -> 'ate' [0.51]
598
+ V -> 'ran' [0.28]
599
+ N -> 'boy' [0.11]
600
+ N -> 'cookie' [0.12]
601
+ N -> 'table' [0.13]
602
+ N -> 'telescope' [0.14]
603
+ N -> 'hill' [0.5]
604
+ Name -> 'Jack' [0.52]
605
+ Name -> 'Bob' [0.48]
606
+ P -> 'with' [0.61]
607
+ P -> 'under' [0.39]
608
+ Det -> 'the' [0.41]
609
+ Det -> 'a' [0.31]
610
+ Det -> 'my' [0.28]
611
+
612
+ Create several parsers using different queuing strategies and show the
613
+ resulting parses.
614
+
615
+ >>> from nltk.parse import pchart
616
+
617
+ >>> parser = pchart.InsideChartParser(grammar)
618
+ >>> for t in parser.parse(tokens):
619
+ ... print(t)
620
+ (S
621
+ (NP (Name Jack))
622
+ (VP
623
+ (V saw)
624
+ (NP
625
+ (NP (Name Bob))
626
+ (PP (P with) (NP (Det my) (N cookie)))))) (p=6.31607e-06)
627
+ (S
628
+ (NP (Name Jack))
629
+ (VP
630
+ (VP (V saw) (NP (Name Bob)))
631
+ (PP (P with) (NP (Det my) (N cookie))))) (p=2.03744e-07)
632
+
633
+ >>> parser = pchart.RandomChartParser(grammar)
634
+ >>> for t in parser.parse(tokens):
635
+ ... print(t)
636
+ (S
637
+ (NP (Name Jack))
638
+ (VP
639
+ (V saw)
640
+ (NP
641
+ (NP (Name Bob))
642
+ (PP (P with) (NP (Det my) (N cookie)))))) (p=6.31607e-06)
643
+ (S
644
+ (NP (Name Jack))
645
+ (VP
646
+ (VP (V saw) (NP (Name Bob)))
647
+ (PP (P with) (NP (Det my) (N cookie))))) (p=2.03744e-07)
648
+
649
+ >>> parser = pchart.UnsortedChartParser(grammar)
650
+ >>> for t in parser.parse(tokens):
651
+ ... print(t)
652
+ (S
653
+ (NP (Name Jack))
654
+ (VP
655
+ (V saw)
656
+ (NP
657
+ (NP (Name Bob))
658
+ (PP (P with) (NP (Det my) (N cookie)))))) (p=6.31607e-06)
659
+ (S
660
+ (NP (Name Jack))
661
+ (VP
662
+ (VP (V saw) (NP (Name Bob)))
663
+ (PP (P with) (NP (Det my) (N cookie))))) (p=2.03744e-07)
664
+
665
+ >>> parser = pchart.LongestChartParser(grammar)
666
+ >>> for t in parser.parse(tokens):
667
+ ... print(t)
668
+ (S
669
+ (NP (Name Jack))
670
+ (VP
671
+ (V saw)
672
+ (NP
673
+ (NP (Name Bob))
674
+ (PP (P with) (NP (Det my) (N cookie)))))) (p=6.31607e-06)
675
+ (S
676
+ (NP (Name Jack))
677
+ (VP
678
+ (VP (V saw) (NP (Name Bob)))
679
+ (PP (P with) (NP (Det my) (N cookie))))) (p=2.03744e-07)
680
+
681
+ >>> parser = pchart.InsideChartParser(grammar, beam_size = len(tokens)+1)
682
+ >>> for t in parser.parse(tokens):
683
+ ... print(t)
684
+
685
+
686
+ Unit tests for the Viterbi Parse classes
687
+ ----------------------------------------
688
+
689
+ >>> from nltk.parse import ViterbiParser
690
+ >>> tokens = "Jack saw Bob with my cookie".split()
691
+ >>> grammar = toy_pcfg2
692
+
693
+ Parse the tokenized sentence.
694
+
695
+ >>> parser = ViterbiParser(grammar)
696
+ >>> for t in parser.parse(tokens):
697
+ ... print(t)
698
+ (S
699
+ (NP (Name Jack))
700
+ (VP
701
+ (V saw)
702
+ (NP
703
+ (NP (Name Bob))
704
+ (PP (P with) (NP (Det my) (N cookie)))))) (p=6.31607e-06)
705
+
706
+
707
+ Unit tests for the FeatStructNonterminal class
708
+ ----------------------------------------------
709
+
710
+ >>> from nltk.grammar import FeatStructNonterminal
711
+ >>> FeatStructNonterminal(
712
+ ... pos='n', agr=FeatStructNonterminal(number='pl', gender='f'))
713
+ [agr=[gender='f', number='pl'], pos='n']
714
+
715
+ >>> FeatStructNonterminal('VP[+fin]/NP[+pl]')
716
+ VP[+fin]/NP[+pl]
717
+
718
+
719
+ Tracing the Feature Chart Parser
720
+ --------------------------------
721
+
722
+ We use the featurechart.demo() function for tracing the Feature Chart Parser.
723
+
724
+ >>> nltk.parse.featurechart.demo(print_times=False,
725
+ ... print_grammar=True,
726
+ ... parser=nltk.parse.featurechart.FeatureChartParser,
727
+ ... sent='I saw John with a dog')
728
+ <BLANKLINE>
729
+ Grammar with 18 productions (start state = S[])
730
+ S[] -> NP[] VP[]
731
+ PP[] -> Prep[] NP[]
732
+ NP[] -> NP[] PP[]
733
+ VP[] -> VP[] PP[]
734
+ VP[] -> Verb[] NP[]
735
+ VP[] -> Verb[]
736
+ NP[] -> Det[pl=?x] Noun[pl=?x]
737
+ NP[] -> 'John'
738
+ NP[] -> 'I'
739
+ Det[] -> 'the'
740
+ Det[] -> 'my'
741
+ Det[-pl] -> 'a'
742
+ Noun[-pl] -> 'dog'
743
+ Noun[-pl] -> 'cookie'
744
+ Verb[] -> 'ate'
745
+ Verb[] -> 'saw'
746
+ Prep[] -> 'with'
747
+ Prep[] -> 'under'
748
+ <BLANKLINE>
749
+ * FeatureChartParser
750
+ Sentence: I saw John with a dog
751
+ |.I.s.J.w.a.d.|
752
+ |[-] . . . . .| [0:1] 'I'
753
+ |. [-] . . . .| [1:2] 'saw'
754
+ |. . [-] . . .| [2:3] 'John'
755
+ |. . . [-] . .| [3:4] 'with'
756
+ |. . . . [-] .| [4:5] 'a'
757
+ |. . . . . [-]| [5:6] 'dog'
758
+ |[-] . . . . .| [0:1] NP[] -> 'I' *
759
+ |[-> . . . . .| [0:1] S[] -> NP[] * VP[] {}
760
+ |[-> . . . . .| [0:1] NP[] -> NP[] * PP[] {}
761
+ |. [-] . . . .| [1:2] Verb[] -> 'saw' *
762
+ |. [-> . . . .| [1:2] VP[] -> Verb[] * NP[] {}
763
+ |. [-] . . . .| [1:2] VP[] -> Verb[] *
764
+ |. [-> . . . .| [1:2] VP[] -> VP[] * PP[] {}
765
+ |[---] . . . .| [0:2] S[] -> NP[] VP[] *
766
+ |. . [-] . . .| [2:3] NP[] -> 'John' *
767
+ |. . [-> . . .| [2:3] S[] -> NP[] * VP[] {}
768
+ |. . [-> . . .| [2:3] NP[] -> NP[] * PP[] {}
769
+ |. [---] . . .| [1:3] VP[] -> Verb[] NP[] *
770
+ |. [---> . . .| [1:3] VP[] -> VP[] * PP[] {}
771
+ |[-----] . . .| [0:3] S[] -> NP[] VP[] *
772
+ |. . . [-] . .| [3:4] Prep[] -> 'with' *
773
+ |. . . [-> . .| [3:4] PP[] -> Prep[] * NP[] {}
774
+ |. . . . [-] .| [4:5] Det[-pl] -> 'a' *
775
+ |. . . . [-> .| [4:5] NP[] -> Det[pl=?x] * Noun[pl=?x] {?x: False}
776
+ |. . . . . [-]| [5:6] Noun[-pl] -> 'dog' *
777
+ |. . . . [---]| [4:6] NP[] -> Det[-pl] Noun[-pl] *
778
+ |. . . . [--->| [4:6] S[] -> NP[] * VP[] {}
779
+ |. . . . [--->| [4:6] NP[] -> NP[] * PP[] {}
780
+ |. . . [-----]| [3:6] PP[] -> Prep[] NP[] *
781
+ |. . [-------]| [2:6] NP[] -> NP[] PP[] *
782
+ |. [---------]| [1:6] VP[] -> VP[] PP[] *
783
+ |. [--------->| [1:6] VP[] -> VP[] * PP[] {}
784
+ |[===========]| [0:6] S[] -> NP[] VP[] *
785
+ |. . [------->| [2:6] S[] -> NP[] * VP[] {}
786
+ |. . [------->| [2:6] NP[] -> NP[] * PP[] {}
787
+ |. [---------]| [1:6] VP[] -> Verb[] NP[] *
788
+ |. [--------->| [1:6] VP[] -> VP[] * PP[] {}
789
+ |[===========]| [0:6] S[] -> NP[] VP[] *
790
+ (S[]
791
+ (NP[] I)
792
+ (VP[]
793
+ (VP[] (Verb[] saw) (NP[] John))
794
+ (PP[] (Prep[] with) (NP[] (Det[-pl] a) (Noun[-pl] dog)))))
795
+ (S[]
796
+ (NP[] I)
797
+ (VP[]
798
+ (Verb[] saw)
799
+ (NP[]
800
+ (NP[] John)
801
+ (PP[] (Prep[] with) (NP[] (Det[-pl] a) (Noun[-pl] dog))))))
802
+
803
+
804
+ Unit tests for the Feature Chart Parser classes
805
+ -----------------------------------------------
806
+
807
+ The list of parsers we want to test.
808
+
809
+ >>> parsers = [nltk.parse.featurechart.FeatureChartParser,
810
+ ... nltk.parse.featurechart.FeatureTopDownChartParser,
811
+ ... nltk.parse.featurechart.FeatureBottomUpChartParser,
812
+ ... nltk.parse.featurechart.FeatureBottomUpLeftCornerChartParser,
813
+ ... nltk.parse.earleychart.FeatureIncrementalChartParser,
814
+ ... nltk.parse.earleychart.FeatureEarleyChartParser,
815
+ ... nltk.parse.earleychart.FeatureIncrementalTopDownChartParser,
816
+ ... nltk.parse.earleychart.FeatureIncrementalBottomUpChartParser,
817
+ ... nltk.parse.earleychart.FeatureIncrementalBottomUpLeftCornerChartParser,
818
+ ... ]
819
+
820
+ A helper function that tests each parser on the given grammar and sentence.
821
+ We check that the number of trees are correct, and that all parsers
822
+ return the same trees. Otherwise an error is printed.
823
+
824
+ >>> def unittest(grammar, sentence, nr_trees):
825
+ ... sentence = sentence.split()
826
+ ... trees = None
827
+ ... for P in parsers:
828
+ ... result = P(grammar).parse(sentence)
829
+ ... result = set(tree.freeze() for tree in result)
830
+ ... if len(result) != nr_trees:
831
+ ... print("Wrong nr of trees:", len(result))
832
+ ... elif trees is None:
833
+ ... trees = result
834
+ ... elif result != trees:
835
+ ... print("Trees differ for parser:", P.__name__)
836
+
837
+ The demo grammar from before, with an ambiguous sentence.
838
+
839
+ >>> isawjohn = nltk.parse.featurechart.demo_grammar()
840
+ >>> unittest(isawjohn, "I saw John with a dog with my cookie", 5)
841
+
842
+ This grammar tests that variables in different grammar rules are renamed
843
+ before unification. (The problematic variable is in this case ?X).
844
+
845
+ >>> whatwasthat = nltk.grammar.FeatureGrammar.fromstring('''
846
+ ... S[] -> NP[num=?N] VP[num=?N, slash=?X]
847
+ ... NP[num=?X] -> "what"
848
+ ... NP[num=?X] -> "that"
849
+ ... VP[num=?P, slash=none] -> V[num=?P] NP[]
850
+ ... V[num=sg] -> "was"
851
+ ... ''')
852
+ >>> unittest(whatwasthat, "what was that", 1)
853
+
854
+ This grammar tests that the same rule can be used in different places
855
+ in another rule, and that the variables are properly renamed.
856
+
857
+ >>> thislovesthat = nltk.grammar.FeatureGrammar.fromstring('''
858
+ ... S[] -> NP[case=nom] V[] NP[case=acc]
859
+ ... NP[case=?X] -> Pron[case=?X]
860
+ ... Pron[] -> "this"
861
+ ... Pron[] -> "that"
862
+ ... V[] -> "loves"
863
+ ... ''')
864
+ >>> unittest(thislovesthat, "this loves that", 1)
865
+
866
+
867
+ Tests for loading feature grammar files
868
+ ---------------------------------------
869
+
870
+ Alternative 1: first load the grammar, then create the parser.
871
+
872
+ >>> fcfg = nltk.data.load('grammars/book_grammars/feat0.fcfg')
873
+ >>> fcp1 = nltk.parse.FeatureChartParser(fcfg)
874
+ >>> print((type(fcp1)))
875
+ <class 'nltk.parse.featurechart.FeatureChartParser'>
876
+
877
+ Alternative 2: directly load the parser.
878
+
879
+ >>> fcp2 = nltk.parse.load_parser('grammars/book_grammars/feat0.fcfg')
880
+ >>> print((type(fcp2)))
881
+ <class 'nltk.parse.featurechart.FeatureChartParser'>
882
+
883
+
884
+