Skip to content

Commit

Permalink
update DPLP++ make_merge flair model to use Sentence() to wrap inputs
Browse files Browse the repository at this point in the history
  * Now compatible with flair versions 0.4.5--0.6.1
  * Fixes #9
  • Loading branch information
amir-zeldes committed Nov 25, 2020
1 parent 4646941 commit 462de8a
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/dplp_plusplus/src/make_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,24 @@ def clear_embeddings(self, sentences, also_clear_word_embeddings=False):
sentence.clear_embeddings(also_clear_word_embeddings=also_clear_word_embeddings)

def predict_proba(self, sentences):
from flair import __version__
from flair.data import Sentence

# Sort sentences and keep order
sents = [(len(s.split()),i,s) for i, s in enumerate(sentences)]
sents.sort(key=lambda x:x[0], reverse=True)

sentences = [s[2] for s in sents]

major, minor = str(__version__).split(".")[0:2]
if int(major) > 0 or int(minor) > 4:
sentences = [Sentence(s, use_tokenizer=lambda q: q.split()) for s in sentences]

preds = self.tagger.predict(sentences)

if preds is None: # Newer versions of flair have void predict method, use modified Sentence list
preds = sentences

# sort back
sents = [tuple(list(sents[i]) + [s]) for i, s in enumerate(preds)]
sents.sort(key=lambda x:x[1])
Expand Down

0 comments on commit 462de8a

Please sign in to comment.