Я хочу оценивать / оценивать отклики разных пользователей. Для этого я использовал Multinomial navie bayes
. Ниже мой код.
# use natural language toolkit
import nltk
from nltk.stem.lancaster import LancasterStemmer
import os
import json
import datetime
stemmer = LancasterStemmer()
# 3 classes of training data
training_data = []
# capture unique stemmed words in the training corpus
class_words={}
corpus_words = {}
classes = list(set([a['class'] for a in training_data]))
for c in classes:
class_words[c] = []
for data in training_data:
# tokenize each sentence into words
for word in nltk.word_tokenize(data['sentence']):
# ignore a few things
if word not in ["?", "'s"]:
# stem and lowercase each word
stemmed_word = stemmer.stem(word.lower())
if stemmed_word not in corpus_words:
corpus_words[stemmed_word] = 1
else:
corpus_words[stemmed_word] += 1
class_words[data['class']].extend([stemmed_word])
# we now have each word and the number of occurances of the word in our training corpus (the word's commonality)
print ("Corpus words and counts: %s" % corpus_words)
# also we have all words in each class
print ("Class words: %s" % class_words)
sentence="The biggest advantages to a JavaScript having a ability to support all modern browser and produce the same result."
def calculate_class_score(sentence, class_name):
score = 0
for word in nltk.word_tokenize(sentence):
if word in class_words[class_name]:
score += 1
return score
for c in class_words.keys():
print ("Class: %s Score: %s" % (c, calculate_class_score(sentence, c)))
# calculate a score for a given class taking into account word commonality
def calculate_class_score_commonality(sentence, class_name):
score = 0
for word in nltk.word_tokenize(sentence):
if word in class_words[class_name]:
score += (1 / corpus_words[word])
return score
# now we can find the class with the highest score
for c in class_words.keys():
print ("Class: %s Score: %s" % (c, calculate_class_score_commonality(sentence, c)))
def find_class(sentence):
high_class = None
high_score = 0
for c in class_words.keys():
score = calculate_class_score_commonality(sentence, c)
if score > high_score:
high_class = c
high_score = score
return high_class, high_score
Примечание: Я не добавил тренировочных данных.
Когда я даю ввод как
find_class("the biggest advantages to a JavaScript having a ability to
support all modern browser and produce the same result.JavaScript
small bit of code you can test")
Я получаю вывод как
('Advantages', 5.07037037037037)
Но когда я даю ввод как
find_class("JavaScript can be executed within the user's browser
without having to communicate with the server, saving on bandwidth")
Я получаю ответ / вывод как
('Advantages', 2.0454545)
Я создаю его для вопросов об интервью с JavaScript / viva.
Когда пользователь вводит один и тот же ответ другим способом, как я упоминал выше, я получаю разные оценки. Я хочу, чтобы результаты были точными. Как мне это сделать.