Spaces:
Sleeping
Sleeping
File size: 19,786 Bytes
d916065 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 |
.. Copyright (C) 2001-2023 NLTK Project
.. For license information, see LICENSE.TXT
==============================
Combinatory Categorial Grammar
==============================
Relative Clauses
----------------
>>> from nltk.ccg import chart, lexicon
Construct a lexicon:
>>> lex = lexicon.fromstring('''
... :- S, NP, N, VP
...
... Det :: NP/N
... Pro :: NP
... Modal :: S\\NP/VP
...
... TV :: VP/NP
... DTV :: TV/NP
...
... the => Det
...
... that => Det
... that => NP
...
... I => Pro
... you => Pro
... we => Pro
...
... chef => N
... cake => N
... children => N
... dough => N
...
... will => Modal
... should => Modal
... might => Modal
... must => Modal
...
... and => var\\.,var/.,var
...
... to => VP[to]/VP
...
... without => (VP\\VP)/VP[ing]
...
... be => TV
... cook => TV
... eat => TV
...
... cooking => VP[ing]/NP
...
... give => DTV
...
... is => (S\\NP)/NP
... prefer => (S\\NP)/NP
...
... which => (N\\N)/(S/NP)
...
... persuade => (VP/VP[to])/NP
... ''')
>>> parser = chart.CCGChartParser(lex, chart.DefaultRuleSet)
>>> for parse in parser.parse("you prefer that cake".split()):
... chart.printCCGDerivation(parse)
... break
...
you prefer that cake
NP ((S\NP)/NP) (NP/N) N
-------------->
NP
--------------------------->
(S\NP)
--------------------------------<
S
>>> for parse in parser.parse("that is the cake which you prefer".split()):
... chart.printCCGDerivation(parse)
... break
...
that is the cake which you prefer
NP ((S\NP)/NP) (NP/N) N ((N\N)/(S/NP)) NP ((S\NP)/NP)
----->T
(S/(S\NP))
------------------>B
(S/NP)
---------------------------------->
(N\N)
----------------------------------------<
N
------------------------------------------------>
NP
------------------------------------------------------------->
(S\NP)
-------------------------------------------------------------------<
S
Some other sentences to try:
"that is the cake which we will persuade the chef to cook"
"that is the cake which we will persuade the chef to give the children"
>>> sent = "that is the dough which you will eat without cooking".split()
>>> nosub_parser = chart.CCGChartParser(lex, chart.ApplicationRuleSet +
... chart.CompositionRuleSet + chart.TypeRaiseRuleSet)
Without Substitution (no output)
>>> for parse in nosub_parser.parse(sent):
... chart.printCCGDerivation(parse)
With Substitution:
>>> for parse in parser.parse(sent):
... chart.printCCGDerivation(parse)
... break
...
that is the dough which you will eat without cooking
NP ((S\NP)/NP) (NP/N) N ((N\N)/(S/NP)) NP ((S\NP)/VP) (VP/NP) ((VP\VP)/VP['ing']) (VP['ing']/NP)
----->T
(S/(S\NP))
------------------------------------->B
((VP\VP)/NP)
----------------------------------------------<Sx
(VP/NP)
----------------------------------------------------------->B
((S\NP)/NP)
---------------------------------------------------------------->B
(S/NP)
-------------------------------------------------------------------------------->
(N\N)
---------------------------------------------------------------------------------------<
N
----------------------------------------------------------------------------------------------->
NP
------------------------------------------------------------------------------------------------------------>
(S\NP)
------------------------------------------------------------------------------------------------------------------<
S
Conjunction
-----------
>>> from nltk.ccg.chart import CCGChartParser, ApplicationRuleSet, CompositionRuleSet
>>> from nltk.ccg.chart import SubstitutionRuleSet, TypeRaiseRuleSet, printCCGDerivation
>>> from nltk.ccg import lexicon
Lexicons for the tests:
>>> test1_lex = '''
... :- S,N,NP,VP
... I => NP
... you => NP
... will => S\\NP/VP
... cook => VP/NP
... which => (N\\N)/(S/NP)
... and => var\\.,var/.,var
... might => S\\NP/VP
... eat => VP/NP
... the => NP/N
... mushrooms => N
... parsnips => N'''
>>> test2_lex = '''
... :- N, S, NP, VP
... articles => N
... the => NP/N
... and => var\\.,var/.,var
... which => (N\\N)/(S/NP)
... I => NP
... anyone => NP
... will => (S/VP)\\NP
... file => VP/NP
... without => (VP\\VP)/VP[ing]
... forget => VP/NP
... reading => VP[ing]/NP
... '''
Tests handling of conjunctions.
Note that while the two derivations are different, they are semantically equivalent.
>>> lex = lexicon.fromstring(test1_lex)
>>> parser = CCGChartParser(lex, ApplicationRuleSet + CompositionRuleSet + SubstitutionRuleSet)
>>> for parse in parser.parse("I will cook and might eat the mushrooms and parsnips".split()):
... printCCGDerivation(parse)
I will cook and might eat the mushrooms and parsnips
NP ((S\NP)/VP) (VP/NP) ((_var0\.,_var0)/.,_var0) ((S\NP)/VP) (VP/NP) (NP/N) N ((_var0\.,_var0)/.,_var0) N
---------------------->B
((S\NP)/NP)
---------------------->B
((S\NP)/NP)
------------------------------------------------->
(((S\NP)/NP)\.,((S\NP)/NP))
-----------------------------------------------------------------------<
((S\NP)/NP)
------------------------------------->
(N\.,N)
------------------------------------------------<
N
-------------------------------------------------------->
NP
------------------------------------------------------------------------------------------------------------------------------->
(S\NP)
-----------------------------------------------------------------------------------------------------------------------------------<
S
I will cook and might eat the mushrooms and parsnips
NP ((S\NP)/VP) (VP/NP) ((_var0\.,_var0)/.,_var0) ((S\NP)/VP) (VP/NP) (NP/N) N ((_var0\.,_var0)/.,_var0) N
---------------------->B
((S\NP)/NP)
---------------------->B
((S\NP)/NP)
------------------------------------------------->
(((S\NP)/NP)\.,((S\NP)/NP))
-----------------------------------------------------------------------<
((S\NP)/NP)
------------------------------------------------------------------------------->B
((S\NP)/N)
------------------------------------->
(N\.,N)
------------------------------------------------<
N
------------------------------------------------------------------------------------------------------------------------------->
(S\NP)
-----------------------------------------------------------------------------------------------------------------------------------<
S
Tests handling subject extraction.
Interesting to point that the two parses are clearly semantically different.
>>> lex = lexicon.fromstring(test2_lex)
>>> parser = CCGChartParser(lex, ApplicationRuleSet + CompositionRuleSet + SubstitutionRuleSet)
>>> for parse in parser.parse("articles which I will file and forget without reading".split()):
... printCCGDerivation(parse)
articles which I will file and forget without reading
N ((N\N)/(S/NP)) NP ((S/VP)\NP) (VP/NP) ((_var0\.,_var0)/.,_var0) (VP/NP) ((VP\VP)/VP['ing']) (VP['ing']/NP)
-----------------<
(S/VP)
------------------------------------->B
((VP\VP)/NP)
----------------------------------------------<Sx
(VP/NP)
------------------------------------------------------------------------->
((VP/NP)\.,(VP/NP))
----------------------------------------------------------------------------------<
(VP/NP)
--------------------------------------------------------------------------------------------------->B
(S/NP)
------------------------------------------------------------------------------------------------------------------->
(N\N)
-----------------------------------------------------------------------------------------------------------------------------<
N
articles which I will file and forget without reading
N ((N\N)/(S/NP)) NP ((S/VP)\NP) (VP/NP) ((_var0\.,_var0)/.,_var0) (VP/NP) ((VP\VP)/VP['ing']) (VP['ing']/NP)
-----------------<
(S/VP)
------------------------------------>
((VP/NP)\.,(VP/NP))
---------------------------------------------<
(VP/NP)
------------------------------------->B
((VP\VP)/NP)
----------------------------------------------------------------------------------<Sx
(VP/NP)
--------------------------------------------------------------------------------------------------->B
(S/NP)
------------------------------------------------------------------------------------------------------------------->
(N\N)
-----------------------------------------------------------------------------------------------------------------------------<
N
Unicode support
---------------
Unicode words are supported.
>>> from nltk.ccg import chart, lexicon
Lexicons for the tests:
>>> lex = lexicon.fromstring('''
... :- S, N, NP, PP
...
... AdjI :: N\\N
... AdjD :: N/N
... AdvD :: S/S
... AdvI :: S\\S
... Det :: NP/N
... PrepNPCompl :: PP/NP
... PrepNAdjN :: S\\S/N
... PrepNAdjNP :: S\\S/NP
... VPNP :: S\\NP/NP
... VPPP :: S\\NP/PP
... VPser :: S\\NP/AdjI
...
... auto => N
... bebidas => N
... cine => N
... ley => N
... libro => N
... ministro => N
... panader铆a => N
... presidente => N
... super => N
...
... el => Det
... la => Det
... las => Det
... un => Det
...
... Ana => NP
... Pablo => NP
...
... y => var\\.,var/.,var
...
... pero => (S/NP)\\(S/NP)/(S/NP)
...
... anunci贸 => VPNP
... compr贸 => VPNP
... cree => S\\NP/S[dep]
... desminti贸 => VPNP
... lee => VPNP
... fueron => VPPP
...
... es => VPser
...
... interesante => AdjD
... interesante => AdjI
... nueva => AdjD
... nueva => AdjI
...
... a => PrepNPCompl
... en => PrepNAdjN
... en => PrepNAdjNP
...
... ayer => AdvI
...
... que => (NP\\NP)/(S/NP)
... que => S[dep]/S
... ''')
>>> parser = chart.CCGChartParser(lex, chart.DefaultRuleSet)
>>> for parse in parser.parse(u"el ministro anunci贸 pero el presidente desminti贸 la nueva ley".split()):
... printCCGDerivation(parse) # doctest: +SKIP
... # it fails on python2.7 because of the unicode problem explained in https://github.com/nltk/nltk/pull/1354
... break
el ministro anunci贸 pero el presidente desminti贸 la nueva ley
(NP/N) N ((S\NP)/NP) (((S/NP)\(S/NP))/(S/NP)) (NP/N) N ((S\NP)/NP) (NP/N) (N/N) N
------------------>
NP
------------------>T
(S/(S\NP))
-------------------->
NP
-------------------->T
(S/(S\NP))
--------------------------------->B
(S/NP)
----------------------------------------------------------->
((S/NP)\(S/NP))
------------>
N
-------------------->
NP
--------------------<T
(S\(S/NP))
-------------------------------------------------------------------------------<B
(S\(S/NP))
--------------------------------------------------------------------------------------------<B
(S/NP)
-------------------------------------------------------------------------------------------------------------->
S
|