Example usage for edu.stanford.nlp.trees Tree value

List of usage examples for edu.stanford.nlp.trees Tree value

Introduction

In this page you can find the example usage for edu.stanford.nlp.trees Tree value.

Prototype

@Override
    public String value() 

Source Link

Usage

From source file:Anaphora_Resolution.ParseAllXMLDocuments.java

private static boolean existentialEmphasis(Tree candidate) {
    // We want to check whether our NP's Dets are "a" or "an".
    for (Tree child : candidate) {
        if (child.label().value().equals("DT")) {
            for (Tree leaf : child) {
                if (leaf.value().equals("a") || leaf.value().equals("an") || leaf.value().equals("A")
                        || leaf.value().equals("An")) {
                    //System.out.println("Existential emphasis!");
                    return true;
                }//w w w.  j a  v a  2  s.c o m
            }
        }
    }
    return false;
}

From source file:com.project.NLP.Requirement.PhrasesIdentification.java

/**
 * method to extract the classes from a sentence
 *
 * @return ArrayList: arrayList of classes from a sentence
 *//*from  w  w w . j  av  a  2 s  .com*/
public ArrayList getClassList() {
    nounList = new ArrayList();
    attributeLists = new ArrayList();
    int adjectiveExist = 0;
    int adjectiveNoun = 0;
    String adj = "";
    String storingClass = "";
    HashSet classWithAttr = new HashSet();
    storingClassWithAttr = new HashMap<String, HashSet>();

    List<Tree> leaves;
    String phraseNotation = "(NP([<NNS|NN|NNP]$VP))";//@" + phrase + "! << @" + phrase;

    /*For the single Tree */
    TregexPattern VBpattern = TregexPattern.compile(phraseNotation);
    TregexMatcher matcher = VBpattern.matcher(sTree);
    String tempClass = "";

    while (matcher.findNextMatchingNode()) {
        Tree match = matcher.getMatch();
        Tree[] innerChild = match.children();
        adjectiveExist = 0;
        adjectiveNoun = 0;
        int separator = 0;

        if (innerChild.length > 1) {
            int count = 1;
            int loopCount = 1;
            for (Tree inChild : innerChild) {
                if (inChild.value().equals("CC")) {
                    separator = 1;
                }
                if ((inChild.value().equals("JJ")) || (inChild.value().equals("VBG"))) {
                    adjectiveExist++;
                    leaves = inChild.getLeaves();
                    adj = leaves.get(0).yieldWords().get(0).word();
                    if (dictionaryForClassList.contains(adj)) {
                        adj = "";
                    }
                }
                //if adjective exist store the classes and attributes separately
                if (adjectiveExist == 1) {
                    storeClassesAndAttributesWhenAdjectiveExistToIdentifyClasses(inChild, adjectiveNoun, adj);
                } else {
                    //storeClassesAndAttributesWhenAdjectiveNotExistToIdentifyClasses(inChild, loopCount, innerChild, separator, tempClass, count);
                    if ((inChild.value().equals("NN"))
                            || (inChild.value().equals("NNS") || (inChild.value().equals("NNP")))) {
                        leaves = inChild.getLeaves(); //leaves correspond to the tokens
                        if (separator == 0) {
                            if (loopCount == innerChild.length) {
                                String identifiedWord = ((leaves.get(0).yieldWords()).get(0).word());
                                String word = "";
                                word = stemmingForAWord(identifiedWord);
                                if (!dictionaryForClassList.contains(word)) {
                                    nounList.remove(tempClass);
                                    nounList.add(word);
                                    attributeLists.add(tempClass);

                                }

                            } else if (count == 1) {
                                String identifiedWord = ((leaves.get(0).yieldWords()).get(0).word());
                                /*if the identified word is having underscore skips the stemming part . ex: user_id*/
                                String word = stemmingForAWord(identifiedWord);
                                nounList.add(word);
                                tempClass = word;
                                storingClass = word;

                            } else {
                                /*if the identified word is having underscore skips the stemming part . ex: user_id*/
                                if (tempClass.contains("_")) {
                                    nounList.remove(tempClass);
                                } else {
                                    nounList.remove(morphology.stem(tempClass));
                                    nounList.remove(tempClass);
                                }
                                String identifiedWord = ((leaves.get(0).yieldWords()).get(0).word());

                                tempClass += " " + identifiedWord;
                                nounList.add(tempClass);
                                storingClass = tempClass;
                            }

                            count++;
                        } else {
                            String identifiedWord = ((leaves.get(0).yieldWords()).get(0).word());
                            /*if the identified word is having underscore skips the stemming part . ex: user_id*/
                            String word = stemmingForAWord(identifiedWord);
                            nounList.add(word);
                            tempClass = word;
                            storingClass = word;
                        }
                    }

                }
                loopCount++;
            }
        } else {
            for (Tree inChild : innerChild) {
                if ((inChild.value().equals("NN")) || (inChild.value().equals("NNS"))
                        || (inChild.value().equals("NNP"))) {
                    leaves = inChild.getLeaves(); //leaves correspond to the tokens
                    String identifiedWord = ((leaves.get(0).yieldWords()).get(0).word());
                    if (!identifiedWord.contains("_")) {
                        nounList.add(morphology.stem(identifiedWord));
                    } else {
                        nounList.add(identifiedWord);
                    }
                }
                if (inChild.value().equals("JJ")) {
                    //leaves correspond to the tokens
                    leaves = inChild.getLeaves();
                    nounList.add(((leaves.get(0).yieldWords()).get(0).word()));
                }
            }
        }
    }
    System.out.println("NOUN LIST :" + nounList);
    return nounList;
}

