|
| 1 | +import nltk |
| 2 | +import pandas as pd |
| 3 | +from sklearn.neighbors import KNeighborsClassifier |
| 4 | +from sklearn.feature_extraction.text import CountVectorizer |
| 5 | +from sklearn.cross_validation import train_test_split |
| 6 | +# import and instantiate a Multinomial Naive Bayes model |
| 7 | +from sklearn.naive_bayes import MultinomialNB |
| 8 | +from sklearn import metrics |
| 9 | +from sklearn.datasets import load_iris |
| 10 | +iris = load_iris() |
| 11 | + |
| 12 | + |
| 13 | +def qst_type(test_qst): |
| 14 | + path = 'qstns.tsv' |
| 15 | + sent = pd.read_table(path, header=None, names=['qtype', 'sentance']) |
| 16 | + sent.shape |
| 17 | + sent['label_num'] = sent.qtype.map({'whqst':0, 'statement':1,'yesOrNo':2}) |
| 18 | + X = iris.data |
| 19 | + y = iris.target |
| 20 | + X = sent.sentance |
| 21 | + y = sent.label_num |
| 22 | + sent.head(5) |
| 23 | + |
| 24 | + |
| 25 | + X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1) |
| 26 | + |
| 27 | + vect = CountVectorizer() |
| 28 | + vect.fit(X_train) |
| 29 | + X_train_dtm = vect.transform(X_train) |
| 30 | + X_train_dtm = vect.fit_transform(X_train) |
| 31 | + X_train_dtm |
| 32 | + X_test_dtm = vect.transform(X_test) |
| 33 | + X_test_dtm |
| 34 | + |
| 35 | + |
| 36 | + nb = MultinomialNB() |
| 37 | + nb.fit(X_train_dtm, y_train) |
| 38 | + y_pred_class = nb.predict(X_test_dtm) |
| 39 | + #a=y_pred_class.tostring() |
| 40 | + #print (a) |
| 41 | + #metrics.accuracy_score(y_test, y_pred_class) |
| 42 | + |
| 43 | + |
| 44 | + test_qst_dtm = vect.transform(test_qst) |
| 45 | + test_qst_dtm |
| 46 | + y_pred_class = nb.predict(test_qst_dtm) |
| 47 | + a = y_pred_class |
| 48 | + if a == 0: |
| 49 | + c = "wh_questions" |
| 50 | + return(c) |
| 51 | + elif a == 1: |
| 52 | + c = ["statement"] |
| 53 | + elif a == 2: |
| 54 | + c == ["yes or no question"] |
| 55 | + |
| 56 | + return(a) |
| 57 | + |
| 58 | +#Just edit the question here and try type of qustions |
| 59 | +test_qst = ["Tell me city which is capital of India"] |
| 60 | +q_type = qst_type(test_qst) |
| 61 | +print(q_type) |
0 commit comments