Skip to content

Instantly share code, notes, and snippets.

@dchaplinsky
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dchaplinsky/5abf1622ba7324120a24 to your computer and use it in GitHub Desktop.
Save dchaplinsky/5abf1622ba7324120a24 to your computer and use it in GitHub Desktop.
<span class="ner-popup" data-slots="{{ data.slots|tojson }}" data-refs="{{ data.refs|tojson }}" title="{{ data.type }}">
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