From source file:com.project.NLP.Requirement.PhrasesIdentification.java

/**
 * method to identify the classes and attributes when adjective exist
 *
 * @param inChild/* www  . j  av a 2 s .c  om*/
 * @param adjectiveNoun
 * @param adj
 */
private void storeClassesAndAttributesWhenAdjectiveExistToIdentifyClasses(Tree inChild, int adjectiveNoun,
        String adj) {
    List<Tree> leaves;
    if ((inChild.value().equals("NN")) || (inChild.value().equals("NNS")) || (inChild.value().equals("NNP"))) {
        leaves = inChild.getLeaves();
        adjectiveNoun++;
        String className = "";
        if (adjectiveNoun == 1) {
            className = leaves.get(0).yieldWords().get(0).word();
            nounList.add(adj + " " + className);
        } else {
            attributeLists.add(leaves.get(0).yieldWords().get(0).word());
        }

    }
}

From source file:com.project.NLP.Requirement.PhrasesIdentification.java

/**
 * method to identify the attributes using the tokenization
 *
 * @return ArrayList: arrayList of attributes
 *//*from   w ww.  ja  v  a2s.c  om*/
public ArrayList getAttributeList() {
    nounList = new ArrayList();
    attributeLists = new ArrayList();
    ArrayList adjAtt = new ArrayList();
    int separator = 0;
    List<Tree> leaves;
    String phraseNotation = "NP([<NNS|NN|NNP]![<JJ|VBG])!$VP";// !<VBG";//@" + phrase + "! << @" + phrase;

    TregexPattern VBpattern = TregexPattern.compile(phraseNotation);
    TregexMatcher matcher = VBpattern.matcher(sTree);

    while (matcher.findNextMatchingNode()) {
        Tree match = matcher.getMatch();
        Tree[] innerChild = match.children();
        int adjectiveExist = 0;
        String adj = "";
        String attribute = "";
        String b = "";

        if (innerChild.length > 1) {
            int count = 1;

            for (Tree inChild : innerChild) {
                if (inChild.value().equals("CC") || inChild.value().equals(",")) {
                    separator = 1;
                }
                if ((inChild.value().equals("JJ")) || (inChild.value().equals("VBG"))) {
                    adjectiveExist++;
                    leaves = inChild.getLeaves();
                    adj = leaves.get(0).toString();
                    if (designEleList.contains(adj)) {
                        adj = "";
                    }
                }
                if ((inChild.value().equals("NN")) || (inChild.value().equals("NNS"))
                        || (inChild.value().equals("NNP"))) {
                    leaves = inChild.getLeaves(); //leaves correspond to the tokens
                    if (count == 1) {
                        if (adjectiveExist == 1) {
                            attribute = adj + " " + leaves.get(0).yieldWords().get(0).word();
                        } else {
                            attribute = leaves.get(0).yieldWords().get(0).word();
                        }
                        if (!designEleList.contains(attribute)) {
                            String identifiedWord = attribute;
                            if (!identifiedWord.contains("_")) {
                                attributeLists.add(morphology.stem(identifiedWord));
                            } else {
                                attributeLists.add(identifiedWord);
                            }
                        }

                    } else if (count >= 2 && separator == 0) {
                        if (!attribute.contains("_")) {

                            attributeLists.remove(morphology.stem(attribute));
                            attributeLists.remove(attribute);
                        } else {
                            attributeLists.remove(attribute);
                        }

                        attribute += " " + (leaves.get(0).yieldWords()).get(0).word();
                        attributeLists.add(attribute);
                    } else if (count >= 2 && separator == 1) {
                        attribute = (leaves.get(0).yieldWords()).get(0).word();
                        if (!attribute.contains("_")) {
                            attributeLists.add(morphology.stem(attribute));
                        } else {
                            attributeLists.add(attribute);
                        }
                        separator = 0;
                    }
                    count++;
                }
            }
        } else {
            for (Tree inChild : innerChild) {
                if ((inChild.value().equals("NN")) || (inChild.value().equals("NNS"))) {
                    leaves = inChild.getLeaves(); //leaves correspond to the tokens
                    String identifiedWord = ((leaves.get(0).yieldWords()).get(0).word());
                    if (!identifiedWord.contains("_")) {
                        attributeLists.add(morphology.stem(identifiedWord));
                    } else {
                        attributeLists.add(identifiedWord);
                    }
                }
            }
        }
    }
    adjAtt = getAdjectiveAttribute();
    if (!adjAtt.isEmpty()) {
        String att = "";
        for (int i = 0; i < adjAtt.size(); i++) {
            att = adjAtt.get(i).toString();
            if (!att.isEmpty() || !att.equals("") || !(att.equals(" "))) {
                attributeLists.add(att.trim());
            }
        }
    }

    System.out.println("ATTRIBUTE LIST :" + attributeLists);
    return attributeLists;

}

