| 295 | print "pattern.search.Pattern.fromstring()" |
| 296 | |
| 297 | def test_match(self): |
| 298 | # Assert Pattern.match() |
| 299 | P = search.Pattern.fromstring |
| 300 | X = search.STRICT |
| 301 | S = lambda s: Sentence(parse(s, relations=True, lemmata=True)) |
| 302 | for i, (pattern, test, match) in enumerate(( |
| 303 | (P("^rabbit"), "white rabbit", None), # 0 |
| 304 | (P("^rabbit"), "rabbit", "rabbit"), # 1 |
| 305 | (P("rabbit"), "big white rabbit", "rabbit"), # 2 |
| 306 | (P("rabbit*"), "big white rabbits", "rabbits"), # 3 |
| 307 | (P("JJ|NN"), S("big white rabbits"), "big"), # 4 |
| 308 | (P("JJ+"), S("big white rabbits"), "big white"), # 5 |
| 309 | (P("JJ+ NN*"), S("big white rabbits"), "big white rabbits"), # 6 |
| 310 | (P("JJ black|white NN*"), S("big white rabbits"), "big white rabbits"), # 7 |
| 311 | (P("NP"), S("big white rabbit"), "big white rabbit"), # 8 |
| 312 | (P("big? rabbit", X), S("big white rabbit"), "rabbit"), # 9 strict |
| 313 | (P("big? rabbit|NN"), S("big white rabbit"), "rabbit"), # 10 explicit |
| 314 | (P("big? rabbit"), S("big white rabbit"), "big white rabbit"), # 11 greedy |
| 315 | (P("rabbit VP JJ"), S("the rabbit was huge"), "the rabbit was huge"), # 12 |
| 316 | (P("rabbit be JJ"), S("the rabbit was huge"), "the rabbit was huge"), # 13 lemma |
| 317 | (P("rabbit be JJ", X), S("the rabbit was huge"), "rabbit was huge"), # 14 |
| 318 | (P("rabbit is JJ"), S("the rabbit was huge"), None), # 15 |
| 319 | (P("the NP"), S("the rabid rodents"), "the rabid rodents"), # 16 overlap |
| 320 | (P("t*|r*+"), S("the rabid rodents"), "the rabid rodents"), # 17 |
| 321 | (P("(DT) JJ? NN*"), S("the rabid rodents"), "the rabid rodents"), # 18 |
| 322 | (P("(DT) JJ? NN*"), S("the rabbit"), "the rabbit"), # 19 |
| 323 | (P("rabbit"), S("the big rabbit"), "the big rabbit"), # 20 greedy |
| 324 | (P("eat carrot"), S("is eating a carrot"), "is eating a carrot"), # 21 |
| 325 | (P("eat carrot|NP"), S("is eating a carrot"), "is eating a carrot"), # 22 |
| 326 | (P("eat NP"), S("is eating a carrot"), "is eating a carrot"), # 23 |
| 327 | (P("eat a"), S("is eating a carrot"), "is eating a"), # 24 |
| 328 | (P("!NP carrot"), S("is eating a carrot"), "is eating a carrot"), # 25 |
| 329 | (P("eat !pizza"), S("is eating a carrot"), "is eating a carrot"), # 26 |
| 330 | (P("eating a"), S("is eating a carrot"), "is eating a"), # 27 |
| 331 | (P("eating !carrot", X), S("is eating a carrot"), "eating a"), # 28 |
| 332 | (P("eat !carrot"), S("is eating a carrot"), None), # 28 NP chunk is a carrot |
| 333 | (P("eat !DT"), S("is eating a carrot"), None), # 30 eat followed by DT |
| 334 | (P("eat !NN"), S("is eating a carrot"), "is eating a"), # 31 a/DT is not NN |
| 335 | (P("!be carrot"), S("is eating a carrot"), "is eating a carrot"), # 32 is eating == eat != is |
| 336 | (P("!eat|VP carrot"), S("is eating a carrot"), None), # 33 VP chunk == eat |
| 337 | (P("white_rabbit"), S("big white rabbit"), None), # 34 |
| 338 | (P("[white rabbit]"), S("big white rabbit"), None), # 35 |
| 339 | (P("[* white rabbit]"), S("big white rabbit"), "big white rabbit"), # 36 |
| 340 | (P("[big * rabbit]"), S("big white rabbit"), "big white rabbit"), # 37 |
| 341 | (P("big [big * rabbit]"), S("big white rabbit"), "big white rabbit"), # 38 |
| 342 | (P("[*+ rabbit]"), S("big white rabbit"), None), # 39 bad pattern: "+" is literal |
| 343 | )): |
| 344 | m = pattern.match(test) |
| 345 | #print i, match, "<=>", m and m.string or None |
| 346 | self.assertTrue(getattr(m, "string", None) == match) |
| 347 | # Assert chunk with head at the front. |
| 348 | s = S("Felix the cat") |
| 349 | s.chunks[0]._head = lambda ch: ch.words[0] # head = "Felix" (it's a hack) |
| 350 | self.assertEqual(P("felix").match(s).string, "Felix the cat") |
| 351 | # Assert negation + custom greedy() function. |
| 352 | s = S("the big white rabbit") |
| 353 | g = lambda chunk, constraint: len([w for w in chunk if not constraint.match(w)]) == 0 |
| 354 | self.assertEqual(P("!white").match(s).string, "the big white rabbit") # a rabbit != white |