-
-
Save artfuldev/289566335b33c2c5f3af295f30b71c61 to your computer and use it in GitHub Desktop.
Coreference resolution using coreferee and spacy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import spacy | |
import coreferee | |
def flatten(list_of_lists): | |
flat_list = []; | |
for item in list_of_lists: | |
if type(item) == list: | |
for sub_item in item: | |
flat_list.append(sub_item) | |
else: | |
flat_list.append(item) | |
return flat_list | |
def non_empty(replacement): | |
return replacement[1] != None if 1 < len(replacement) else False | |
def to_string_replacement(replacement): | |
[i, tokens] = replacement | |
return [i, str(tokens)] | |
def replacements(doc): | |
def resolve(mention): | |
return flatten(list( | |
map(to_string_replacement, | |
filter(non_empty, | |
map(lambda i: [i, doc._.coref_chains.resolve(doc[i])], | |
mention))))) | |
def to_replacements(chain): | |
return list(filter(non_empty, map(resolve, chain))) | |
return flatten(list(map(to_replacements, doc._.coref_chains))) | |
def resolved_text(doc): | |
tokens = list(token.text for token in doc) | |
for [i, replacement] in replacements(doc): | |
tokens[i] = replacement | |
return " ".join(tokens) | |
def resolve_coreference(text): | |
nlp = spacy.load('en_core_web_trf') | |
nlp.add_pipe('coreferee') | |
return resolved_text(nlp(text)) | |
resolve_coreference('Akiva Eldar is the chief political columnist and ' + | |
'an editorial writer for Haaretz. His columns also appear regularly in the ' + | |
'Ha\'aretz-Herald Tribune edition. In May 2006, The Financial Times selected ' + | |
'him among the most prominent and influential commentators in the world, and ' + | |
'his comments inspire callers from across the political spectrum') | |
# Akiva Eldar is the chief political columnist and an editorial writer for | |
# Haaretz . [Eldar] columns also appear regularly in the Ha’aretz - Herald | |
# Tribune edition . In May 2006 , The Financial Times selected [Eldar] among the | |
# most prominent and influential commentators in the world , and [Eldar] | |
# comments inspire callers from across the political spectrum |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment