Results 1 to 9 of 9
Like Tree2Likes
  • 2 Post By JosAH

Thread: Matching words in a sentence in java to follow instructions

  1. #1
    CodeX Pro is offline Member
    Join Date
    Dec 2012
    Posts
    32
    Rep Power
    0

    Default Matching words in a sentence in java to follow instructions

    Hello All,

    I am currently working on a web project and I have come across some problem and I need some help. I wanted to ask is is there some way by which we could parse a sentence for certain words and do what they ask for.

    example :-

    A user gives the string "Add the values 5 and 6", so when this is given as input the program will try to parse thee meaning of it and then calculate the result and give the output as 11.

    other ways can be :- "Add 5 + 6" or "add the following 5,5,6 and 9" here , are used to separate the numbers and "and" states that 9 is the last number, also add is an instruction. It can be multiply as well.

    If a user gives "subtract 6 from 7" then the result would be 1.

    Can you provide some ways by which this can be implemented or what procedure can be followed.

    Another question is that, how can I identify the nested functions.

    Suppose I give add(5+6,7+6) so this should evaluate to 24. Note that if I pass add(11,13) then it would be okay to a certain point but how to parse the inner variables as well. Another concern is that if I do --- add("Hello ","World") , then the output should be "Hello World" and also the input can be variable and it shouldn't be limited to two or three arguments.


    I hope to get some help.

    Thank you.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,864
    Rep Power
    8

    Default Re: Matching words in a sentence in java to follow instructions

    This approaches something called natural language processing and isn't an easy problem. Worth a google though.

    Start small. Work on the most basic case and get that working, then worry about adding functionality as you go.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,376
    Rep Power
    5

    Default Re: Matching words in a sentence in java to follow instructions

    First I notice in your second example the + sign is redundant unless you are specifying the unary operator. And what about "add 5 - 6" or add 5 to negative 6.

    You have broached upon the topic of lexical analysis. It is not trivial to do even for a language of limiited vocabulary and syntax (like Java or C). You could do things like parse for keywords such as add and subtract. But there may always be ambiguities. Perhaps there is a package which makes this easier but I don't know of any myself.

    Typically you would first need to define a grammar and then generate a parser using something like ANTLR or JAVACC (or YACC or BISON for C).

    It's not easy (imho) but it can be done.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    CodeX Pro is offline Member
    Join Date
    Dec 2012
    Posts
    32
    Rep Power
    0

    Default Re: Matching words in a sentence in java to follow instructions

    Quote Originally Posted by jim829 View Post
    First I notice in your second example the + sign is redundant unless you are specifying the unary operator. And what about "add 5 - 6" or add 5 to negative 6.

    You have broached upon the topic of lexical analysis. It is not trivial to do even for a language of limiited vocabulary and syntax (like Java or C). You could do things like parse for keywords such as add and subtract. But there may always be ambiguities. Perhaps there is a package which makes this easier but I don't know of any myself.

    Typically you would first need to define a grammar and then generate a parser using something like ANTLR or JAVACC (or YACC or BISON for C).

    It's not easy (imho) but it can be done.

    Regards,
    Jim
    Yes I have been reading about compiler designing and things like JavaCC or yacc etc. The thing is I want to have a simple parser and lexical analyzer of my own. I am working on it hard. Only if i get some references then it would be just great. I don't want to use external libraries.

  5. #5
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,376
    Rep Power
    5

    Default Re: Matching words in a sentence in java to follow instructions

    Then you should do as Kevin suggested. Search the web for assistance. You may want to read up on state tables. Essentially, the parser generators like YACC generate huge state tables based on some user defined grammar. And usually there are two parts to it. First is simply getting the tokens (which would be done with tools such as Lex). The second is evaluating the context of those tokens and taking specific action (usually done by the parser).

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: Matching words in a sentence in java to follow instructions

    My feeling tells me that using JavaCC or ANTLR or similar, would be overkill; my guess is that the grammar for this all is simple enough to write a dedicated parser for this all. Maybe it wouldn't recognize each an every variant, but that can be fiddled with ;-) For starters, consider the verbs as prefix forms of the mathematical operators, i.e. <X>+<Y> == add <X> and <Y> etc.

    kind regards,

    Jos
    gimbal2 and CodeX Pro like this.
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    CodeX Pro is offline Member
    Join Date
    Dec 2012
    Posts
    32
    Rep Power
    0

    Default Re: Matching words in a sentence in java to follow instructions

    Quote Originally Posted by JosAH View Post
    My feeling tells me that using JavaCC or ANTLR or similar, would be overkill; my guess is that the grammar for this all is simple enough to write a dedicated parser for this all. Maybe it wouldn't recognize each an every variant, but that can be fiddled with ;-) For starters, consider the verbs as prefix forms of the mathematical operators, i.e. <X>+<Y> == add <X> and <Y> etc.

    kind regards,

    Jos
    I was thinking something similar to what you said but when implementing them it gets a bit tricky when we consider the other possible cases.

  8. #8
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,376
    Rep Power
    5

    Default Re: Matching words in a sentence in java to follow instructions

    You're probably correct. But I was taking it to extremes. Personally, I would just go for entering mathematical expressions to be evaluated. But that might not satisfy the OP's requirements.

    Regards,
    Jim
    Last edited by jim829; 08-29-2013 at 03:36 PM. Reason: YAT
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: Matching words in a sentence in java to follow instructions

    Quote Originally Posted by CodeX Pro View Post
    I was thinking something similar to what you said but when implementing them it gets a bit tricky when we consider the other possible cases.
    That's the art of designing proper grammars; from the top of my head:

    Java Code:
    <E> := add <E> { , <E> }* and <E>
    <E> := subtract <E> { , <E> }* from <E>
    <E> := <T> { { + | - } <T> }*
    <T> : multiply <F> { , <F> }* and <F>
    <T> : divide <F> by <F> { , <F> }* 
    <T> := <F>  { { * | / } <F> }*
    <F> <number> | ( <E> )
    I hope you get the idea ...

    kind regards,

    Jos
    Last edited by JosAH; 08-29-2013 at 08:40 PM.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. how to reverse the order of words in a sentence?
    By dksellou in forum New To Java
    Replies: 5
    Last Post: 11-29-2012, 04:34 AM
  2. Replies: 0
    Last Post: 09-14-2012, 09:34 AM
  3. Replies: 3
    Last Post: 07-08-2012, 11:08 PM
  4. personalizing instructions
    By _inase in forum New To Java
    Replies: 4
    Last Post: 02-02-2011, 02:35 AM
  5. Replies: 0
    Last Post: 02-25-2010, 12:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •