Package functionality

Source Code of functionality.SentencesToTree

/*
*    Copyright (C) 2012  Marta Rodr�guez, Teresa de Salas, Ana Vargas
*
*    This program is free software: you can redistribute it and/or
*    modify it under the terms of the GNU General Public License as
*    published by the Free Software Foundation, either version 3 of
*    the License, or any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*   You should have received a copy of the GNU General Public License
*   along with this program. If not, see
*   http://www.gnu.org/licenses/.
*/

package functionality;

import java.util.ArrayList;

import opennlp.tools.parser.ParserModel;
import opennlp.tools.tokenize.Tokenizer;
import opennlp.tools.tokenize.TokenizerME;
import opennlp.tools.tokenize.TokenizerModel;

/**
*
* @author Marta Rodr�guez
* @author Teresa de Salas
* @author Ana Vargas
*
*/
public class SentencesToTree {
  /**
   * original phrase
   */
  private String text;
  /**
   * build from the original phrase
   */
  private Tree tree;
 
  /**
   *
   * @param text
   */
  public SentencesToTree(String text, TokenizerModel model){
    /* Configure the tokenizer with preloaded model */
    Tokenizer tokenizer = new TokenizerME(model);
    /* tokens has an array of strings, where each string is a token */
    String s = spaces(tokenizer.tokenize(text));
    this.text = this.upperCase(s);
  }
 
  /**
   *
   * @param parsModel
   * @return is is possible split return final sentence
   * null in other case
   */
  public String toTree(ParserModel parsModel){
    Parsing par = new Parsing(text);
    CreateTree ct = new CreateTree(par.getParsing(parsModel));
    ct.analyzes();
    tree = new Tree(ct.getTree());
    tree.putLevel(1);
    tree.whatStructure();
    if(tree.getStructure()!= 0){
      ArrayList<Tree> aT = tree.splitTree();
      aT.get(0).leavesTree(aT.get(0));
      aT.get(1).leavesTree(aT.get(1));
      String first = aT.get(0).leavesToString();
      String second = aT.get(1).leavesToString();
      second = upperCase(second);
      tree.setFinalSent(first.concat(" ".concat(second)));
      return tree.getFinalSent();
    }
    else return null;
 
 
  /**
   *
   * @param str
   * @return a word with the first letter changed to upper case
   */
  private String upperCase(String str) {
    char c = str.charAt(0);
    String firstLetter = "";
    firstLetter = firstLetter + c;
    firstLetter = firstLetter.toUpperCase();
    return firstLetter.concat(str.substring(1, str.length()));
  }
 
  /**
   *
   * @param tokens
   * @return sentence with a spaces between the tokens
   */
  public String spaces(String[] tokens){
    String c = "";
    for(int i = 0; i < tokens.length; i++){
      c = c.concat(tokens[i]);
      c = c.concat(" ");
    }
    return c.substring(0, c.length()-1);
  }
}
TOP

Related Classes of functionality.SentencesToTree

TOP
Copyright © 2018 www.massapi.com. 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.