Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
max = 0.0
if !QaCorp.where("question like ?", "%#{self.text}%").empty?
# Exact question exist in the database.
answer = QaCorp.where("question like ?", "%#{self.text}%").first.answer
max = 1.0
else
corpus = QaCorp.all.map {|qa| TfIdfSimilarity::Document.new(qa.question)}
d_question = TfIdfSimilarity::Document.new(self.text)
corpus << d_question
model = TfIdfSimilarity::TfIdfModel.new(corpus, :library => :nmatrix)
matrix = model.similarity_matrix
puts "calculating matrix"
fixed_text = ""
puts "searching the nearest question-anser pair"
corpus.each do |doc|
# score is a number between 0.0 and 1.0
if d_question.text != doc.text
score = matrix[model.document_index(d_question), model.document_index(doc)]
puts "score is #{score}"
if score > max
max = score
fixed_text = doc.text
end
end
end
if max > 0.7 #make this a hyper parameter for the model
answer = QaCorp.where("question like ?", "%#{fixed_text}%").first.answer
else
answer = nil
end
end
puts max
answer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.