From source file:com.project.NLP.Requirement.PhrasesIdentification.java

/**
 * method to identify the attributes when the words which are identifies as
 * nouns are in adjective phrases//from   ww w.j a va  2 s .  c om
 *
 * @return ArrayList of adjective attributes
 */
public ArrayList getAdjectiveAttribute() {
    adjAttributeList = new ArrayList();
    //adjAttributeList = new ArrayList();

    int adjectiveExist = 0;
    int adjectiveNoun = 0;
    int nnCount = 0;
    String adj = "";
    List<Tree> leaves;
    String phraseNotation = "NP[<NNS|NN]!$VP";//@" + phrase + "! << @" + phrase;
    DesignElementClass designEle = new DesignElementClass();
    ArrayList designEleList = designEle.getDesignElementsList();

    /*For single Tree  */
    TregexPattern VBpattern = TregexPattern.compile(phraseNotation);
    TregexMatcher matcher = VBpattern.matcher(sTree);

    while (matcher.findNextMatchingNode()) {
        Tree match = matcher.getMatch();
        Tree[] innerChild = match.children();
        String a = "";
        boolean separatorExist = false;
        if (innerChild.length > 1) {
            int count = 1;
            adjectiveExist = 0;
            adjectiveNoun = 0;
            nnCount = 0;
            String attribute = "";
            adj = "";

            for (Tree inChild : innerChild) {
                //checks whether there are any separators
                if (inChild.value().equals("CC")) {
                    separatorExist = true;
                    attribute = "";
                    adjectiveExist = 0;
                    adjectiveNoun = 0;
                }
                //checks whether there are adjectives
                if ((inChild.value().equals("JJ")) || (inChild.value().equals("VBG"))) {
                    adjectiveExist++;
                    leaves = inChild.getLeaves();
                    adj = leaves.get(0).toString();
                    if (designEleList.contains(adj)) {
                        adj = "";
                    }

                }
                //if the adjective exist store the attributes
                if (adjectiveExist == 1) {
                    adjectiveNoun = storeAdjectiveAttribute(inChild, adjectiveNoun, nnCount, adj);
                }
            }
            if (adjectiveExist == 1 && adjectiveNoun == 0 && !adj.isEmpty()) {
                adjAttributeList.add(stemmingForAWord(adj));

            }
        }
    }

    System.out.println("ADJECTVE ATTRIBUTE :" + adjAttributeList);
    return adjAttributeList;

}

