SmitaGautam commited on
Commit
899e4f3
1 Parent(s): 880688e

Update svm_predict.py

Browse files
Files changed (1) hide show
  1. svm_predict.py +24 -23
svm_predict.py CHANGED
@@ -1,24 +1,25 @@
1
- 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('ner_svm_4_withpos_kaggle.pkl')
8
- nltk.download('averaged_perceptron_tagger_eng')
9
-
10
- def predict(sentence):
11
- tokens = word_tokenize(sentence)
12
- sent_pos_tags = pos_tag(tokens)
13
- predictions = []
14
- for idx, word in enumerate(tokens):
15
- prev_tag = -1 if idx==0 else sent_pos_tags[idx-1][1]
16
- next_tag = -1 if idx==len(tokens)-1 else sent_pos_tags[idx+1][1]
17
- current_tag = sent_pos_tags[idx][1]
18
- prev_idx = pos_tags.index(prev_tag) if prev_tag in pos_tags else -1
19
- next_idx = pos_tags.index(next_tag) if next_tag in pos_tags else -1
20
- current_idx = pos_tags.index(current_tag) if current_tag in pos_tags else -1
21
- vec = feature_vector(word, prev_idx, next_idx, current_idx)
22
- y_pred = model.predict([vec])
23
- predictions.append(y_pred[0])
 
24
  return tokens, predictions
 
1
+ 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('ner_svm_4_withpos_kaggle.pkl')
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
+ 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