File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Finding a Match in an Array and Printing a Reply

 
David Coello
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am building a chatterbot for a school project and am looking for a way to do the following:

1. Match any word from the inputline with a string in an array.

If someone types "Hello stranger", I want to find the array string that says "Hello". If a match is found, I want to reply "Greetings".
If then someone types "How are you today", I want to find the array string that says "you today". If a match is found, I want to reply "Great".

And try to create a conversation going.

I am not sure if to use a match regex function or contains function.

This is my code:





Thanks so much for your feedback.

David
 
Henry Wong
author
Marshal
Pie
Posts: 20833
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Coello wrote:
1. Match any word from the inputline with a string in an array.

If someone types "Hello stranger", I want to find the array string that says "Hello". If a match is found, I want to reply "Greetings".
If then someone types "How are you today", I want to find the array string that says "you today". If a match is found, I want to reply "Great".

And try to create a conversation going.

I am not sure if to use a match regex function or contains function.


From the description for the assignment, I would say that either option is possible.

Henry
 
Winston Gutkowski
Bartender
Pie
Posts: 9472
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Coello wrote:I am not sure if to use a match regex function or contains function.

Well, personally, I'm not a big fan of matches(), because it usually
(a) Makes the regex longer and and more complex.
(b) Runs slower.
It's also not the default style for grep, which is where we get regexes from.

I really wish they'd add a String.includes() method that takes a regex but, until they do, the equivalent is Matcher.find(). I suggest you read the API docs for more details.

HIH

Winston

PS: String.contains() is probably not what you want, since it doesn't use regexes; but you could use it for matching specific words (maybe).
 
Henry Wong
author
Marshal
Pie
Posts: 20833
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:PS: String.contains() is probably not what you want, since it doesn't use regexes; but you could use it for matching specific words (maybe).


True. The contains() method is simply a substring search, so no detection of word boundaries. However, with requirements like this ...

If someone types "Hello stranger", I want to find the array string that says "Hello". If a match is found, I want to reply "Greetings".


where the rest of the sentence is ignored, not detecting the word boundary is probably not high on the edge condition to take care of list...

Henry
 
Campbell Ritchie
Sheriff
Pie
Posts: 47232
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Different approach to try:
Split the string into an array using whitespace as a separator.
Put those individual Strings into a Collection, maybe a HashSet<String>
Use the contains() method of that Set.
 
Winston Gutkowski
Bartender
Pie
Posts: 9472
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Different approach to try:
Split the string into an array using whitespace as a separator.
Put those individual Strings into a Collection, maybe a HashSet<String>
Use the contains() method of that Set.

I have to admit, that has a very aesthetic appeal because it's so compact.

Winston
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic