Last active
August 29, 2015 14:08
-
-
Save dchaplinsky/5abf1622ba7324120a24 to your computer and use it in GitHub Desktop.
This file contains 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
<span class="ner-popup" data-slots="{{ data.slots|tojson }}" data-refs="{{ data.refs|tojson }}" title="{{ data.type }}"> |
This file contains 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
class Article(object): | |
def __init__(self, id, data): | |
def compare_ner(a, b): | |
if a["start"] == b["start"]: | |
return cmp(b["end"], a["end"]) | |
else: | |
return cmp(a["start"], b["start"]) | |
for d in data: | |
if not hasattr(self, d): | |
setattr(self, d, unicode(data[d].decode("utf8"))) | |
if hasattr(self, "pullenti"): | |
self.pullenti = json.loads(self.pullenti) | |
ner_entries = [] | |
title_len = len(self.title) + 1 | |
for k, entry in enumerate(self.pullenti): | |
self.suggested_tags[entry["type"]] += unify_tags(entry) | |
if u'occurences' in entry: | |
for occurence in entry[u'occurences']: | |
if occurence[u"start"] > title_len: | |
ner_entries.append({ | |
"start": occurence[u"start"] - title_len, | |
"end": occurence[u"end"] - title_len, | |
"key": k, | |
}) | |
# let's make our tags unique | |
for t in self.suggested_tags.keys(): | |
self.suggested_tags[t] = set(self.suggested_tags[t]) | |
ner_entries.sort(cmp=compare_ner) | |
if hasattr(self, "description"): | |
prev_end = 0 | |
shift = 0 | |
for entry in ner_entries: | |
if entry["start"] > prev_end: | |
prev_end = entry["end"] | |
start = entry["start"] + shift | |
href = render_template("ner_entry.html", | |
data=self.pullenti[entry["key"]]) | |
self.description = self.description[:start] + href + self.description[start:] | |
shift += len(href) | |
end = entry["end"] + 1 + shift | |
self.description = self.description[:end] + "</span>" + self.description[end:] | |
shift += 7 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment