Example usage for edu.stanford.nlp.trees TypedDependency dep

List of usage examples for edu.stanford.nlp.trees TypedDependency dep

Introduction

In this page you can find the example usage for edu.stanford.nlp.trees TypedDependency dep.

Prototype

IndexedWord dep

To view the source code for edu.stanford.nlp.trees TypedDependency dep.

Click Source Link

Usage

From source file:DependencyParse.java

License:Apache License

public static void main(String[] args) throws Exception {
    Properties props = StringUtils.argsToProperties(args);
    if (!props.containsKey("tokpath") || !props.containsKey("parentpath") || !props.containsKey("relpath")) {
        System.err.println(//  w ww .  jav a2s  .com
                "usage: java DependencyParse -tokenize - -tokpath <tokpath> -parentpath <parentpath> -relpath <relpath>");
        System.exit(1);
    }

    boolean tokenize = false;
    if (props.containsKey("tokenize")) {
        tokenize = true;
    }

    String tokPath = props.getProperty("tokpath");
    String parentPath = props.getProperty("parentpath");
    String relPath = props.getProperty("relpath");

    BufferedWriter tokWriter = new BufferedWriter(new FileWriter(tokPath));
    BufferedWriter parentWriter = new BufferedWriter(new FileWriter(parentPath));
    BufferedWriter relWriter = new BufferedWriter(new FileWriter(relPath));

    MaxentTagger tagger = new MaxentTagger(TAGGER_MODEL);
    DependencyParser parser = DependencyParser.loadFromModelFile(PARSER_MODEL);
    Scanner stdin = new Scanner(System.in);
    int count = 0;
    long start = System.currentTimeMillis();
    while (stdin.hasNextLine()) {
        String line = stdin.nextLine();
        List<HasWord> tokens = new ArrayList<>();
        if (tokenize) {
            PTBTokenizer<Word> tokenizer = new PTBTokenizer(new StringReader(line), new WordTokenFactory(), "");
            for (Word label; tokenizer.hasNext();) {
                tokens.add(tokenizer.next());
            }
        } else {
            for (String word : line.split(" ")) {
                tokens.add(new Word(word));
            }
        }

        List<TaggedWord> tagged = tagger.tagSentence(tokens);

        int len = tagged.size();
        Collection<TypedDependency> tdl = parser.predict(tagged).typedDependencies();
        int[] parents = new int[len];
        for (int i = 0; i < len; i++) {
            // if a node has a parent of -1 at the end of parsing, then the node
            // has no parent.
            parents[i] = -1;
        }

        String[] relns = new String[len];
        for (TypedDependency td : tdl) {
            // let root have index 0
            int child = td.dep().index();
            int parent = td.gov().index();
            relns[child - 1] = td.reln().toString();
            parents[child - 1] = parent;
        }

        // print tokens
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < len - 1; i++) {
            if (tokenize) {
                sb.append(PTBTokenizer.ptbToken2Text(tokens.get(i).word()));
            } else {
                sb.append(tokens.get(i).word());
            }
            sb.append(' ');
        }
        if (tokenize) {
            sb.append(PTBTokenizer.ptbToken2Text(tokens.get(len - 1).word()));
        } else {
            sb.append(tokens.get(len - 1).word());
        }
        sb.append('\n');
        tokWriter.write(sb.toString());

        // print parent pointers
        sb = new StringBuilder();
        for (int i = 0; i < len - 1; i++) {
            sb.append(parents[i]);
            sb.append(' ');
        }
        sb.append(parents[len - 1]);
        sb.append('\n');
        parentWriter.write(sb.toString());

        // print relations
        sb = new StringBuilder();
        for (int i = 0; i < len - 1; i++) {
            sb.append(relns[i]);
            sb.append(' ');
        }
        sb.append(relns[len - 1]);
        sb.append('\n');
        relWriter.write(sb.toString());

        count++;
        if (count % 1000 == 0) {
            double elapsed = (System.currentTimeMillis() - start) / 1000.0;
            System.err.printf("Parsed %d lines (%.2fs)\n", count, elapsed);
        }
    }

    long totalTimeMillis = System.currentTimeMillis() - start;
    System.err.printf("Done: %d lines in %.2fs (%.1fms per line)\n", count, totalTimeMillis / 1000.0,
            totalTimeMillis / (double) count);
    tokWriter.close();
    parentWriter.close();
    relWriter.close();
}

From source file:ConstituencyParse.java

License:Apache License

public int[] depTreeParents(Tree tree, List<HasWord> tokens) {
    GrammaticalStructure gs = gsf.newGrammaticalStructure(tree);
    Collection<TypedDependency> tdl = gs.typedDependencies();
    int len = tokens.size();
    int[] parents = new int[len];
    for (int i = 0; i < len; i++) {
        // if a node has a parent of -1 at the end of parsing, then the node
        // has no parent.
        parents[i] = -1;// w w  w  . ja  va2 s .co  m
    }

    for (TypedDependency td : tdl) {
        // let root have index 0
        int child = td.dep().index();
        int parent = td.gov().index();
        parents[child - 1] = parent;
    }

    return parents;
}

From source file:ca.ualberta.exemplar.core.ParserMalt.java

License:Open Source License

@Override
public List<CoreMap> parseText(String text) {

    List<CoreMap> sentences = null;

    try {//w w w. java 2s .  com
        Annotation document = new Annotation(text);
        pipeline.annotate(document);

        sentences = document.get(SentencesAnnotation.class);
        for (CoreMap sentence : sentences) {

            List<CoreLabel> tokens = sentence.get(TokensAnnotation.class);
            String[] conllInput = sentenceToCoNLLInput(tokens);

            DependencyGraph graph = (DependencyGraph) maltParser.parse(conllInput);

            Result result = graphToCoNLL(graph);
            List<List<String>> conll = result.conll;
            int rootIndex = result.rootIndex;

            EnglishGrammaticalStructure gs = EnglishGrammaticalStructure.buildCoNNLXGrammaticStructure(conll);

            TreeGraphNode root = null;

            List<TypedDependency> deps = gs.typedDependenciesCCprocessed();

            // Add root dependency and ner annotations
            int size = deps.size();
            for (int i = 0; i < size; i++) {
                TypedDependency td = deps.get(i);
                if (td.gov().index() == rootIndex) {
                    root = td.gov();
                    deps.add(new TypedDependency(GrammaticalRelation.ROOT, td.gov(), td.gov()));
                }
                {
                    TreeGraphNode n = td.dep();
                    if (n.label().ner() == null) {
                        n.label().setNER(tokens.get(n.index() - 1).ner());
                        n.label().setBeginPosition(tokens.get(n.index() - 1).beginPosition());
                        n.label().setEndPosition(tokens.get(n.index() - 1).endPosition());
                        n.label().setLemma(tokens.get(n.index() - 1).lemma());
                    }
                }
                {
                    TreeGraphNode n = td.gov();
                    if (n.label().ner() == null) {
                        n.label().setNER(tokens.get(n.index() - 1).ner());
                        n.label().setBeginPosition(tokens.get(n.index() - 1).beginPosition());
                        n.label().setEndPosition(tokens.get(n.index() - 1).endPosition());
                        n.label().setLemma(tokens.get(n.index() - 1).lemma());
                    }
                }
            }
            if (root == null)
                continue;

            List<TreeGraphNode> roots = new ArrayList<TreeGraphNode>();
            roots.add(gs.root());

            SemanticGraph sg = new SemanticGraph(deps, roots);
            sentence.set(CollapsedCCProcessedDependenciesAnnotation.class, sg);

        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return sentences;
}

From source file:cc.vidr.parseviz.ParseViz.java

License:Open Source License

public static void printDependenciesDot(Tree tree, StringBuilder sb) {
    sb.append("digraph{\n");
    for (TypedDependency td : typedDependencies(tree)) {
        GrammaticalRelation reln = td.reln();
        TreeGraphNode gov = td.gov();/*from w  w  w .ja  va2 s .  c o m*/
        TreeGraphNode dep = td.dep();
        sb.append("n").append(gov.index()).append("[label=\"").append(gov).append("\"];\n").append("n")
                .append(dep.index()).append("[label=\"").append(dep).append("\"];\n").append("n")
                .append(gov.index()).append("->n").append(dep.index()).append("[label=\"").append(reln)
                .append("\"];\n");
    }
    sb.append("}\n");
}

From source file:com.epictodo.controller.nlp.NLPEngine.java

License:Open Source License

/**
 * This method understands the natural input from an input sentence
 * The sentence is analyzed and important information will be extracted and returned
 * <p/>//w w w  . j a va  2 s. c o m
 * Assumptions:
 * 1. PERSON name has to start with a capital letter. For example, "Kenneth"
 * 2. Priority is determined by weekly basis from today's date. Priority increases every week.
 * <p/>
 * 3. Time has to strictly follow the format (hh:mm)
 * 3.1 10:00 is accepted
 * 3.2 1000 is not accepted
 * <p/>
 * 4. PERSON name cannot be named after a month (for example, April, May, June) -> Registers as a DATE
 * 5. PERSON name cannot be named after a day (for example, Sunday) -> Registers as a DATE
 * <p/>
 * 6. Time period: Day cannot be placed after the time period
 * 6.1. Tuesday 2 weeks later (PASSED) - gets the Tuesday 2 weeks later
 * 6.2. 2 weeks later Tuesday (FAILED) - gets 2 weeks later from today's date
 * <p/>
 * 7. There can be only 2 time input which will be allowed to be parsed in. Additional time will be rejected.
 * 7.1. from 10:00 to 14:00 then 18:00 > reflects only [10:00, 14:00] as start_time and end_time
 * <p/>
 * 8. If users input date in quick date for example, (01/12/2014 -> dd/MM/yyyy)
 * 8.1. [Issue] Manual correction of storing through validation
 * <p/>
 * 9. Chinese names will be read as two different PERSON. For example, Jie Ning will be read as 'Jie', 'Ning'
 * 10. Supports one short date, for example '14/11/2014'
 * <p/>
 * Usage:
 * <p/>
 * 1. flexiAdd("meeting with Damith on project submission Tuesday 2 weeks later at 10:34");
 * 2. flexiAdd("project submission for cs2103 next Tuesday from 10:00 to 14:00 to Damith");
 * <p/>
 * Output:
 * <p/>
 * 1.
 * Task Name: meeting submission
 * Task Desc: Damith Tuesday 2 weeks later at 10:34
 * Task Date: 181114
 * Task Time: 10:34
 * Task Priority: 8
 * Task Start Time: null
 * Task End Time: null
 * Task Duration: 0.0
 * <p/>
 * 2.
 * Task Name: submission cs2103
 * Task Desc: Damith 10:00 14:00 next Tuesday
 * Task Date: 111114
 * Task Time: null
 * Task Priority: 9
 * Task Start Time: 10:00
 * Task End Time: 14:00
 * Task Duration: 4.0
 *
 * @param _sentence
 * @throws ParseException
 */
public Response flexiAdd(String _sentence) throws ParseException {
    _response = new Response();
    boolean is_date_set = false;
    boolean is_time_set = false;
    boolean is_priority_set = false;
    String tomorrow_date;
    String date_value;
    String time_value;
    String _priority;
    String start_time;
    String end_time;
    double _duration;
    int num_days;

    List<String> analyzed_results = new ArrayList<>();
    List<String> task_name = new ArrayList<>();
    List<String> task_desc = new ArrayList<>();
    List<String> task_date = new ArrayList<>();
    List<String> task_time = new ArrayList<>();
    List<String> date_list = new ArrayList<>();

    StringBuilder string_builder = new StringBuilder(CAPACITY);

    // Initialize Sentence Structure & Sentence Analysis to Map
    Map<String, String> sentence_struct_map = sentence_struct.sentenceDependencies(_sentence);
    Map<String, String> date_time_map = sentence_analysis.dateTimeAnalyzer(_sentence);
    Map<String, String> sentence_token_map = sentence_analysis.sentenceAnalyzer(_sentence);
    LinkedHashMap<String, LinkedHashSet<String>> entities_map = sentence_analysis
            .nerEntitiesExtractor(_sentence);
    List<TypedDependency> grammar_struct_map = grammartical_parser.grammarAnalyzer(_sentence);

    /**
     * Name Entity Recognition (NER) map
     * For loop to traverse key:value of NER map to retrieve keywords to be processed
     *
     * Example: root, nn, dobj, nsubj, aux, xcomp, prep, num, etc.
     *
     */
    for (Map.Entry<String, LinkedHashSet<String>> map_result : entities_map.entrySet()) {
        String _key = map_result.getKey();
        LinkedHashSet<String> _value = map_result.getValue();
    }

    /**
     * Sentence analyzer to analyze the text for keywords
     * For loop to traverse the following:
     *
     * 1. (TIME) - stores TIME's (value) key to check if it's a single or multiple TIME value
     * 1.1. Checks if key is valid time before storing.
     * 1.2. "at" maybe classified as a valid TIME but we do not want to register this
     *
     * 2. (PERSON) - stores PERSON name to be processed in Task Description
     * 2.1. PERSON does not accept 'Prof' as an actual person, rather an entity
     *
     * 3. (LOCATION) - stores LOCATION name if it exists.
     * 3.1. TOKYO, SINGAPORE are actual LOCATION
     * 3.2. COM1, LT15, SR1 are not an actual LOCATION stored in our model (can be implemented using Classifier in future)
     *
     */
    for (Map.Entry<String, String> map_result : sentence_token_map.entrySet()) {
        String _key = map_result.getKey();
        String _value = map_result.getValue();

        if (_value.equalsIgnoreCase("TIME")) {
            if (time_validator.validate(_key)) {
                task_time.add(_key);
            }
        }

        if (_value.equalsIgnoreCase("NUMBER")) {
            if (date_validator.validateDateExpression(_key)) {
                date_value = date_validator.fixShortDate(_key);
                task_date.add(date_value);
                num_days = date_validator.compareDate(date_value);

                // Checks if date distance is >= 0 or <= 1 of 1 day
                if (num_days >= 0 && num_days <= 1) {
                    _priority = date_validator.determinePriority(date_value);
                    date_value = date_validator.genericDateFormat(date_value);

                    if (_response.getTaskDate() == null) {
                        _response.setTaskDate(date_value);
                    }

                    _response.setPriority(Integer.parseInt(_priority));
                    is_priority_set = true;
                } else { // Check if TaskDate has been set previously, prevent override
                    _priority = date_validator.determinePriority(date_value);
                    date_value = date_validator.genericDateFormat(date_value);

                    if (_response.getTaskDate() == null) {
                        _response.setTaskDate(date_value);
                    }

                    _response.setPriority(Integer.parseInt(_priority));
                    is_priority_set = true;
                }

                is_date_set = true;
            } else {
                date_value = date_validator.nlpShortDate(_key);
                task_date.add(date_value);
                num_days = date_validator.compareDate(date_value);

                // Checks if date distance is >= 0 or <= 1 of 1 day
                if (num_days >= 0 && num_days <= 1) {
                    _priority = date_validator.determinePriority(date_value);
                    date_value = date_validator.genericDateFormat(date_value);

                    if (_response.getTaskDate() == null) {
                        _response.setTaskDate(date_value);
                    }

                    _response.setPriority(Integer.parseInt(_priority));
                    is_priority_set = true;
                } else { // Check if TaskDate has been set previously, prevent override
                    _priority = date_validator.determinePriority(date_value);
                    date_value = date_validator.genericDateFormat(date_value);

                    if (_response.getTaskDate() == null) {
                        _response.setTaskDate(date_value);
                    }

                    _response.setPriority(Integer.parseInt(_priority));
                    is_priority_set = true;
                }

                is_date_set = true;
            }
        }

        if (_value.equalsIgnoreCase("PERSON")) {
            if (!_key.equalsIgnoreCase("Prof")) {
                task_desc.add(_key);
                analyzed_results.add(_key);
            } else {
                task_desc.add(_key);
                analyzed_results.add(_key);
            }
        } else if (_value.equalsIgnoreCase("LOCATION")) {
            task_desc.add(_key);
            analyzed_results.add(_key);
        } else if (_value.equalsIgnoreCase("ORGANIZATION")) {
            task_desc.add(_key);
            analyzed_results.add(_key);
        }
    }

    /**
     * This algorithm checks if the time values stored are more than or equals to 2
     * There can be instances where users input 3 or more time variables, but the first 2 will be registered
     *
     * This algorithm will getTimeDuration before storing start_time, end_time & _duration to _response
     */
    if (task_time.size() != 0 && task_time.size() >= 2) {
        start_time = task_time.get(0);
        end_time = task_time.get(1);

        _duration = time_validator.getTimeDuration(start_time, end_time);
        _response.setStartTime(start_time);
        _response.setEndTime(end_time);
        _response.setTaskDuration(_duration);

        is_time_set = true;
    } else if (task_time.size() != 0 && task_time.size() < 2) {
        _response.setTaskTime(task_time.get(0));

        is_time_set = true;
    }

    /**
     * Date time analyzer map to analyze date time values to be stored
     * This algorithm will check for TOMORROW date & time distance.
     *
     * If that case happens, the result will be parsed to getDateInFormat & getTimeInFormat to handle such cases
     * Otherwise, the result will be parsed to validateDate & validateTime to handle the more generic cases
     */
    for (Map.Entry<String, String> map_result : date_time_map.entrySet()) {
        String _key = map_result.getKey();
        String _value = map_result.getValue();

        if (!is_date_set) {
            tomorrow_date = date_validator.convertDateFormat(_value);
            num_days = date_validator.compareDate(tomorrow_date);

            // Checks if date distance is >= 0 or <= 1 of 1 day
            if (num_days >= 0 && num_days <= 1) {
                _priority = date_validator.determinePriority(tomorrow_date);
                date_value = date_validator.genericDateFormat(tomorrow_date);
                time_value = date_validator.getTimeInFormat(_value);

                if (!is_date_set) { // Check if TaskDate has been set previously, prevent override
                    if (date_value.length() == 5) {
                        date_list.add(0, "0");

                        Scanner _scanner = new Scanner(date_value);
                        while (_scanner.hasNext()) {
                            date_list.add(_scanner.next());
                        }
                        _scanner.close();

                        for (String date : date_list) {
                            string_builder.append(date);
                        }

                        _response.setTaskDate(string_builder.toString());
                        is_date_set = true;
                    } else {
                        _response.setTaskDate(date_value);
                        is_date_set = true;
                    }
                }

                if (!is_time_set) {
                    _response.setTaskTime(time_value);
                    is_time_set = true;
                }

                if (!is_priority_set) {
                    _response.setPriority(Integer.parseInt(_priority));
                    is_priority_set = true;
                }

                //                _response.setTaskDate(date_value);
                //                _response.setTaskTime(time_value);
                //                _response.setPriority(Integer.parseInt(_priority));
                task_desc.add(_key);
                analyzed_results.add(_key);
                analyzed_results.add(date_value);
                analyzed_results.add(time_value);
                analyzed_results.add(_priority);
            } else { // Check if TaskDate has been set previously, prevent override
                _priority = date_validator.determinePriority(_value);
                date_value = date_validator.genericDateFormat(tomorrow_date);
                time_value = date_validator.validateTime(_value);

                if (!is_date_set) {
                    if (date_value.length() == 5) {
                        date_list.add(0, "0");

                        Scanner _scanner = new Scanner(date_value);
                        while (_scanner.hasNext()) {
                            date_list.add(_scanner.next());
                        }
                        _scanner.close();

                        for (String date : date_list) {
                            string_builder.append(date);
                        }

                        _response.setTaskDate(string_builder.toString());
                        is_date_set = true;
                    } else {
                        _response.setTaskDate(date_value);
                        is_date_set = true;
                    }
                }

                if (!is_time_set) {
                    _response.setTaskTime(time_value);
                    is_time_set = true;
                }

                if (!is_priority_set) {
                    _response.setPriority(Integer.parseInt(_priority));
                    is_priority_set = true;
                }

                //                _response.setTaskDate(date_value);
                //                _response.setTaskTime(time_value);
                //                _response.setPriority(Integer.parseInt(_priority));
                task_desc.add(_key);
                analyzed_results.add(_key);
                analyzed_results.add(date_value);
                analyzed_results.add(time_value);
                analyzed_results.add(_priority);
            }
        }
    }

    /**
     * Sentence Dependencies map to analyze and return the dependencies of the sentence structure
     * This algorithm checks the dependencies relationship in the tree structure, and returns the results
     */
    for (Map.Entry<String, String> map_result : sentence_struct_map.entrySet()) {
        String _key = map_result.getKey();
        String _value = map_result.getValue();

        if ((_key.equalsIgnoreCase("root") || _key.equalsIgnoreCase("dep") || _key.equalsIgnoreCase("dobj")
                || _key.equalsIgnoreCase("prep_on") || _key.equalsIgnoreCase("prep_for")
                || _key.equalsIgnoreCase("nn") || _key.equalsIgnoreCase("xcomp"))
                && (_value.equalsIgnoreCase("aux"))) {
            task_name.add(_value);
            analyzed_results.add(_value);
        }
    }

    /**
     * Grammartical Struct map analyzes and return the relationship and dependencies of the grammartical structure
     * This algorithm checks for
     *
     * 1. root: the grammatical relation that points to the root of the sentence
     * 1.1. Remove words like 'be'
     * 1.2. Remove words that are already added into the list
     * 2. nn: noun compound modifier is any noun that serves to modify the head noun
     * 2.1. Remove words that is stored already in Task Description
     * 3. nsubj: nominal subject is a noun phrase which is the syntactic subject of a clause
     *    aux: auxiliary is a non-main verb of the clause
     *    xcomp: open clausal complement  is a clausal complement without
     *           its own subject, whose reference is determined by an external subject
     *    dobj: direct object is the noun phrase which is the (accusative) object of the verb
     * 3.1. Add words that does not already exist in Task Name
     * 4. amod: adjectival modifier is any adjectival phrase that serves to modify the meaning of the NP
     * 4.1. Remove words like 'next'
     * 5. prep:  prepositional modifier of a verb, adjective, or noun is any prepositional phrase that serves to
     *           modify the meaning of the verb, adjective, noun, or even another prepositon
     * 5.1. Check Task Time size is more than 1 or less than 2
     * 5.2. Remove Task Time accordingly
     * 6. dep: dependent is when the system is unable to determine a more precise
     *         dependency relation between two words
     * 6.1. Remove words like 'regarding'
     * 7. aux: is a non-main verb of the clause, e.g., a modal auxiliary, or a form of
     *         "be", "do" or "have" in a periphrastic tense
     * 7.1. Remove words like 'to'
     *
     * This algorithm ensures that NLP maintains a certain integrity to making sense while parsing the sentence
     *
     */
    for (TypedDependency type_dependency : grammar_struct_map) {
        String reln_key = type_dependency.reln().getShortName();
        String dep_value = type_dependency.dep().nodeString();

        if (reln_key.equalsIgnoreCase("root")) {
            if (task_name.contains("Be") || task_name.contains("be")) {
                task_name.remove(dep_value);
            } else if (task_name.contains(dep_value)) {
                task_name.remove(dep_value);
            } else {
                task_name.add(dep_value);
            }
        }

        if (reln_key.equalsIgnoreCase("nn")) {
            if (!task_name.contains(dep_value)) {
                task_name.add(dep_value);
            }

            if (task_desc.contains(dep_value)) {
                task_name.remove(dep_value);
            }
        }

        if (reln_key.equalsIgnoreCase("nsubj") || reln_key.equalsIgnoreCase("aux")
                || reln_key.equalsIgnoreCase("xcomp") || reln_key.equalsIgnoreCase("dobj")) {
            if (!task_name.contains(dep_value)) {
                task_name.add(dep_value);
            }
        }

        if (reln_key.equalsIgnoreCase("amod") && !dep_value.equalsIgnoreCase("next")) {
            if (!task_name.contains(dep_value)) {
                task_name.add(dep_value);
            }
        }

        if (reln_key.equalsIgnoreCase("prep")) {
            if (!task_name.contains(dep_value) && (!date_validator.checkDateFormat(dep_value)
                    && !date_validator.checkDateFormat2(dep_value))) {
                task_name.add(dep_value);
            }

            if (task_time.size() > 0 && task_time.size() < 2) {
                if (dep_value.contains(task_time.get(0))) {
                    task_name.remove(dep_value);
                }
            } else if (task_time.size() >= 2 && task_time.size() < 3) {
                if (dep_value.contains(task_time.get(0)) || dep_value.contains(task_time.get(1))) {
                    task_name.remove(dep_value);
                }
            }
        }

        if (reln_key.equalsIgnoreCase("dep") && dep_value.equalsIgnoreCase("regarding")) {
            if (task_name.contains(dep_value)) {
                task_name.remove(dep_value);
            }
        }

        if (reln_key.equalsIgnoreCase("aux") && dep_value.equalsIgnoreCase("to")) {
            if (task_name.contains(dep_value)) {
                task_name.remove(dep_value);
            }
        }
    }

    _response.setTaskName(task_name);
    _response.setTaskDesc(task_desc);

    return _response;
}

From source file:com.search.MySearchHandler.java

License:Apache License

private static String compress(TermTokenStream queryStream) {

    List<TypedDependency> tdl = getTyped(queryStream.toNLPString());

    ListIterator<TypedDependency> tdllist = tdl.listIterator();
    int count = 1;
    while (tdllist.hasNext()) {

        TypedDependency typd = tdllist.next();
        // Have to fix the multiple words in the sequence issue
        if ((typd.reln().toString().equals("nn") | typd.reln().toString().equals("amod")
                | (typd.reln().toString().equals("det") && typd.dep().index() != 1)
                | typd.reln().toString().equals("num") | typd.reln().toString().equals("number"))
                && ((typd.gov().index() - typd.dep().index()) == 1
                        || (typd.gov().index() - typd.dep().index()) == -1)) {
            if ((typd.gov().index() - typd.dep().index()) == -1) {
                queryStream.mergeWithNext(typd.gov().index() - count);
                count++;/*  w  ww . j  ava 2 s. co  m*/
            } else {
                queryStream.mergeWithNext(typd.dep().index() - count);
                count++;
            }
        }
    }

    if (found(getTyped(queryStream.toNLPString()))) {
        return compress(queryStream);
    }

    return queryStream.toNLPString();
}

From source file:com.search.MySearchHandler.java

License:Apache License

private static boolean found(List<TypedDependency> typed) {
    ListIterator<TypedDependency> tdllist = typed.listIterator();
    boolean iftrue = false;
    while (tdllist.hasNext()) {

        TypedDependency typd = tdllist.next();
        // Have to fix the multiple words in the sequence issue
        if ((typd.reln().toString().equals("nn") | typd.reln().toString().equals("amod")
                | (typd.reln().toString().equals("det") && typd.dep().index() != 1)
                | typd.reln().toString().equals("num") | typd.reln().toString().equals("number"))
                && ((typd.gov().index() - typd.dep().index()) == 1
                        || (typd.gov().index() - typd.dep().index()) == -1)) {
            iftrue = true;//  w w  w .  ja v  a  2 s  .  c  o  m
        }

    }
    return iftrue;
}

From source file:coreferenceresolver.element.Token.java

/**
 * @param newTokenSentiment the sentimentOrientation to set
 *//*from  w  w  w. j a va  2  s . co m*/
public void setSentimentOrientation(int newTokenSentiment, Collection<TypedDependency> typedDeps) {
    if ((newTokenSentiment == Util.POSITIVE || newTokenSentiment == Util.NEGATIVE)
            && (!this.POS.equals("IN"))) {
        for (TypedDependency typedDependency : typedDeps) {
            if (typedDependency.reln().toString().equals("neg") && typedDependency.gov().value().equals(word)) {
                newTokenSentiment = Util.reverseSentiment(newTokenSentiment);
            }
            if (typedDependency.reln().toString().equals("mark")
                    && (typedDependency.dep().value().toLowerCase().equals("although")
                            || typedDependency.dep().value().toLowerCase().equals("though")
                            || typedDependency.dep().value().toLowerCase().equals("while"))
                    && typedDependency.gov().value().equals(word)) {
                newTokenSentiment = Util.NEUTRAL;
                break;
            }
        }
    }
    this.sentimentOrientation = newTokenSentiment;
}

From source file:de.tudarmstadt.ukp.dkpro.core.stanfordnlp.StanfordDependencyConverter.java

License:Open Source License

protected void doCreateDependencyTags(JCas aJCas, TreebankLanguagePack aLP, Tree parseTree,
        List<Token> tokens) {
    GrammaticalStructure gs;/*from w w  w  . j  a v  a2  s.  c  o  m*/
    try {
        gs = aLP.grammaticalStructureFactory(aLP.punctuationWordRejectFilter(), aLP.typedDependencyHeadFinder())
                .newGrammaticalStructure(parseTree);
    } catch (UnsupportedOperationException e) {
        // We already warned in the model provider if dependencies are not supported, so here
        // we just do nothing and skip the dependencies.
        return;
    }

    Collection<TypedDependency> dependencies = null;
    switch (mode) {
    case BASIC:
        dependencies = gs.typedDependencies(); // gs.typedDependencies(false);
        break;
    case NON_COLLAPSED:
        dependencies = gs.allTypedDependencies(); // gs.typedDependencies(true);
        break;
    case COLLAPSED_WITH_EXTRA:
        dependencies = gs.typedDependenciesCollapsed(true);
        break;
    case COLLAPSED:
        dependencies = gs.typedDependenciesCollapsed(false);
        break;
    case CC_PROPAGATED:
        dependencies = gs.typedDependenciesCCprocessed(true);
        break;
    case CC_PROPAGATED_NO_EXTRA:
        dependencies = gs.typedDependenciesCCprocessed(false);
        break;
    case TREE:
        dependencies = gs.typedDependenciesCollapsedTree();
        break;
    }

    for (TypedDependency currTypedDep : dependencies) {
        int govIndex = currTypedDep.gov().index();
        int depIndex = currTypedDep.dep().index();
        if (govIndex != 0) {
            // Stanford CoreNLP produces a dependency relation between a verb and ROOT-0 which
            // is not token at all!
            Token govToken = tokens.get(govIndex - 1);
            Token depToken = tokens.get(depIndex - 1);

            StanfordAnnotator.createDependencyAnnotation(aJCas, currTypedDep.reln(), govToken, depToken);
        }
    }
}

From source file:de.tudarmstadt.ukp.dkpro.core.stanfordnlp.StanfordParser.java

License:Open Source License

protected void doCreateDependencyTags(ParserGrammar aParser, StanfordAnnotator sfAnnotator, Tree parseTree,
        List<Token> tokens) {
    GrammaticalStructure gs;//from   w w w.j a v  a 2s. c o m
    try {
        TreebankLanguagePack tlp = aParser.getTLPParams().treebankLanguagePack();
        gs = tlp.grammaticalStructureFactory(tlp.punctuationWordRejectFilter(), tlp.typedDependencyHeadFinder())
                .newGrammaticalStructure(parseTree);
    } catch (UnsupportedOperationException e) {
        // We already warned in the model provider if dependencies are not supported, so here
        // we just do nothing and skip the dependencies.
        return;
    }

    Collection<TypedDependency> dependencies = null;
    switch (mode) {
    case BASIC:
        dependencies = gs.typedDependencies(); // gs.typedDependencies(false);
        break;
    case NON_COLLAPSED:
        dependencies = gs.allTypedDependencies(); // gs.typedDependencies(true);
        break;
    case COLLAPSED_WITH_EXTRA:
        dependencies = gs.typedDependenciesCollapsed(true);
        break;
    case COLLAPSED:
        dependencies = gs.typedDependenciesCollapsed(false);
        break;
    case CC_PROPAGATED:
        dependencies = gs.typedDependenciesCCprocessed(true);
        break;
    case CC_PROPAGATED_NO_EXTRA:
        dependencies = gs.typedDependenciesCCprocessed(false);
        break;
    case TREE:
        dependencies = gs.typedDependenciesCollapsedTree();
        break;
    }

    for (TypedDependency currTypedDep : dependencies) {
        int govIndex = currTypedDep.gov().index();
        int depIndex = currTypedDep.dep().index();
        if (govIndex != 0) {
            // Stanford CoreNLP produces a dependency relation between a verb and ROOT-0 which
            // is not token at all!
            Token govToken = tokens.get(govIndex - 1);
            Token depToken = tokens.get(depIndex - 1);

            sfAnnotator.createDependencyAnnotation(currTypedDep.reln(), govToken, depToken);
        }
    }
}