• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Finding a Match in an Array and Printing a Reply

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 80278
432
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Mo-om! You're embarassing me! Can you just read a tiny ad like a normal person?
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic