Package opennlp.tools.parser

Examples of opennlp.tools.parser.Parse


    POSTaggerME tagger =  new POSTaggerME(posModel);
    Parser parser = new ChunkParser(chunker, tagger);
    Parse[] results = ParserTool.parseLine("The Minnesota Twins , " +
            "the 1991 World Series Champions , are currently in third place .",
            parser, 1);
    Parse p = results[0];
    Parse[] chunks = p.getChildren();
    assertTrue(chunks.length == 9);
    assertTrue(chunks[0].getType().equals("NP"));
    assertTrue(chunks[0].getHead().toString().equals("Twins"));
    //<end id="openChunkParse"/>
  }
View Full Code Here


  @Override
  public Query parse() throws ParseException {

    //<start id="qqp.parse"/>
    Parse parse = ParserTool.parseLine(qstr, parser, 1)[0];//<co id="qqp.parseLine"/>
    /*
    <calloutlist>
        <callout arearefs="qqp.parseLine"><para>Parse the question using the <classname>TreebankParser</classname>.  The resulting <classname>Parse</classname> object can then be utilized by the classifier to determine the Answer Type.</para></callout>
    </calloutlist>
    */
 
View Full Code Here

    List<Parse> parts = new ArrayList<Parse>();
    parts.add(parse);
    while (parts.size() > 0) {
      List<Parse> newParts = new ArrayList<Parse>();
      for (int pi=0,pn=parts.size();pi<pn;pi++) {
        Parse cp = parts.get(pi);
        if (cp.getType().equals("NP") && cp.isFlat()) {
          nps.add(cp);
        }
        else if (!cp.isPosTag()) {
          newParts.addAll(Arrays.asList(cp.getChildren()));
        }
      }
      parts = newParts;
    }
    return nps.toArray(new Parse[nps.size()]);
View Full Code Here

    }
    return nps.toArray(new Parse[nps.size()]);
  }
 
  private Parse getContainingNounPhrase(Parse token) {
    Parse parent = token.getParent();
    if (parent.getType().equals("NP")) {
      return parent;
    }
    return null;
  }
