nei-demo-backup / svm_predict.py
SmitaGautam's picture
Update svm_predict.py
8ecee00 verified
raw
history blame
847 Bytes
import nltk
from nltk import word_tokenize
from nltk import pos_tag
import joblib
import numpy as np
from train import feature_vector, pos_tags
model = joblib.load('model.pkl')
scaler = joblib.load('scaler.pkl')
nltk.download('averaged_perceptron_tagger_eng')
nltk.download('punkt_tab')
def predict(sentence):
tokens = word_tokenize(sentence)
sent_pos_tags = pos_tag(tokens)
sent_features = []
l = len(tokens)
for idx, word in enumerate(tokens):
current_tag = sent_pos_tags[idx][1]
current_idx = pos_tags.index(current_tag) if current_tag in pos_tags else -1
word_features = feature_vector(word, (1+idx)/l, current_idx)
sent_features.append(word_features)
# scaled_features = scaler.transform(sent_features)
predictions = model.predict(sent_features)
return tokens, predictions