INNER CODE UNIT · Python
inference
graviraja/100-Days-of-NLP · applications/classification/ner_tagging/app/app.py:75
def inference(sentence):
if isinstance(sentence, str):
tokens = [words_vocab[words_vocab.START]] + sentence.split() + [words_vocab[words_vocab.END]]
else:
tokens = sentence
chars = [['<START']] + [['<START>'] + [ch for ch in word] + ['<END>'] for word in tokens[1:-1]] + [['<END>']]
char_seq = []
for word in chars:
word_len = len(word)
# truncate the word if it is greater than max_word_len
if word_len > MAX_WORD_LEN:
word = word[:MAX_WORD_LEN]
# pad the word if it less
else:
pad_length = MAX_WORD_LEN - word_len
word = word + [chars_vocab.PAD] * pad_length