From source file:com.project.NLP.Requirement.PhrasesIdentification.java

/**
 * method to store the adjective attribute
 *
 * @param inChild//  w w  w . j  a  v  a2s.  c  o m
 * @param adjectiveNoun
 * @param nnCount
 * @param adj
 * @return int: number of adjective noun
 */
private int storeAdjectiveAttribute(Tree inChild, int adjectiveNoun, int nnCount, String adj) {
    List<Tree> leaves;
    String attribute = "";
    if ((inChild.value().equals("NN")) || inChild.value().equals("NNS")) {
        leaves = inChild.getLeaves();
        adjectiveNoun++;
        nnCount++;
        if (adjectiveNoun == 1) {
            attribute = stemmingForAWord(leaves.get(0).yieldWords().get(0).word());
            attribute = adj + " " + stemmingForAWord(leaves.get(0).yieldWords().get(0).word());
            adjAttributeList.add(attribute);

        }
        if (adjectiveNoun > 1) {

            adjAttributeList.remove(morphology.stem(attribute));
            adjAttributeList.remove(attribute);
            attribute += " " + leaves.get(0).yieldWords().get(0).word();
            adjAttributeList.add(attribute);
        }
    }
    return adjectiveNoun;
}

From source file:com.search.MySearchHandler.java

License:Apache License

public static List<Tree> getNounPhrases(Tree parse, List<Tree> nounPhrases) {

    if (Pattern.matches("NN(|P|S|PS)", parse.value()) || Pattern.matches("JJ", parse.value())) {
        nounPhrases.addAll(parse.getLeaves());
    }//from ww w  . j ava2  s .  c om
    for (Tree child : parse.children()) {
        getNounPhrases(child, nounPhrases);
    }
    return nounPhrases;
}

From source file:com.search.MySearchHandler.java

License:Apache License

public static List<Tree> getVerbPhrases(Tree parse, List<Tree> verbPhrases) {

    if (Pattern.matches("VB(|D|G|Z|N|P)", parse.value())) {
        verbPhrases.addAll(parse.getLeaves());
    }/*  w  w  w  . ja  v  a  2 s  . c  om*/
    for (Tree child : parse.children()) {
        getVerbPhrases(child, verbPhrases);
    }
    return verbPhrases;
}

From source file:com.search.MySearchHandler.java

License:Apache License

public static List<Tree> getWHPhrases(Tree parse, List<Tree> whPhrases) {
    if (parse.value().equals("WRB") || parse.value().equals("WDT") || parse.value().equals("WP")) {
        whPhrases.addAll(parse.getLeaves());
    }//from  w w  w  . java 2 s  .  c o m
    for (Tree child : parse.children()) {
        getWHPhrases(child, whPhrases);
    }
    return whPhrases;
}

From source file:coreferenceresolver.util.StanfordUtil.java

