user339108
user339108

Reputation: 13101

how to create exclamations for a particular sentence

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!

Upvotes: 6

Views: 493

Answers (6)

Adam Schmideg
Adam Schmideg

Reputation: 10860

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"); 

Upvotes: 1

sje397
sje397

Reputation: 41862

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).

Upvotes: 2

polygenelubricants
polygenelubricants

Reputation: 383886

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...

Upvotes: 8

Chad Brewbaker
Chad Brewbaker

Reputation: 2579

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.

Upvotes: 1

BernardMarx
BernardMarx

Reputation: 916

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.

Upvotes: 3

Jesper
Jesper

Reputation: 206916

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).

Upvotes: -2

Related Questions