aspose file tools*
The moose likes Beginning Java and the fly likes Finding a Match in an Array and Printing a Reply Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Finding a Match in an Array and Printing a Reply" Watch "Finding a Match in an Array and Printing a Reply" New topic
Author

Finding a Match in an Array and Printing a Reply

David Coello
Greenhorn

Joined: Jun 05, 2012
Posts: 22
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
Sheriff

Joined: Sep 28, 2004
Posts: 19073
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

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).


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19073
    
  40

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

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

Joined: Mar 17, 2011
Posts: 8427
    
  23

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Finding a Match in an Array and Printing a Reply