Rezan Achmad
Rezan Achmad

Reputation: 1910

How to get relationship between noun phrases in a sentence?

I have this pattern to get cause-effect relationship between noun phrases in a sentence:

<NP I> * have * effect/impact on/in <NP II>

NP is Noun Phrase.

If I have a sentence:

Technology can have negative impact on social interactions

then based on above pattern, NP I match with Technology and NP II match with social interactions

The question: what is appropriate algorithm to get NP I and NP II?

Thanks

Upvotes: 0

Views: 630

Answers (1)

FThompson
FThompson

Reputation: 28687

Regular expression (RegEx) is extremely useful in cases like this. The following regex matches your string format and allows for you to analyze the differing variables of the input.

([\\w\\s]*?) (\\w*?) have (\\w*?) (effect|impact) (on|in) ([\\w\\s]*?)(\\.)

By running the following program, you can see how regex matcher groups work, and that group 1 is NP 1, and group 6 is NP 2.

public class Regex {

    public static void main(String[] args) {
        Pattern p = Pattern.compile("([\\w\\s]*?) (\\w*?) have (\\w*?) (effect|impact) (on|in) ([\\w\\s]*?)(\\.)");
        String s = "Greenhouse gases can have negative impact on global warming.";
        Matcher m = p.matcher(s);
        if (m.find()) {
            for (int i = 0; i < m.groupCount(); i++) {
                System.out.println("Group " + i + ": " + m.group(i));
            }
        }
    }
}

In the above example, the string "Greenhouse gases can have negative impact on global warming." is analyzed. The following is the output of the program.

Group 0: Greenhouse gases can have negative impact on global warming.
Group 1: Greenhouse gases
Group 2: can
Group 3: negative
Group 4: impact
Group 5: on
Group 6: global warming

Upvotes: 1

Related Questions