Search in sources :

Example 1 with ParserGrammar

use of edu.stanford.nlp.parser.common.ParserGrammar in project CoreNLP by stanfordnlp.

the class LexicalizedParserServer method loadModel.

private static ParserGrammar loadModel(String parserModel, String taggerModel) {
    ParserGrammar model;
    if (taggerModel == null) {
        model = ParserGrammar.loadModel(parserModel);
    } else {
        model = ParserGrammar.loadModel(parserModel, "-preTag", "-taggerSerializedFile", taggerModel);
        // preload tagger so the first query doesn't take forever
        model.loadTagger();
    }
    model.setOptionFlags(model.defaultCoreNLPFlags());
    return model;
}
Also used : ParserGrammar(edu.stanford.nlp.parser.common.ParserGrammar)

Example 2 with ParserGrammar

use of edu.stanford.nlp.parser.common.ParserGrammar in project CoreNLP by stanfordnlp.

the class ParseAndSetLabels method main.

public static void main(String[] args) {
    // TODO: rather than always rolling our own arg parser, we should
    // find a library which does it for us nicely
    String outputFile = null;
    String sentencesFile = null;
    String labelsFile = null;
    String parserFile = LexicalizedParser.DEFAULT_PARSER_LOC;
    String taggerFile = null;
    MissingLabels missing = MissingLabels.DEFAULT;
    String defaultLabel = "-1";
    String separator = "\\t+";
    String saveUnknownsFile = null;
    String remapLabels = null;
    int argIndex = 0;
    boolean binarize = true;
    boolean useLabelKeys = false;
    while (argIndex < args.length) {
        if (args[argIndex].equalsIgnoreCase("-output")) {
            outputFile = args[argIndex + 1];
            argIndex += 2;
        } else if (args[argIndex].equalsIgnoreCase("-sentences")) {
            sentencesFile = args[argIndex + 1];
            argIndex += 2;
        } else if (args[argIndex].equalsIgnoreCase("-labels")) {
            labelsFile = args[argIndex + 1];
            argIndex += 2;
        } else if (args[argIndex].equalsIgnoreCase("-parser")) {
            parserFile = args[argIndex + 1];
            argIndex += 2;
        } else if (args[argIndex].equalsIgnoreCase("-tagger")) {
            taggerFile = args[argIndex + 1];
            argIndex += 2;
        } else if (args[argIndex].equalsIgnoreCase("-missing")) {
            missing = MissingLabels.valueOf(args[argIndex + 1]);
            argIndex += 2;
        } else if (args[argIndex].equalsIgnoreCase("-separator")) {
            separator = args[argIndex + 1];
            argIndex += 2;
        } else if (args[argIndex].equalsIgnoreCase("-default")) {
            defaultLabel = args[argIndex + 1];
            argIndex += 2;
        } else if (args[argIndex].equalsIgnoreCase("-saveUnknowns")) {
            saveUnknownsFile = args[argIndex + 1];
            argIndex += 2;
        } else if (args[argIndex].equalsIgnoreCase("-remapLabels")) {
            remapLabels = args[argIndex + 1];
            argIndex += 2;
        } else if (args[argIndex].equalsIgnoreCase("-binarize")) {
            binarize = true;
            argIndex += 1;
        } else if (args[argIndex].equalsIgnoreCase("-nobinarize")) {
            binarize = false;
            argIndex += 1;
        } else if (args[argIndex].equalsIgnoreCase("-useLabelKeys")) {
            useLabelKeys = true;
            argIndex += 1;
        } else if (args[argIndex].equalsIgnoreCase("-nouseLabelKeys")) {
            useLabelKeys = false;
            argIndex += 1;
        } else {
            throw new IllegalArgumentException("Unknown argument " + args[argIndex]);
        }
    }
    if (outputFile == null) {
        throw new IllegalArgumentException("-output is required");
    }
    if (sentencesFile == null && !useLabelKeys) {
        throw new IllegalArgumentException("-sentences or -useLabelKeys is required");
    }
    if (sentencesFile != null && useLabelKeys) {
        throw new IllegalArgumentException("Use only one of -sentences or -useLabelKeys");
    }
    if (labelsFile == null) {
        throw new IllegalArgumentException("-labels is required");
    }
    ParserGrammar parser = loadParser(parserFile, taggerFile);
    TreeBinarizer binarizer = null;
    if (binarize) {
        binarizer = TreeBinarizer.simpleTreeBinarizer(parser.getTLPParams().headFinder(), parser.treebankLanguagePack());
    }
    Map<String, String> labelMap = readLabelMap(labelsFile, separator, remapLabels);
    List<String> sentences;
    if (sentencesFile == null) {
        sentences = readSentences(sentencesFile);
    } else {
        sentences = new ArrayList<String>(labelMap.keySet());
    }
    List<Tree> trees = parseSentences(sentences, parser, binarizer);
    Set<String> unknowns = setLabels(trees, labelMap, missing, defaultLabel);
    writeTrees(trees, outputFile);
}
Also used : ParserGrammar(edu.stanford.nlp.parser.common.ParserGrammar) TreeBinarizer(edu.stanford.nlp.parser.lexparser.TreeBinarizer) Tree(edu.stanford.nlp.trees.Tree)

Example 3 with ParserGrammar

use of edu.stanford.nlp.parser.common.ParserGrammar in project CoreNLP by stanfordnlp.

the class ParserAnnotator method loadModel.

private static ParserGrammar loadModel(String parserLoc, boolean verbose, String[] flags) {
    if (verbose) {
        log.info("Loading Parser Model [" + parserLoc + "] ...");
        log.info("  Flags:");
        for (String flag : flags) {
            log.info("  " + flag);
        }
        log.info();
    }
    ParserGrammar result = ParserGrammar.loadModel(parserLoc);
    result.setOptionFlags(result.defaultCoreNLPFlags());
    result.setOptionFlags(flags);
    return result;
}
Also used : ParserGrammar(edu.stanford.nlp.parser.common.ParserGrammar)

Aggregations

ParserGrammar (edu.stanford.nlp.parser.common.ParserGrammar)3 TreeBinarizer (edu.stanford.nlp.parser.lexparser.TreeBinarizer)1 Tree (edu.stanford.nlp.trees.Tree)1