Skip to content

Instantly share code, notes, and snippets.

@funktor
Last active October 14, 2018 05: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 funktor/e5a6e431672e503b9e3ae72c6cca7232 to your computer and use it in GitHub Desktop.
Save funktor/e5a6e431672e503b9e3ae72c6cca7232 to your computer and use it in GitHub Desktop.
def extract_phrase(test_sentence, tagger):
test_features = sent2features(test_sentence)
pred_labels, phrase_chunks = tagger.tag(test_features), []
start, end = 0, 0
for idx in range(len(pred_labels)):
if pred_labels[idx] == 'B':
start = idx
elif pred_labels[idx] == 'E':
end = idx
phrase_chunks.append((start, end))
if end < start:
phrase_chunks.append((start, len(pred_labels)-1))
return [test_sentence[start:end+1] for start, end in phrase_chunks]
def extract_phrase_chunks(test_sentences, tagger):
output = [extract_phrase(sent, tagger) for sent in test_sentences]
return output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment