6

I would like to create exclamations for a particular sentence using the java API?

e.g. It's surprising == Isn't it surprising!
e.g. It's cold == Isn't it cold!

Are there any vendors or tools which help you generate exclamations, provided you give a sentence (i.e. the left hand side in the above example). Note: The sentences will be provided by the user and we should be able to get the correct sentence.

I am not sure, if this needs to be tagged under other categories

EDIT1

Some more examples, I would like this to be as generic as possible

e.g. They're late == Aren't they late!
e.g. He looks tired == Doesn't he look tired!
e.g. That child is dirty == Isn't that child dirty!
e.g. It's hot == Isn't it hot!

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
user339108
  • 12,613
  • 33
  • 81
  • 112
  • 6
    You want this to be grammatically aware, or is a dumb regex substitution acceptable? – polygenelubricants Jun 23 '10 at 11:34
  • 1
    Is there really such thing as a single "correct sentence" for these inputs? "It's cold!" is also grammatically correct. – matt b Jun 23 '10 at 11:40
  • I would like to have a grammatically correct sentence, if there are multiple ones then it's fine with me. Note: I have added more examples – user339108 Jun 23 '10 at 17:04
  • The solutions given below, solve just a possible scenario from the above use case. Would it be possible to do this in a generic fashion – user339108 Jul 03 '10 at 07:43
  • 4
    Looking at these exclamation forms, I think you should prefix each answer with one of "Verily," "Forsooth,", "Ye Gods,", or "Great Caesar's Ghost,". – PaulMcG Jul 31 '10 at 05:20
  • Yes, this will be like self learning engine which will learn as it goes on different texts... It must be trained before also which is concept of Artificial Intelligence(Like OCR kind of thing). You need to put effort for grammar engine which can parse text, recognize the grammer changes (Like MSWord sometimes gives) and change the sentence. And also stores data related to this change in the engine. – Parth Aug 04 '10 at 05:20

6 Answers6

8

Depending on how "smart" and "sophisticated" you want this to be, this can be either very hard or very easy problem. Here's a simple regex solution that is quite dumb:

    String[] sentences = {
        "It's surprising",
        "It's cold",
        "It's $*($&%!",
        "That is a hot coffee indeed..."
    };
    for (String sentence : sentences) {
        System.out.println(
            sentence.replaceAll("It's (.+)", "Isn't it $1!")
        );
    }

This prints (as seen on ideone.com):

Isn't it surprising!
Isn't it cold!
Isn't it $*($&%!!
That is a hot coffee indeed...
Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
3

I don't think you will get very far with simple regex constructions. The problem is that since you are obviously operating in the natural language domain there are many, many possibilities you have to take into consideration. How general does the solution have to be?

I know you said it something like this is possible with the Java API, but would using Prolog be an option? SWI-Prolog has a Java interface (JPL) and the problem you are describing would be much better solved in Prolog. Infact this is the kind of problem Prolog does best and is used in academia for. SWI-Prolog even includes a package for natural language processing (http://www.swi-prolog.org/pldoc/package/nlp.html). This is the best way I am aware of to handle a problem as yours.

Ofcourse I do not know how important this feature is to your product/project and using Prolog probably isn't an option, so your other option is to write a parser that would extract verb/noun etc and create a corresponding "sentence model" ( aka group of objects). Then you could transform this sentence model into another sentence model based on some rules, designed in an extensible way, so that when new cases pop-up ( and with such a wide domain they will) you can just add a new "rule" to your transformation.

This is indeed a non-trivial solution, but I can't imagine how a trivial solution might look like.

BernardMarx
  • 916
  • 8
  • 17
2

This question is not about exclamations. You can just add '!' to all your input examples and get valid exclamatory sentences.

You are after grammar transformations, like these.

LingPipe looks like it has some intersting stuff that you could use (it's java), particularly if you are developing a learning system, and need to recognise 'parts of speach' (like e.g. subject and verb phrase, as per your examples).

sje397
  • 41,293
  • 8
  • 87
  • 103
1

Look at the Natural Language ToolKit, then refine your question to what subset of the English language you want your code to work with and a clearer definition of the types of exclamation translation you want.

Chad Brewbaker
  • 2,523
  • 2
  • 19
  • 26
1

Here is my take with regexps only, no deep language analysis. It can be tricked easily, but it handles most of your examples.

s.replace("(.+?)('re| are) (.+)", "Aren't $1 $3!")
.replace("(.+?)('s| is) (.+)", "Isn't $1 $3!")
.replace("(I|You|We|They) (.+)", "Don't $1 $2!")
.replace("(He|She|It) (\\w+)s (.*)", "Doesn't $1 $2 $3!")
// correct case
.replace(" You", " you")
.replace(" He", " he")
.replace(" She", " she")
.replace(" It", " it")
.replace(" We", " we")
.replace(" They", " they"); 
Adam Schmideg
  • 10,590
  • 10
  • 53
  • 83
-2

I don't know how sophisticated you want this to be, but if you just want to change expressions like "It's whatever" to "Isn't it whatever!", then this is very simple:

String text = "It's cold";
String result = "Isn't it " + text.substring(5) + "!";

(Even simpler than polygenelubricant's solution with regular expressions).

Jesper
  • 202,709
  • 46
  • 318
  • 350
  • Ofcourse regexes are a lot more flexible and capable than my simple solution, but we don't know how far the requirements of the poster go, so I thought I'd present the simplest possible solution based on what he asked. – Jesper Jun 23 '10 at 13:07
  • I want this to be more generic based on the input, would it be possible to do this? Are there any other toolkits which support generation of such exclamations – user339108 Jun 23 '10 at 17:17