ccg_semantics.doctest 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. .. Copyright (C) 2001-2020 NLTK Project
  2. .. For license information, see LICENSE.TXT
  3. ==============================================
  4. Combinatory Categorial Grammar with semantics
  5. ==============================================
  6. -----
  7. Chart
  8. -----
  9. >>> from nltk.ccg import chart, lexicon
  10. >>> from nltk.ccg.chart import printCCGDerivation
  11. No semantics
  12. -------------------
  13. >>> lex = lexicon.fromstring('''
  14. ... :- S, NP, N
  15. ... She => NP
  16. ... has => (S\\NP)/NP
  17. ... books => NP
  18. ... ''',
  19. ... False)
  20. >>> parser = chart.CCGChartParser(lex, chart.DefaultRuleSet)
  21. >>> parses = list(parser.parse("She has books".split()))
  22. >>> print(str(len(parses)) + " parses")
  23. 3 parses
  24. >>> printCCGDerivation(parses[0])
  25. She has books
  26. NP ((S\NP)/NP) NP
  27. -------------------->
  28. (S\NP)
  29. -------------------------<
  30. S
  31. >>> printCCGDerivation(parses[1])
  32. She has books
  33. NP ((S\NP)/NP) NP
  34. ----->T
  35. (S/(S\NP))
  36. -------------------->
  37. (S\NP)
  38. ------------------------->
  39. S
  40. >>> printCCGDerivation(parses[2])
  41. She has books
  42. NP ((S\NP)/NP) NP
  43. ----->T
  44. (S/(S\NP))
  45. ------------------>B
  46. (S/NP)
  47. ------------------------->
  48. S
  49. Simple semantics
  50. -------------------
  51. >>> lex = lexicon.fromstring('''
  52. ... :- S, NP, N
  53. ... She => NP {she}
  54. ... has => (S\\NP)/NP {\\x y.have(y, x)}
  55. ... a => NP/N {\\P.exists z.P(z)}
  56. ... book => N {book}
  57. ... ''',
  58. ... True)
  59. >>> parser = chart.CCGChartParser(lex, chart.DefaultRuleSet)
  60. >>> parses = list(parser.parse("She has a book".split()))
  61. >>> print(str(len(parses)) + " parses")
  62. 7 parses
  63. >>> printCCGDerivation(parses[0])
  64. She has a book
  65. NP {she} ((S\NP)/NP) {\x y.have(y,x)} (NP/N) {\P.exists z.P(z)} N {book}
  66. ------------------------------------->
  67. NP {exists z.book(z)}
  68. ------------------------------------------------------------------->
  69. (S\NP) {\y.have(y,exists z.book(z))}
  70. -----------------------------------------------------------------------------<
  71. S {have(she,exists z.book(z))}
  72. >>> printCCGDerivation(parses[1])
  73. She has a book
  74. NP {she} ((S\NP)/NP) {\x y.have(y,x)} (NP/N) {\P.exists z.P(z)} N {book}
  75. --------------------------------------------------------->B
  76. ((S\NP)/N) {\P y.have(y,exists z.P(z))}
  77. ------------------------------------------------------------------->
  78. (S\NP) {\y.have(y,exists z.book(z))}
  79. -----------------------------------------------------------------------------<
  80. S {have(she,exists z.book(z))}
  81. >>> printCCGDerivation(parses[2])
  82. She has a book
  83. NP {she} ((S\NP)/NP) {\x y.have(y,x)} (NP/N) {\P.exists z.P(z)} N {book}
  84. ---------->T
  85. (S/(S\NP)) {\F.F(she)}
  86. ------------------------------------->
  87. NP {exists z.book(z)}
  88. ------------------------------------------------------------------->
  89. (S\NP) {\y.have(y,exists z.book(z))}
  90. ----------------------------------------------------------------------------->
  91. S {have(she,exists z.book(z))}
  92. >>> printCCGDerivation(parses[3])
  93. She has a book
  94. NP {she} ((S\NP)/NP) {\x y.have(y,x)} (NP/N) {\P.exists z.P(z)} N {book}
  95. ---------->T
  96. (S/(S\NP)) {\F.F(she)}
  97. --------------------------------------------------------->B
  98. ((S\NP)/N) {\P y.have(y,exists z.P(z))}
  99. ------------------------------------------------------------------->
  100. (S\NP) {\y.have(y,exists z.book(z))}
  101. ----------------------------------------------------------------------------->
  102. S {have(she,exists z.book(z))}
  103. >>> printCCGDerivation(parses[4])
  104. She has a book
  105. NP {she} ((S\NP)/NP) {\x y.have(y,x)} (NP/N) {\P.exists z.P(z)} N {book}
  106. ---------->T
  107. (S/(S\NP)) {\F.F(she)}
  108. ---------------------------------------->B
  109. (S/NP) {\x.have(she,x)}
  110. ------------------------------------->
  111. NP {exists z.book(z)}
  112. ----------------------------------------------------------------------------->
  113. S {have(she,exists z.book(z))}
  114. >>> printCCGDerivation(parses[5])
  115. She has a book
  116. NP {she} ((S\NP)/NP) {\x y.have(y,x)} (NP/N) {\P.exists z.P(z)} N {book}
  117. ---------->T
  118. (S/(S\NP)) {\F.F(she)}
  119. --------------------------------------------------------->B
  120. ((S\NP)/N) {\P y.have(y,exists z.P(z))}
  121. ------------------------------------------------------------------->B
  122. (S/N) {\P.have(she,exists z.P(z))}
  123. ----------------------------------------------------------------------------->
  124. S {have(she,exists z.book(z))}
  125. >>> printCCGDerivation(parses[6])
  126. She has a book
  127. NP {she} ((S\NP)/NP) {\x y.have(y,x)} (NP/N) {\P.exists z.P(z)} N {book}
  128. ---------->T
  129. (S/(S\NP)) {\F.F(she)}
  130. ---------------------------------------->B
  131. (S/NP) {\x.have(she,x)}
  132. ------------------------------------------------------------------->B
  133. (S/N) {\P.have(she,exists z.P(z))}
  134. ----------------------------------------------------------------------------->
  135. S {have(she,exists z.book(z))}
  136. Complex semantics
  137. -------------------
  138. >>> lex = lexicon.fromstring('''
  139. ... :- S, NP, N
  140. ... She => NP {she}
  141. ... has => (S\\NP)/NP {\\x y.have(y, x)}
  142. ... a => ((S\\NP)\\((S\\NP)/NP))/N {\\P R x.(exists z.P(z) & R(z,x))}
  143. ... book => N {book}
  144. ... ''',
  145. ... True)
  146. >>> parser = chart.CCGChartParser(lex, chart.DefaultRuleSet)
  147. >>> parses = list(parser.parse("She has a book".split()))
  148. >>> print(str(len(parses)) + " parses")
  149. 2 parses
  150. >>> printCCGDerivation(parses[0])
  151. She has a book
  152. NP {she} ((S\NP)/NP) {\x y.have(y,x)} (((S\NP)\((S\NP)/NP))/N) {\P R x.(exists z.P(z) & R(z,x))} N {book}
  153. ---------------------------------------------------------------------->
  154. ((S\NP)\((S\NP)/NP)) {\R x.(exists z.book(z) & R(z,x))}
  155. ----------------------------------------------------------------------------------------------------<
  156. (S\NP) {\x.(exists z.book(z) & have(x,z))}
  157. --------------------------------------------------------------------------------------------------------------<
  158. S {(exists z.book(z) & have(she,z))}
  159. >>> printCCGDerivation(parses[1])
  160. She has a book
  161. NP {she} ((S\NP)/NP) {\x y.have(y,x)} (((S\NP)\((S\NP)/NP))/N) {\P R x.(exists z.P(z) & R(z,x))} N {book}
  162. ---------->T
  163. (S/(S\NP)) {\F.F(she)}
  164. ---------------------------------------------------------------------->
  165. ((S\NP)\((S\NP)/NP)) {\R x.(exists z.book(z) & R(z,x))}
  166. ----------------------------------------------------------------------------------------------------<
  167. (S\NP) {\x.(exists z.book(z) & have(x,z))}
  168. -------------------------------------------------------------------------------------------------------------->
  169. S {(exists z.book(z) & have(she,z))}
  170. Using conjunctions
  171. ---------------------
  172. # TODO: The semantics of "and" should have been more flexible
  173. >>> lex = lexicon.fromstring('''
  174. ... :- S, NP, N
  175. ... I => NP {I}
  176. ... cook => (S\\NP)/NP {\\x y.cook(x,y)}
  177. ... and => var\\.,var/.,var {\\P Q x y.(P(x,y) & Q(x,y))}
  178. ... eat => (S\\NP)/NP {\\x y.eat(x,y)}
  179. ... the => NP/N {\\x.the(x)}
  180. ... bacon => N {bacon}
  181. ... ''',
  182. ... True)
  183. >>> parser = chart.CCGChartParser(lex, chart.DefaultRuleSet)
  184. >>> parses = list(parser.parse("I cook and eat the bacon".split()))
  185. >>> print(str(len(parses)) + " parses")
  186. 7 parses
  187. >>> printCCGDerivation(parses[0])
  188. I cook and eat the bacon
  189. NP {I} ((S\NP)/NP) {\x y.cook(x,y)} ((_var0\.,_var0)/.,_var0) {\P Q x y.(P(x,y) & Q(x,y))} ((S\NP)/NP) {\x y.eat(x,y)} (NP/N) {\x.the(x)} N {bacon}
  190. ------------------------------------------------------------------------------------->
  191. (((S\NP)/NP)\.,((S\NP)/NP)) {\Q x y.(eat(x,y) & Q(x,y))}
  192. -------------------------------------------------------------------------------------------------------------------<
  193. ((S\NP)/NP) {\x y.(eat(x,y) & cook(x,y))}
  194. ------------------------------->
  195. NP {the(bacon)}
  196. -------------------------------------------------------------------------------------------------------------------------------------------------->
  197. (S\NP) {\y.(eat(the(bacon),y) & cook(the(bacon),y))}
  198. ----------------------------------------------------------------------------------------------------------------------------------------------------------<
  199. S {(eat(the(bacon),I) & cook(the(bacon),I))}
  200. >>> printCCGDerivation(parses[1])
  201. I cook and eat the bacon
  202. NP {I} ((S\NP)/NP) {\x y.cook(x,y)} ((_var0\.,_var0)/.,_var0) {\P Q x y.(P(x,y) & Q(x,y))} ((S\NP)/NP) {\x y.eat(x,y)} (NP/N) {\x.the(x)} N {bacon}
  203. ------------------------------------------------------------------------------------->
  204. (((S\NP)/NP)\.,((S\NP)/NP)) {\Q x y.(eat(x,y) & Q(x,y))}
  205. -------------------------------------------------------------------------------------------------------------------<
  206. ((S\NP)/NP) {\x y.(eat(x,y) & cook(x,y))}
  207. --------------------------------------------------------------------------------------------------------------------------------------->B
  208. ((S\NP)/N) {\x y.(eat(the(x),y) & cook(the(x),y))}
  209. -------------------------------------------------------------------------------------------------------------------------------------------------->
  210. (S\NP) {\y.(eat(the(bacon),y) & cook(the(bacon),y))}
  211. ----------------------------------------------------------------------------------------------------------------------------------------------------------<
  212. S {(eat(the(bacon),I) & cook(the(bacon),I))}
  213. >>> printCCGDerivation(parses[2])
  214. I cook and eat the bacon
  215. NP {I} ((S\NP)/NP) {\x y.cook(x,y)} ((_var0\.,_var0)/.,_var0) {\P Q x y.(P(x,y) & Q(x,y))} ((S\NP)/NP) {\x y.eat(x,y)} (NP/N) {\x.the(x)} N {bacon}
  216. -------->T
  217. (S/(S\NP)) {\F.F(I)}
  218. ------------------------------------------------------------------------------------->
  219. (((S\NP)/NP)\.,((S\NP)/NP)) {\Q x y.(eat(x,y) & Q(x,y))}
  220. -------------------------------------------------------------------------------------------------------------------<
  221. ((S\NP)/NP) {\x y.(eat(x,y) & cook(x,y))}
  222. ------------------------------->
  223. NP {the(bacon)}
  224. -------------------------------------------------------------------------------------------------------------------------------------------------->
  225. (S\NP) {\y.(eat(the(bacon),y) & cook(the(bacon),y))}
  226. ---------------------------------------------------------------------------------------------------------------------------------------------------------->
  227. S {(eat(the(bacon),I) & cook(the(bacon),I))}
  228. >>> printCCGDerivation(parses[3])
  229. I cook and eat the bacon
  230. NP {I} ((S\NP)/NP) {\x y.cook(x,y)} ((_var0\.,_var0)/.,_var0) {\P Q x y.(P(x,y) & Q(x,y))} ((S\NP)/NP) {\x y.eat(x,y)} (NP/N) {\x.the(x)} N {bacon}
  231. -------->T
  232. (S/(S\NP)) {\F.F(I)}
  233. ------------------------------------------------------------------------------------->
  234. (((S\NP)/NP)\.,((S\NP)/NP)) {\Q x y.(eat(x,y) & Q(x,y))}
  235. -------------------------------------------------------------------------------------------------------------------<
  236. ((S\NP)/NP) {\x y.(eat(x,y) & cook(x,y))}
  237. --------------------------------------------------------------------------------------------------------------------------------------->B
  238. ((S\NP)/N) {\x y.(eat(the(x),y) & cook(the(x),y))}
  239. -------------------------------------------------------------------------------------------------------------------------------------------------->
  240. (S\NP) {\y.(eat(the(bacon),y) & cook(the(bacon),y))}
  241. ---------------------------------------------------------------------------------------------------------------------------------------------------------->
  242. S {(eat(the(bacon),I) & cook(the(bacon),I))}
  243. >>> printCCGDerivation(parses[4])
  244. I cook and eat the bacon
  245. NP {I} ((S\NP)/NP) {\x y.cook(x,y)} ((_var0\.,_var0)/.,_var0) {\P Q x y.(P(x,y) & Q(x,y))} ((S\NP)/NP) {\x y.eat(x,y)} (NP/N) {\x.the(x)} N {bacon}
  246. -------->T
  247. (S/(S\NP)) {\F.F(I)}
  248. ------------------------------------------------------------------------------------->
  249. (((S\NP)/NP)\.,((S\NP)/NP)) {\Q x y.(eat(x,y) & Q(x,y))}
  250. -------------------------------------------------------------------------------------------------------------------<
  251. ((S\NP)/NP) {\x y.(eat(x,y) & cook(x,y))}
  252. --------------------------------------------------------------------------------------------------------------------------->B
  253. (S/NP) {\x.(eat(x,I) & cook(x,I))}
  254. ------------------------------->
  255. NP {the(bacon)}
  256. ---------------------------------------------------------------------------------------------------------------------------------------------------------->
  257. S {(eat(the(bacon),I) & cook(the(bacon),I))}
  258. >>> printCCGDerivation(parses[5])
  259. I cook and eat the bacon
  260. NP {I} ((S\NP)/NP) {\x y.cook(x,y)} ((_var0\.,_var0)/.,_var0) {\P Q x y.(P(x,y) & Q(x,y))} ((S\NP)/NP) {\x y.eat(x,y)} (NP/N) {\x.the(x)} N {bacon}
  261. -------->T
  262. (S/(S\NP)) {\F.F(I)}
  263. ------------------------------------------------------------------------------------->
  264. (((S\NP)/NP)\.,((S\NP)/NP)) {\Q x y.(eat(x,y) & Q(x,y))}
  265. -------------------------------------------------------------------------------------------------------------------<
  266. ((S\NP)/NP) {\x y.(eat(x,y) & cook(x,y))}
  267. --------------------------------------------------------------------------------------------------------------------------------------->B
  268. ((S\NP)/N) {\x y.(eat(the(x),y) & cook(the(x),y))}
  269. ----------------------------------------------------------------------------------------------------------------------------------------------->B
  270. (S/N) {\x.(eat(the(x),I) & cook(the(x),I))}
  271. ---------------------------------------------------------------------------------------------------------------------------------------------------------->
  272. S {(eat(the(bacon),I) & cook(the(bacon),I))}
  273. >>> printCCGDerivation(parses[6])
  274. I cook and eat the bacon
  275. NP {I} ((S\NP)/NP) {\x y.cook(x,y)} ((_var0\.,_var0)/.,_var0) {\P Q x y.(P(x,y) & Q(x,y))} ((S\NP)/NP) {\x y.eat(x,y)} (NP/N) {\x.the(x)} N {bacon}
  276. -------->T
  277. (S/(S\NP)) {\F.F(I)}
  278. ------------------------------------------------------------------------------------->
  279. (((S\NP)/NP)\.,((S\NP)/NP)) {\Q x y.(eat(x,y) & Q(x,y))}
  280. -------------------------------------------------------------------------------------------------------------------<
  281. ((S\NP)/NP) {\x y.(eat(x,y) & cook(x,y))}
  282. --------------------------------------------------------------------------------------------------------------------------->B
  283. (S/NP) {\x.(eat(x,I) & cook(x,I))}
  284. ----------------------------------------------------------------------------------------------------------------------------------------------->B
  285. (S/N) {\x.(eat(the(x),I) & cook(the(x),I))}
  286. ---------------------------------------------------------------------------------------------------------------------------------------------------------->
  287. S {(eat(the(bacon),I) & cook(the(bacon),I))}
  288. Tests from published papers
  289. ------------------------------
  290. An example from "CCGbank: A Corpus of CCG Derivations and Dependency Structures Extracted from the Penn Treebank", Hockenmaier and Steedman, 2007, Page 359, https://www.aclweb.org/anthology/J/J07/J07-3004.pdf
  291. >>> lex = lexicon.fromstring('''
  292. ... :- S, NP
  293. ... I => NP {I}
  294. ... give => ((S\\NP)/NP)/NP {\\x y z.give(y,x,z)}
  295. ... them => NP {them}
  296. ... money => NP {money}
  297. ... ''',
  298. ... True)
  299. >>> parser = chart.CCGChartParser(lex, chart.DefaultRuleSet)
  300. >>> parses = list(parser.parse("I give them money".split()))
  301. >>> print(str(len(parses)) + " parses")
  302. 3 parses
  303. >>> printCCGDerivation(parses[0])
  304. I give them money
  305. NP {I} (((S\NP)/NP)/NP) {\x y z.give(y,x,z)} NP {them} NP {money}
  306. -------------------------------------------------->
  307. ((S\NP)/NP) {\y z.give(y,them,z)}
  308. -------------------------------------------------------------->
  309. (S\NP) {\z.give(money,them,z)}
  310. ----------------------------------------------------------------------<
  311. S {give(money,them,I)}
  312. >>> printCCGDerivation(parses[1])
  313. I give them money
  314. NP {I} (((S\NP)/NP)/NP) {\x y z.give(y,x,z)} NP {them} NP {money}
  315. -------->T
  316. (S/(S\NP)) {\F.F(I)}
  317. -------------------------------------------------->
  318. ((S\NP)/NP) {\y z.give(y,them,z)}
  319. -------------------------------------------------------------->
  320. (S\NP) {\z.give(money,them,z)}
  321. ---------------------------------------------------------------------->
  322. S {give(money,them,I)}
  323. >>> printCCGDerivation(parses[2])
  324. I give them money
  325. NP {I} (((S\NP)/NP)/NP) {\x y z.give(y,x,z)} NP {them} NP {money}
  326. -------->T
  327. (S/(S\NP)) {\F.F(I)}
  328. -------------------------------------------------->
  329. ((S\NP)/NP) {\y z.give(y,them,z)}
  330. ---------------------------------------------------------->B
  331. (S/NP) {\y.give(y,them,I)}
  332. ---------------------------------------------------------------------->
  333. S {give(money,them,I)}
  334. An example from "CCGbank: A Corpus of CCG Derivations and Dependency Structures Extracted from the Penn Treebank", Hockenmaier and Steedman, 2007, Page 359, https://www.aclweb.org/anthology/J/J07/J07-3004.pdf
  335. >>> lex = lexicon.fromstring('''
  336. ... :- N, NP, S
  337. ... money => N {money}
  338. ... that => (N\\N)/(S/NP) {\\P Q x.(P(x) & Q(x))}
  339. ... I => NP {I}
  340. ... give => ((S\\NP)/NP)/NP {\\x y z.give(y,x,z)}
  341. ... them => NP {them}
  342. ... ''',
  343. ... True)
  344. >>> parser = chart.CCGChartParser(lex, chart.DefaultRuleSet)
  345. >>> parses = list(parser.parse("money that I give them".split()))
  346. >>> print(str(len(parses)) + " parses")
  347. 3 parses
  348. >>> printCCGDerivation(parses[0])
  349. money that I give them
  350. N {money} ((N\N)/(S/NP)) {\P Q x.(P(x) & Q(x))} NP {I} (((S\NP)/NP)/NP) {\x y z.give(y,x,z)} NP {them}
  351. -------->T
  352. (S/(S\NP)) {\F.F(I)}
  353. -------------------------------------------------->
  354. ((S\NP)/NP) {\y z.give(y,them,z)}
  355. ---------------------------------------------------------->B
  356. (S/NP) {\y.give(y,them,I)}
  357. ------------------------------------------------------------------------------------------------->
  358. (N\N) {\Q x.(give(x,them,I) & Q(x))}
  359. ------------------------------------------------------------------------------------------------------------<
  360. N {\x.(give(x,them,I) & money(x))}
  361. >>> printCCGDerivation(parses[1])
  362. money that I give them
  363. N {money} ((N\N)/(S/NP)) {\P Q x.(P(x) & Q(x))} NP {I} (((S\NP)/NP)/NP) {\x y z.give(y,x,z)} NP {them}
  364. ----------->T
  365. (N/(N\N)) {\F.F(money)}
  366. -------->T
  367. (S/(S\NP)) {\F.F(I)}
  368. -------------------------------------------------->
  369. ((S\NP)/NP) {\y z.give(y,them,z)}
  370. ---------------------------------------------------------->B
  371. (S/NP) {\y.give(y,them,I)}
  372. ------------------------------------------------------------------------------------------------->
  373. (N\N) {\Q x.(give(x,them,I) & Q(x))}
  374. ------------------------------------------------------------------------------------------------------------>
  375. N {\x.(give(x,them,I) & money(x))}
  376. >>> printCCGDerivation(parses[2])
  377. money that I give them
  378. N {money} ((N\N)/(S/NP)) {\P Q x.(P(x) & Q(x))} NP {I} (((S\NP)/NP)/NP) {\x y z.give(y,x,z)} NP {them}
  379. ----------->T
  380. (N/(N\N)) {\F.F(money)}
  381. -------------------------------------------------->B
  382. (N/(S/NP)) {\P x.(P(x) & money(x))}
  383. -------->T
  384. (S/(S\NP)) {\F.F(I)}
  385. -------------------------------------------------->
  386. ((S\NP)/NP) {\y z.give(y,them,z)}
  387. ---------------------------------------------------------->B
  388. (S/NP) {\y.give(y,them,I)}
  389. ------------------------------------------------------------------------------------------------------------>
  390. N {\x.(give(x,them,I) & money(x))}
  391. -------
  392. Lexicon
  393. -------
  394. >>> from nltk.ccg import lexicon
  395. Parse lexicon with semantics
  396. >>> print(str(lexicon.fromstring(
  397. ... '''
  398. ... :- S,NP
  399. ...
  400. ... IntransVsg :: S\\NP[sg]
  401. ...
  402. ... sleeps => IntransVsg {\\x.sleep(x)}
  403. ... eats => S\\NP[sg]/NP {\\x y.eat(x,y)}
  404. ...
  405. ... and => var\\var/var {\\x y.x & y}
  406. ... ''',
  407. ... True
  408. ... )))
  409. and => ((_var0\_var0)/_var0) {(\x y.x & y)}
  410. eats => ((S\NP['sg'])/NP) {\x y.eat(x,y)}
  411. sleeps => (S\NP['sg']) {\x.sleep(x)}
  412. Parse lexicon without semantics
  413. >>> print(str(lexicon.fromstring(
  414. ... '''
  415. ... :- S,NP
  416. ...
  417. ... IntransVsg :: S\\NP[sg]
  418. ...
  419. ... sleeps => IntransVsg
  420. ... eats => S\\NP[sg]/NP {sem=\\x y.eat(x,y)}
  421. ...
  422. ... and => var\\var/var
  423. ... ''',
  424. ... False
  425. ... )))
  426. and => ((_var0\_var0)/_var0)
  427. eats => ((S\NP['sg'])/NP)
  428. sleeps => (S\NP['sg'])
  429. Semantics are missing
  430. >>> print(str(lexicon.fromstring(
  431. ... '''
  432. ... :- S,NP
  433. ...
  434. ... eats => S\\NP[sg]/NP
  435. ... ''',
  436. ... True
  437. ... )))
  438. Traceback (most recent call last):
  439. ...
  440. AssertionError: eats => S\NP[sg]/NP must contain semantics because include_semantics is set to True
  441. ------------------------------------
  442. CCG combinator semantics computation
  443. ------------------------------------
  444. >>> from nltk.sem.logic import *
  445. >>> from nltk.ccg.logic import *
  446. >>> read_expr = Expression.fromstring
  447. Compute semantics from function application
  448. >>> print(str(compute_function_semantics(read_expr(r'\x.P(x)'), read_expr(r'book'))))
  449. P(book)
  450. >>> print(str(compute_function_semantics(read_expr(r'\P.P(book)'), read_expr(r'read'))))
  451. read(book)
  452. >>> print(str(compute_function_semantics(read_expr(r'\P.P(book)'), read_expr(r'\x.read(x)'))))
  453. read(book)
  454. Compute semantics from composition
  455. >>> print(str(compute_composition_semantics(read_expr(r'\x.P(x)'), read_expr(r'\x.Q(x)'))))
  456. \x.P(Q(x))
  457. >>> print(str(compute_composition_semantics(read_expr(r'\x.P(x)'), read_expr(r'read'))))
  458. Traceback (most recent call last):
  459. ...
  460. AssertionError: `read` must be a lambda expression
  461. Compute semantics from substitution
  462. >>> print(str(compute_substitution_semantics(read_expr(r'\x y.P(x,y)'), read_expr(r'\x.Q(x)'))))
  463. \x.P(x,Q(x))
  464. >>> print(str(compute_substitution_semantics(read_expr(r'\x.P(x)'), read_expr(r'read'))))
  465. Traceback (most recent call last):
  466. ...
  467. AssertionError: `\x.P(x)` must be a lambda expression with 2 arguments
  468. Compute type-raise semantics
  469. >>> print(str(compute_type_raised_semantics(read_expr(r'\x.P(x)'))))
  470. \F x.F(P(x))
  471. >>> print(str(compute_type_raised_semantics(read_expr(r'\x.F(x)'))))
  472. \F1 x.F1(F(x))
  473. >>> print(str(compute_type_raised_semantics(read_expr(r'\x y z.P(x,y,z)'))))
  474. \F x y z.F(P(x,y,z))