Spaces:
Sleeping
Sleeping
SmitaGautam
commited on
Commit
•
25b106e
1
Parent(s):
899e4f3
Update svm_predict.py
Browse files- svm_predict.py +11 -9
svm_predict.py
CHANGED
@@ -4,7 +4,7 @@ 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 |
|
@@ -12,14 +12,16 @@ def predict(sentence):
|
|
12 |
tokens = word_tokenize(sentence)
|
13 |
sent_pos_tags = pos_tag(tokens)
|
14 |
predictions = []
|
|
|
15 |
for idx, word in enumerate(tokens):
|
16 |
-
prev_tag = -1 if idx==0 else sent_pos_tags[idx-1][1]
|
17 |
-
next_tag = -1 if idx==len(tokens)-1 else sent_pos_tags[idx+1][1]
|
18 |
-
current_tag = sent_pos_tags[idx][1]
|
19 |
-
prev_idx = pos_tags.index(prev_tag) if prev_tag in pos_tags else -1
|
20 |
-
next_idx = pos_tags.index(next_tag) if next_tag in pos_tags else -1
|
21 |
-
current_idx = pos_tags.index(current_tag) if current_tag in pos_tags else -1
|
22 |
-
vec = feature_vector(word, prev_idx, next_idx, current_idx)
|
|
|
23 |
y_pred = model.predict([vec])
|
24 |
-
predictions.append(y_pred[0])
|
25 |
return tokens, predictions
|
|
|
4 |
import joblib
|
5 |
from train import feature_vector, pos_tags
|
6 |
|
7 |
+
model = joblib.load('SVM_NEI_model.pkl')
|
8 |
nltk.download('averaged_perceptron_tagger_eng')
|
9 |
nltk.download('punkt_tab')
|
10 |
|
|
|
12 |
tokens = word_tokenize(sentence)
|
13 |
sent_pos_tags = pos_tag(tokens)
|
14 |
predictions = []
|
15 |
+
l = len(tokens)
|
16 |
for idx, word in enumerate(tokens):
|
17 |
+
# prev_tag = -1 if idx==0 else sent_pos_tags[idx-1][1]
|
18 |
+
# next_tag = -1 if idx==len(tokens)-1 else sent_pos_tags[idx+1][1]
|
19 |
+
# current_tag = sent_pos_tags[idx][1]
|
20 |
+
# prev_idx = pos_tags.index(prev_tag) if prev_tag in pos_tags else -1
|
21 |
+
# next_idx = pos_tags.index(next_tag) if next_tag in pos_tags else -1
|
22 |
+
# current_idx = pos_tags.index(current_tag) if current_tag in pos_tags else -1
|
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
|