View Full Code Here

    return null;
  }
 
  private int getTokenIndexFollowingPhrase(Parse p,Parse[] toks) {
    Parse[] ptok = p.getTagNodes();
    Parse lastToken = ptok[ptok.length-1];
    for (int ti=0,tl=toks.length;ti<tl;ti++) {
      if (toks[ti] == lastToken) {
        return(ti+1);
      }
    }
View Full Code Here

 
  private Parse findFocusNounPhrase(String queryWord, int qwi, Parse[] toks) {
    if (queryWord.equals("who")) {
      int npStart = movePastCopula(qwi + 1, toks);
      if (npStart > qwi + 1) { // check to ensure there is a copula
        Parse np = getContainingNounPhrase(toks[npStart]);
        if (np != null) {
          return (np);
        }
      }
    }
    else if (queryWord.equals("what")) {
      int npStart = movePastCopula(qwi + 1, toks);
      Parse np = getContainingNounPhrase(toks[npStart]);
      //removed copula case
      if (np != null) {
        Parse head = np.getHead();
        if (falseHeadsPattern.matcher(head.toString()).matches()) {
          npStart += np.getChildren().length;
          int np2Start = movePastPrep(npStart, toks);
          if (np2Start > npStart) {
            Parse snp = getContainingNounPhrase(toks[np2Start]);
            if (snp != null) {
              return (snp);
            }
          }
        }
        return (np);
      }
    }
    else if (queryWord.equals("which")) {
      //check for false query words like which VBD
      int npStart = movePastCopula(qwi + 1, toks);
      if (npStart > qwi + 1) {
        return (getContainingNounPhrase(toks[npStart]));
      }
      else {
        npStart = movePastOf(qwi + 1, toks);
        return (getContainingNounPhrase(toks[npStart]));
      }
    }
    else if (queryWord.equals("how")) {
      if (qwi + 1 < toks.length) {
        return (getContainingNounPhrase(toks[qwi + 1]));
      }
    }
    else if (qwi == 0 && queryWord.equals("name")) {
      int npStart = qwi + 1;
      Parse np = getContainingNounPhrase(toks[npStart]);
      if (np != null) {
        Parse head = np.getHead();
        if (falseHeadsPattern.matcher(head.toString()).matches()) {
          npStart += np.getChildren().length;
          int np2Start = movePastPrep(npStart, toks);
          if (np2Start > npStart) {
            Parse snp = getContainingNounPhrase(toks[np2Start]);
            if (snp != null) {
              return (snp);
            }
          }
        }
View Full Code Here

    features.add("hw=" + toks[nsi]);
    features.add("ht=" + toks[nsi].getType());
  }
 
  public String[] getContext(Parse query) {
    Parse focalNoun = null;
    String queryWord = null;
    List<String> features = new ArrayList<String>();
    features.add("def");
    Parse[] nps = getNounPhrases(query);
    Parse[] toks = query.getTagNodes();
    int fnEnd = 0;
    int i = 0;
    boolean fnIsLast = false;
    for (; i < toks.length; i++) {
      String tok = toks[i].toString().toLowerCase();
      if (isQueryWord(tok)) {
        queryWord = tok;
        focalNoun = findFocusNounPhrase(queryWord, i, toks);
        if (tok.equals("how") && i+1 < toks.length) {
          if (howModifierTagPattern.matcher(toks[i+1].getType()).find()) {
            queryWord=tok+"_"+toks[i+1].toString();
          }
        }
        if (focalNoun != null) {
          fnEnd = getTokenIndexFollowingPhrase(focalNoun,toks);
        }
        if (focalNoun != null && focalNoun.equals(nps[nps.length - 1])) {
          fnIsLast  = true;
        }
        break;
      }
    }
View Full Code Here

 
  public Event next() {
    int split = line.indexOf(' ');
    String outcome = line.substring(0,split);
    String question = line.substring(split+1);
    Parse query = ParserTool.parseLine(question,parser,1)[0];
    return (new Event(outcome, atcg.getContext(query)));
  }
View Full Code Here

      words[i] = children[i].toString();//<co id="cp.words"/>
    }
    String[] tags = tagger.tag(words);//<co id="cp.tag"/>
    tagger.probs(probs);//<co id="cp.probs"/>
    for (int j = 0; j < words.length; j++) {
      Parse word = children[j];
      double prob = probs[j];
      tokens.insert(new Parse(word.getText(), word.getSpan(), tags[j], prob, j));//<co id="cp.augment"/>
      tokens.addProb(Math.log(prob));
    }
    /*
    <calloutlist>
        <callout arearefs="cp.child"><para>The <methodname>parse</methodname> is a callback method from an internal OpenNLP API that tokenizes the original text.</para></callout>
        <callout arearefs="cp.words"><para>Get just the words for use with the tagger</para></callout>
        <callout arearefs="cp.tag"><para>Part of speech tag the words</para></callout>
        <callout arearefs="cp.probs"><para></para></callout>
        <callout arearefs="cp.augment"><para>Augment the initial parse with the part of speech information</para></callout>
    </calloutlist>
    */
    //<end id="cp.pos"/>
    String[] chunks = chunker.chunk(words, tags);
    chunker.probs(probs);
    int chunkStart = -1;
    String chunkType = null;
    double logProb=0;
    for (int ci=0,cn=chunks.length;ci<cn;ci++) {
      if (ci > 0 && !chunks[ci].startsWith("I-") && !chunks[ci-1].equals("O")) {
        Span span = new Span(children[chunkStart].getSpan().getStart(),children[ci-1].getSpan().getEnd());
        tokens.insert(new Parse(tokens.getText(), span, chunkType, logProb,children[ci-1]));
        logProb=0;
      }           
      if (chunks[ci].startsWith("B-")) {
        chunkStart = ci;
        chunkType = chunks[ci].substring(2);
      }
      logProb+=Math.log(probs[ci]);
    }
    if (!chunks[chunks.length-1].equals("O")) {
      int ci = chunks.length;
      Span span = new Span(children[chunkStart].getSpan().getStart(),children[ci-1].getSpan().getEnd());
      tokens.insert(new Parse(tokens.getText(), span, chunkType, logProb,children[ci-1]));
    }
    return tokens;
  }
View Full Code Here

      tokenSpans[i] = new Span(startOffset, textBuilder.length());
    }
   
    String text = textBuilder.toString();
   
    Parse p = new Parse(text, new Span(0, text.length()), AbstractBottomUpParser.INC_NODE, 0, 0);
   
    for (int i = 0; i < tokenSpans.length; i++) {
      Span tokenSpan = tokenSpans[i];
      p.insert(new Parse(text, new Span(tokenSpan.getStart(), tokenSpan.getEnd()), AbstractBottomUpParser.TOK_NODE, 0, i));
    }
   
    return p;
  }
View Full Code Here

TOP

Related Classes of opennlp.tools.parser.Parse

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.