views:

337

answers:

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!

+7  A: 
polygenelubricants
there's a compilation error in your code on ideone.com...
Jesper
Also see variation like this: http://ideone.com/Xl20I - This still qualifies as dumb regex, though.
polygenelubricants
A: 

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
But with regex you can do dumb things like this: http://ideone.com/Xl20I
polygenelubricants
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
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
+3  A: 

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
we are not quite familiar with prolog, will check to see if someone can investigate this option.
+1  A: 

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
Isn't it available only in python?
dierre
Yes, NLTK is pure Python.
Tim McNamara
On the other hand, Python works nicely under Java via Jython...
tucuxi
+2  A: 

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
+1  A: 

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