Artificial Intelligence or Bot in Ruby/Rails [closed] - ruby

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
This is more of a vague/general question, so for that I do apologize in advance. I'm building a simple Rails app with Twilio integration that will allow users to send messages such as "how are you" and receive a quasi-intelligent response back from the app such as, "Good, how about yourself, John?".
I have everything wired up and it works but I was wondering if anyone could point me in the right direction of writing an algorithm in Ruby that would make this "bot" smarter. Right now I'm using a pretty straight forward if/elsif/else chain to parse the payload and deliver the proper response but this does not seem maintainable once I get past 5-10 keywords.
Would I be better off with a case statement (at least for readability) or is there a better OOP design pattern that would help me match my keywords and deliver a certain response?

It depends. If you want to write a real chat bot, prepare for 5+ years of reading papers on neural networks. Might as well just give up now :)
However, if you reduce your requirements (make the bot recognize only a few selected keywords, with predefined response(s) for each), then a simple dictionary approach could suffice.
You're right, storing the dictionary in code is not scalable. It's better to store the knowledge in a data file (YAML, JSON or whatever you prefer) or a database. Then your code will load the file and will be able to look up responses by keywords.
Something like this:
def reply(input)
# you load this from a storage, so that when you add new keywords,
# your code doesn't have to be touched.
knowledge = [
{ keyword: 'how are you', response: 'Good, how about yourself, %{name}?' },
{ keyword: 'bye', response: 'Ciao!' },
]
response = knowledge.detect do |pair|
input.downcase.include?(pair[:keyword].downcase)
end
response && response[:response]
end
reply('How are you doing, machine?') # => "Good, how about yourself, %{name}?"
reply('gotta go, bye') # => "Ciao!"

Related

