Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

entity_extract reads from json instead of individual files #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 30 additions & 7 deletions codex/codex.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def __init__(self, code="en", size="s"):
)

self.entities_ = {}
self.entity_extracts_ = {}
self.relations_ = {}
self.entity_types_ = {}
self.type_labels_ = {}
Expand Down Expand Up @@ -74,13 +75,20 @@ def entity_wikipedia_url(self, eid):

def entity_extract(self, eid):
"""Get the Wikipedia intro extract for this entity"""
fname = os.path.join(
self.data_dir_base, "entities", self.code, "extracts", f"{eid}.txt"
)
if os.path.exists(fname):
with open(fname) as f:
return "".join(f.readlines())
return ""
entity_extracts = self._load_entity_extracts()
if entity_extracts != {}:
if eid in entity_extracts:
return entity_extracts[eid]
else:
return ""
else: # if json doesn't exist, go back to previous implementation
fname = os.path.join(
self.data_dir_base, "entities", self.code, "extracts", f"{eid}.txt"
)
if os.path.exists(fname):
with open(fname) as f:
return "".join(f.readlines())
return ""

def relation_label(self, rid):
"""Get the label of this relation"""
Expand Down Expand Up @@ -162,6 +170,21 @@ def _load_entities(self):
)
return self.entities_

def _load_entity_extracts(self):
# if json doesn't exist, return {} so as to go back to previous implementation
try:
if not len(self.entity_extracts_):
self.entity_extracts_ = json.load(
open(
os.path.join(
self.data_dir_base, "entities", self.code, "entity_extracts.json"
)
)
)
except OSError as e:
self.entity_extracts_ = {}
return self.entity_extracts_

def _load_relations(self):
if not len(self.relations_):
self.relations_ = json.load(
Expand Down
1 change: 1 addition & 0 deletions data/entities/en/entity_extracts.json

Large diffs are not rendered by default.