public void init(boolean simpleInit) throws FileNotFoundException, IOException {
    String outPosFilePath = "./input.txt.pos";
    FileWriter fw = new FileWriter(new File(outPosFilePath));
    BufferedWriter bw = new BufferedWriter(fw);
    props = new Properties();
    if (simpleInit) {
        props.put("annotators", "tokenize, ssplit, pos, parse");
    } else {// ww  w.j  a v a 2 s. c  o m
        props.put("annotators", "tokenize, ssplit, pos, parse, sentiment");
    }
    pipeline = new StanfordCoreNLP(props);

    reviews = new ArrayList<>();

    FileReader fileReader = new FileReader(documentFile);
    BufferedReader bufferedReader = new BufferedReader(fileReader);

    String reviewLine;
    int reviewId = 0;
    int sentenceId;
    //read input file line by line and count the number sentences of each lines
    while ((reviewLine = bufferedReader.readLine()) != null) {
        sentenceId = 0;
        Review newReview = new Review();

        //Add to reviews list
        newReview.setRawContent(reviewLine);

        // create an empty Annotation just with the given text
        document = new Annotation(reviewLine);

        // run all Annotators on this text
        pipeline.annotate(document);
        List<CoreMap> sentences = document.get(SentencesAnnotation.class);

        //Begin extracting from paragraphs
        for (CoreMap sentence : sentences) {
            int sentenceOffsetBegin = sentence.get(CharacterOffsetBeginAnnotation.class);
            int sentenceOffsetEnd = sentence.get(CharacterOffsetEndAnnotation.class);
            Sentence newSentence = new Sentence();
            newSentence.setReviewId(reviewId);
            newSentence.setRawContent(sentence.toString());
            newSentence.setOffsetBegin(sentenceOffsetBegin);
            newSentence.setOffsetEnd(sentenceOffsetEnd);

            if (!simpleInit) {
                int sentimentLevel = RNNCoreAnnotations
                        .getPredictedClass(sentence.get(SentimentCoreAnnotations.SentimentAnnotatedTree.class));
                newSentence.setSentimentLevel(sentimentLevel);

                //Dependency Parsing
                SemanticGraph collCCDeps = sentence.get(CollapsedCCProcessedDependenciesAnnotation.class);
                Collection<TypedDependency> typedDeps = collCCDeps.typedDependencies();
                newSentence.setDependencies(typedDeps);
            }

            List<Tree> sentenceTreeLeaves = sentence.get(TreeCoreAnnotations.TreeAnnotation.class).getLeaves();

            int i = 0;
            for (CoreLabel token : sentence.get(TokensAnnotation.class)) {
                Token newToken = new Token();

                Tree tokenTree = sentenceTreeLeaves.get(i);
                newToken.setTokenTree(tokenTree);

                String word = token.get(TextAnnotation.class);
                newToken.setWord(word);

                String pos = token.get(PartOfSpeechAnnotation.class);
                newToken.setPOS(pos);

                int offsetBegin = token.get(CharacterOffsetBeginAnnotation.class);
                newToken.setOffsetBegin(offsetBegin);

                int offsetEnd = token.get(CharacterOffsetEndAnnotation.class);
                newToken.setOffsetEnd(offsetEnd);

                if (!simpleInit) {
                    //Check NP relative clause
                    Tree twoLevelsAncestor = tokenTree.ancestor(2,
                            sentence.get(TreeCoreAnnotations.TreeAnnotation.class));
                    if (twoLevelsAncestor.value().equals("WHNP") && !word.toLowerCase().equals("who")
                            && !word.toLowerCase().equals("what")) {
                        newToken.setRelativePronoun(true);
                    }

                    //Calculate sentiment for this token
                    int newTokenSentiment = Util.retrieveOriginalSentiment(newToken.getWord());
                    newToken.setSentimentOrientation(newTokenSentiment, newSentence.getDependencies());
                }

                newSentence.addToken(newToken);
                bw.write(token.word() + "/" + token.tag() + " ");
                ++i;
            }
            bw.newLine();

            if (!simpleInit) {

                //Check if this sentence contains a comparative indicator. 
                //If yes, it is a comparative sentence. Identify which NP is superior or inferior in this sentence
                List<Token> comparativeTokens = FeatureExtractor.findComparativeIndicator(newSentence, null,
                        null);
                //TODO
                //Check special comparative samples
                if (!comparativeTokens.isEmpty()) {
                    newSentence.initComparatives(comparativeTokens);
                }
            }

            newReview.addSentence(newSentence);

            ++sentenceId;
        }

        bw.write("./.");
        bw.newLine();

        reviews.add(newReview);
        ++reviewId;
    }
    bw.close();
}