Advertisement
agunq

smart parsing ? (i guess yes)

Aug 1st, 2017
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.66 KB | None | 0 0
  1. text = """Rubular is a Ruby-based regular expression editor. It's a handy way to test regular expressions as you write them.
  2.  
  3. To start, enter a regular expression and a test string. Or you can try an example.
  4. """
  5. tes = "st"
  6. rtext = text.scan(/[#{tes}]+/).sort_by{|e| e.size}[-1]
  7. puts tes + ", did you mean? " + text.split(" ").collect{|text| text if text.downcase =~ %r"^#{rtext}"}.compact[-1]
  8. puts "done"
  9.  
  10. text = """Rubular is a Ruby-based regular expression editor. It's a handy way to test regular expressions as you write them.
  11.  
  12. To start, enter a regular expression and a test string. Or you can try an example.
  13. """
  14. tes = "eprestion"
  15. tlist = text.split(" ").collect{|text|
  16.     rtext = text.scan(/[#{tes}]+/).sort_by{|e| e.size}[-1]
  17.     if rtext != nil
  18.         [text, rtext]
  19.     end
  20.     }.compact
  21. puts tes + ", did you mean? " + tlist.sort_by{|te, rt| rt.size}[-1][0]
  22. puts "done"
  23.  
  24. class AI
  25.     def initialize
  26.         @text = {"apa kabar"=>["kabar baik"],
  27.             "siapa kamu"=>["aku bot", "hi aku bot"],
  28.             "selamat pagi"=>["pagi!", "pagi juga", "selamat pagi"]
  29.             }
  30.     end
  31.     def talk(input)
  32.         output = []
  33.         @text.each do |text, response|
  34.             if input == text
  35.                 return response
  36.             elsif text.include? input
  37.                 return response
  38.             else
  39.                 rtext = text.scan(/[#{input}]+/).sort_by{|e| e.size}[-1]
  40.                 if rtext != nil
  41.                     output << [response, rtext]
  42.                 end
  43.             end
  44.         end
  45.         return output.sort_by{|te, rt| rt.size}[-1][0]
  46.     end
  47. end
  48. ai = AI.new
  49. puts ai.talk("apa kabar").sample
  50. puts "done"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement