File size: 847 Bytes
899e4f3
 
 
 
8ecee00
899e4f3
 
8ecee00
 
899e4f3
 
 
 
 
 
8ecee00
25b106e
899e4f3
8ecee00
 
 
 
 
 
bb65e7e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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