• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Finding a Match in an Array and Printing a Reply

 
Greenhorn
Posts: 22
  • 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
 
author
Posts: 23840
141
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • 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
 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • 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
Posts: 23840
141
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • 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
 
Marshal
Posts: 65806
250
  • 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
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • 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
 
mooooooo ..... tiny ad ....
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!