Spaces:
Sleeping
Sleeping
SmitaGautam
commited on
Commit
•
8ecee00
1
Parent(s):
762a449
Update svm_predict.py
Browse files- svm_predict.py +10 -12
svm_predict.py
CHANGED
@@ -2,26 +2,24 @@ import nltk
|
|
2 |
from nltk import word_tokenize
|
3 |
from nltk import pos_tag
|
4 |
import joblib
|
|
|
5 |
from train import feature_vector, pos_tags
|
6 |
|
7 |
-
model = joblib.load('
|
|
|
8 |
nltk.download('averaged_perceptron_tagger_eng')
|
9 |
nltk.download('punkt_tab')
|
10 |
|
11 |
def predict(sentence):
|
12 |
tokens = word_tokenize(sentence)
|
13 |
sent_pos_tags = pos_tag(tokens)
|
14 |
-
|
15 |
l = len(tokens)
|
16 |
for idx, word in enumerate(tokens):
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
#
|
22 |
-
|
23 |
-
# vec = feature_vector(word, prev_idx, next_idx, current_idx)
|
24 |
-
vec = feature_vector(word, idx/l, sent_pos_tags[idx][1])
|
25 |
-
y_pred = model.predict([vec])
|
26 |
-
predictions.append(round(y_pred[0]))
|
27 |
return tokens, predictions
|
|
|
2 |
from nltk import word_tokenize
|
3 |
from nltk import pos_tag
|
4 |
import joblib
|
5 |
+
import numpy as np
|
6 |
from train import feature_vector, pos_tags
|
7 |
|
8 |
+
model = joblib.load('model.pkl')
|
9 |
+
scaler = joblib.load('scaler.pkl')
|
10 |
nltk.download('averaged_perceptron_tagger_eng')
|
11 |
nltk.download('punkt_tab')
|
12 |
|
13 |
def predict(sentence):
|
14 |
tokens = word_tokenize(sentence)
|
15 |
sent_pos_tags = pos_tag(tokens)
|
16 |
+
sent_features = []
|
17 |
l = len(tokens)
|
18 |
for idx, word in enumerate(tokens):
|
19 |
+
current_tag = sent_pos_tags[idx][1]
|
20 |
+
current_idx = pos_tags.index(current_tag) if current_tag in pos_tags else -1
|
21 |
+
word_features = feature_vector(word, (1+idx)/l, current_idx)
|
22 |
+
sent_features.append(word_features)
|
23 |
+
# scaled_features = scaler.transform(sent_features)
|
24 |
+
predictions = model.predict(sent_features)
|
|
|
|
|
|
|
|
|
25 |
return tokens, predictions
|