All Downloads are FREE. Search and download functionalities are using the official Maven repository.

justhalf.nlp.depparser.StanfordDepParser Maven / Gradle / Ivy

package justhalf.nlp.depparser;

import java.lang.reflect.Field;
import java.util.List;

import edu.stanford.nlp.international.Language;
import edu.stanford.nlp.ling.CoreLabel;
import edu.stanford.nlp.parser.nndep.DependencyParser;
import edu.stanford.nlp.trees.GrammaticalStructure;
import edu.stanford.nlp.trees.TypedDependency;

/**
 * An implementation of {@link DepParser} using Stanford CoreNLP
 */
public class StanfordDepParser implements DepParser {
	
	/** The path to default dependency parser model for English with standard labels */
	public static final String STANDARD_ENGLISH = "edu/stanford/nlp/models/parser/nndep/english_SD.gz";

	/**
	 * The path to default dependency parser model for English with universal labels
* See http://universaldependencies.org/ for more information * on Universal Dependencies */ public static final String UNIVERSAL_ENGLISH = "edu/stanford/nlp/models/parser/nndep/english_UD.gz"; public DependencyParser dependencyParser; public StanfordDepParser() { this(DependencyParser.DEFAULT_MODEL); } public StanfordDepParser(String modelPath){ dependencyParser = DependencyParser.loadFromModelFile(modelPath); Field _lang; try { _lang = DependencyParser.class.getDeclaredField("language"); _lang.setAccessible(true); if(modelPath == STANDARD_ENGLISH){ _lang.set(dependencyParser, Language.English); } } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) { e.printStackTrace(); } } @Override public boolean isThreadSafe() { return true; } @Override public List parse(List sentence) { check(sentence); GrammaticalStructure structure = dependencyParser.predict(sentence); return structure.typedDependenciesCCprocessed(); } private void check(List sentence){ for(CoreLabel word: sentence){ if(word.tag() == null || word.tag().length() == 0){ throw new IllegalStateException("StanfordDepParser requires every word in the sentence to have a POS tag"); } } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy