Applies the named entity recognizer to the given list of tokens, where each token is a [word, tag] list.
(self, tokens)
| 429 | dict.setdefault(self, x[0], []).append(x) |
| 430 | |
| 431 | def apply(self, tokens): |
| 432 | """ Applies the named entity recognizer to the given list of tokens, |
| 433 | where each token is a [word, tag] list. |
| 434 | """ |
| 435 | # Note: we could also scan for patterns, e.g., |
| 436 | # "my|his|her name is|was *" => NNP-PERS. |
| 437 | i = 0 |
| 438 | while i < len(tokens): |
| 439 | w = tokens[i][0].lower() |
| 440 | if RE_ENTITY1.match(w) \ |
| 441 | or RE_ENTITY2.match(w) \ |
| 442 | or RE_ENTITY3.match(w): |
| 443 | tokens[i][1] = self.tag |
| 444 | if w in self: |
| 445 | for e in self[w]: |
| 446 | # Look ahead to see if successive words match the named entity. |
| 447 | e, tag = (e[:-1], "-"+e[-1].upper()) if e[-1] in self.cmd else (e, "") |
| 448 | b = True |
| 449 | for j, e in enumerate(e): |
| 450 | if i + j >= len(tokens) or tokens[i+j][0].lower() != e: |
| 451 | b = False; break |
| 452 | if b: |
| 453 | for token in tokens[i:i+j+1]: |
| 454 | token[1] = (token[1] == "NNPS" and token[1] or self.tag) + tag |
| 455 | i += j |
| 456 | break |
| 457 | i += 1 |
| 458 | return tokens |
| 459 | |
| 460 | #### PARSER ######################################################################################## |
| 461 |