Class: Nlp
- Inherits:
-
Object
- Object
- Nlp
- Defined in:
- lib/nlp.rb
Class Method Summary collapse
-
.closest_sentence(string, sentences) ⇒ Object
TODO: use real NLP, not this silly chunk of code.
- .split_into_tokens(string) ⇒ Object
Class Method Details
.closest_sentence(string, sentences) ⇒ Object
TODO: use real NLP, not this silly chunk of code
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/nlp.rb', line 4 def closest_sentence(string, sentences) string_tokens = split_into_tokens(string) string_tokens_size = string_tokens.size closest_sentence = nil closest_sentence_size = 0 sentences.each do |sentence| sentence_tokens = split_into_tokens(sentence) common_tokens = string_tokens & sentence_tokens bigger_sentence_size = [sentence_tokens.size, string_tokens_size].max if bigger_sentence_size - common_tokens.size == 1 && bigger_sentence_size > closest_sentence_size closest_sentence = sentence closest_sentence_size = bigger_sentence_size end end closest_sentence end |
.split_into_tokens(string) ⇒ Object
21 22 23 |
# File 'lib/nlp.rb', line 21 def split_into_tokens(string) string.gsub('?', '').downcase.split end |