golang structs or maps in RESTFUL API [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
When creating a webserver in golang, is there any specific reason why JSON data would be represented as a MAP over a STRUCT or vice versa? ..or is this decision based purely on user preference ?
I think this discussion could go either way but the advantages of using structs vs maps are that structs give you an idea of how the schema should look like whereas a map leaves the schema open-ended.
If you use structs, developers who look at the code will have a clear idea of what parameters you're expecting for the API or how the response of the API may look like without digging further into implementation detail. On the other hand if the requests or responses were maps, they would have to look at the implementation detail to see what keys and values are being assigned. Hope this helps!

A generic algorithm for extracting product data from web pages [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Preface: this might seem to be a very beginner-level question maybe stupid or ill-formulated. That's why I don't require a determined answer, but just a hint, a point, which I can start with.
I am thinking of script, which would allow me to parse product pages of different online retailers, such as Amazon, for instance. The following information is to be extracted from the product page:
product image
price
availability (in stock/out of stock)
The key point in the algorithm is that, once implemented, it should work for any retailer, for any product page. So it is pretty universal.
What techniques would allow implementation of such an algorithm? Is it even possible to write such a universal parser?
If the information on the product page is marked up in a structured, machine-readable way, e.g. using schema.org microdata, then you can just parse the page HTML into a DOM tree, traverse the tree to locate the microdata elements, and extract the data you want from them.
Unfortunately, many sites still don't use such structured data markup — they just present the information in a human-readable form, with no consideration given for machine parsing. In such cases, you'll need to customize your data extraction code for each site, so that it knows where the information you want is located on the page. Parsing the HTML and then working with the DOM is still often a good first step, but the rest will have to be site-specific (and may need to be updated whenever the site changes its design).
Of course, you can also try to come up with heuristic methods for locating relevant data, like, say, assuming that a number following a $ sign is probably a price. Of course, such methods are also likely to occasionally produce incorrect matches (like, say, mistaking the "$10" in "Order now and save $10!" for a price). You can adjust and refine your heuristics to be smarter about such things, but no matter how good you get at it, there will always be some new and unexpected cases that you haven't anticipated.

Confused about Ruby code placement [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
So, I am getting into Ruby. I'm learning it everyday. And just like Spanish. I am getting able to read it, but not write it.
I am doing "Ruby the Hard Way" and I understand WHY things work, but the more I move through it the more I realize that I could not mimic the code if someone just came up to me and said "I need you to do so and so in Ruby."
I know that it is going to depend largely on what the task is, is how my code will be set up. But are there any tips and/or tricks for Ruby. (i.e. "Always write your variables first or strings before arrays" or something like that.
I'm unclear on when to write which lines of code or WHY something has to be below another block of code. I am aware of the nebulous nature of this question, but I'm looking for a bit of more broad rules for Ruby.
I think the WHY in your question has to do with learning the concepts, but not really applying them in a way that's meaningful to you.
I'd recommend finding a task you can accomplish with Ruby. Eg: write to a file, get content out of a webpage, get the date/time in a specific country, whatever. Start small, but challenge yourself. Look at StackOverflow ruby tag, or Github to see what other people are doing with Ruby.
That will get you thinking about the problem you're solving, and not the code alone; which is what programming is all about.
Then come back here and ask about the specifics you're struggling with.
Small disclaimer: yes, all of the above is my opinion; and as others have commented, this question is too broad.
The question is really too broad. I'll suppose that you know of another language already. If you are looking for code convention, look at the Ruby Style Guide. Taking courses at Code School could get you an idea of the good practice (and make you learn useful frameworks like Rails. Reading "The Ruby Way" can give you a feeling of the "pulse" of the language. Building the blog application used by many Rails book could also help.
Finally, checkout sample Rails or Ruby projects from GitHub.

Auto-Completion [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
How does Google or amazon implement the auto-suggestion at their search box. I am looking for the algorithm with technology stack.
PS: I have searched over the net and found this and this and many many more. But I am more interested in not what they do but how they do it. NoSQL database to store the phases? or is it sorted or hashed according to keyword's? So to rephrase the question: Given the list of different searches ignoring personalization, geographic-location etc, How do they store, manage and suggest it so well.
This comes under the domain of stastical language processing problems. Take a look at spelling suggestion article by Norvig. Auto - completion will use a similar mechanism.
The idea is that from past searches, you know the probability of phrases (or better called bigram, trigram, ngram). For each such phrase, the auto complete selects the one having max value of
P(phrase|word_typed) = P(word_typed|phrase) P(phrase) / P(word_typed)
P(phrase|word_typed) = Probability that phrase is right phrase if word typed
so far is word_typed
Norvig's article is a very accessible and great explanation of this concept.
Google takes your input and gives TOP4 RESULTS ACCORDING TO THE RANK IDs [if results are less it returns parameters as empty strings] given to different keywords which differ dynamically by the hit and miss count.
Then, they make a search query and returns 4 fields with url, title, and 2 more fields in Json, omnibox then populates the data using prepopulate functions in Chrome trunk.

how to keep my infrequently used programming language skills in shape [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
I use ruby infrequently - usually it adds up to writing a script once in two months or more. I do most of my programming with C++, which is very different from ruby.
with such wide gaps between my brushes with ruby I keep forgetting basic aspects of the language (like parsing a text file and other simple stuff).
I would like to do a daily drill of the basic stuff and I was wondering if there is some site I can subscribe to and will send me the Ruby question of the day or something similar.
anyone knows of such a site / Internet service?
It's not daily, but you might be interested in Ruby Quiz.
You might also subscribe to ruby-talk and look over the posts there each day.
Check out Jim Weirich's ruby koans. It's a set of ruby scripts organized by topic that guides you through the different parts of the language by unit testing your knowledge.
def method_with_block
result = yield
result
end
def test_methods_can_take_blocks
yielded_result = method_with_block { 1 + 2 }
assert_equal __, yielded_result
end
The game is to go through these and fill in the __ blanks. Running rake will check your answers.
What about a general problem site like Project Euler( http://projecteuler.net/ ) or the ACM programming competition problem sets ( http://www.inf.bme.hu/contest/tasks/ ) and just restrict yourself to using ruby?
A more arduous (but awesome) task is the google code jam. Just assign yourself one of the problems, and set a time per week to put an hour into it. Quit while you are still working. That way, you hunger for more, and think about it in the interim time.
http://code.google.com/codejam
TL;DR: Find a large task, that is interesting. Work on it in bite-size pieces, leaving yourself hungry for more.

Resources