Skip to content

Instantly share code, notes, and snippets.

@Innarticles
Last active March 3, 2019 17:02
Show Gist options
  • Save Innarticles/99f884bd0f6a03ad112df49ad0c733ba to your computer and use it in GitHub Desktop.
Save Innarticles/99f884bd0f6a03ad112df49ad0c733ba to your computer and use it in GitHub Desktop.
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