Java Code Examples for edu.stanford.nlp.parser.lexparser.LexicalizedParser

The following are top voted examples for showing how to use edu.stanford.nlp.parser.lexparser.LexicalizedParser. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: event-direct-mts   File: SentenceExtractThread.java   View source code 7 votes vote down vote up
public SentenceExtractThread(String resultDir,
        String filename_cluster_read, String extractedSentencesSaveDir,
        String textDir, LexicalizedParser lp, String dictPath) {
    super();
    this.clusterResultDir = resultDir;
    this.filename_cluster_read = filename_cluster_read;
    this.extractedSentencesSaveDir = extractedSentencesSaveDir;
    this.textDir = textDir;
    this.lp = lp;
    try {
        this.dict = WordNetUtil.openDictionary(dictPath);
    } catch (final IOException e) {
        this.log.error("打开WordNet失败!", e);
        //e.printStackTrace();
    }
}
 
Example 2
Project: TASC-Tuples   File: ParserDemo.java   View source code 7 votes vote down vote up
/**
 * demoDP demonstrates turning a file into tokens and then parse trees. Note
 * that the trees are printed by calling pennPrint on the Tree object. It is
 * also possible to pass a PrintWriter to pennPrint if you want to capture
 * the output.
 * 
 * file => tokens => parse trees
 */
public static void demoDP(LexicalizedParser lp, String filename) {
	// This option shows loading, sentence-segmenting and tokenizing
	// a file using DocumentPreprocessor.
	TreebankLanguagePack tlp = new PennTreebankLanguagePack();
	GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
	// You could also create a tokenizer here (as below) and pass it
	// to DocumentPreprocessor
	for (List<HasWord> sentence : new DocumentPreprocessor(filename)) {
		Tree parse = lp.apply(sentence);
		parse.pennPrint();
		System.out.println();

		GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
		Collection tdl = gs.typedDependenciesCCprocessed();
		System.out.println(tdl);
		System.out.println();
	}
}
 
Example 3
Project: Text2Process   File: ParserPanel.java   View source code 7 votes vote down vote up
@Override
public void run() {
  try {
    parser = new edu.stanford.nlp.parser.lexparser.LexicalizedParser(filename);
  } catch (Exception ex) {
    JOptionPane.showMessageDialog(ParserPanel.this, "Error loading parser: " + filename, null, JOptionPane.ERROR_MESSAGE);
    setStatus("Error loading parser");
    parser = null;
  } catch (OutOfMemoryError e) {
    JOptionPane.showMessageDialog(ParserPanel.this, "Could not load parser. Out of memory.", null, JOptionPane.ERROR_MESSAGE);
    setStatus("Error loading parser");
    parser = null;
  }

  stopProgressMonitor();
  if (parser != null) {
    setStatus("Loaded parser.");
    parserFileLabel.setText("Parser: " + filename);
    parseButton.setEnabled(true);
    parseNextButton.setEnabled(true);
  }
}
 
Example 4
Project: Text2Process   File: ParserDemo.java   View source code 7 votes vote down vote up
public static void main(String[] args) {
  LexicalizedParser lp = new LexicalizedParser("englishPCFG.ser.gz");
  lp.setOptionFlags(new String[]{"-maxLength", "80", "-retainTmpSubcategories"});

  String[] sent = { "This", "is", "an", "easy", "sentence", "." };
  Tree parse = (Tree) lp.apply(Arrays.asList(sent));
  parse.pennPrint();
  System.out.println();

  TreebankLanguagePack tlp = new PennTreebankLanguagePack();
  GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
  GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
  Collection tdl = gs.typedDependenciesCollapsed();
  System.out.println(tdl);
  System.out.println();

  TreePrint tp = new TreePrint("penn,typedDependenciesCollapsed");
  tp.printTree(parse);
}
 
Example 5
Project: Text2Process   File: T2PStanfordWrapper.java   View source code 7 votes vote down vote up
/**
 * 
 */
public T2PStanfordWrapper() {
	try {
		ObjectInputStream in;
	    InputStream is;
	    URL u = T2PStanfordWrapper.class.getResource("/englishFactored.ser.gz");
	    if(u == null){
	    	//opening from IDE
	    	is = new FileInputStream(new File("resources/englishFactored.ser.gz"));		    		    	
	    }else{
	    	//opening from jar
	    	URLConnection uc = u.openConnection();
		    is = uc.getInputStream(); 				    
	    }
	    in = new ObjectInputStream(new GZIPInputStream(new BufferedInputStream(is)));  
	    f_parser = new LexicalizedParser(in);
		f_tlp = new PennTreebankLanguagePack(); //new ChineseTreebankLanguagePack();
	    f_gsf = f_tlp.grammaticalStructureFactory();
	}catch(Exception ex) {
		ex.printStackTrace();
	}	    
	//option flags as in the Parser example, but without maxlength
	f_parser.setOptionFlags(new String[]{"-retainTmpSubcategories"});				
	//f_parser.setOptionFlags(new String[]{"-segmentMarkov"});				
	Test.MAX_ITEMS = 4000000; //enables parsing of long sentences
}
 
Example 6
Project: Text2Process   File: ParserDemo.java   View source code 7 votes vote down vote up
public static void main(String[] args) {
  LexicalizedParser lp = new LexicalizedParser("parsers/englishFactored.ser.gz");
  lp.setOptionFlags(new String[]{"-maxLength", "80", "-retainTmpSubcategories"});

  Tree parse = (Tree) lp.apply("Try this sentence, which is slightly longer.");

  TreebankLanguagePack tlp = new PennTreebankLanguagePack();
  GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
  GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
  Collection<TypedDependency> tdl = gs.typedDependenciesCollapsed();
  TypedDependency td = tdl.iterator().next();
  TreeGraphNode node = td.dep();
  node = (TreeGraphNode) node.parent();
  node.deepCopy();
  

  
}
 
Example 7
Project: stanford-nlp   File: ParserAnnotator.java   View source code 7 votes vote down vote up
public static String signature(String annotatorName, Properties props) {
  StringBuilder os = new StringBuilder();
  os.append(annotatorName + ".model:" +
          props.getProperty(annotatorName + ".model",
                  LexicalizedParser.DEFAULT_PARSER_LOC));
  os.append(annotatorName + ".debug:" +
          props.getProperty(annotatorName + ".debug", "false"));
  os.append(annotatorName + ".flags:" +
          props.getProperty(annotatorName + ".flags", ""));
  os.append(annotatorName + ".maxlen:" +
          props.getProperty(annotatorName + ".maxlen", "-1"));
  os.append(annotatorName + ".treemap:" +
          props.getProperty(annotatorName + ".treemap", ""));
  os.append(annotatorName + ".maxtime:" +
          props.getProperty(annotatorName + ".maxtime", "0"));
  os.append(annotatorName + ".buildgraphs:" +
          props.getProperty(annotatorName + ".buildgraphs", "true"));
  os.append(annotatorName + ".nthreads:" + 
            props.getProperty(annotatorName + ".nthreads", props.getProperty("nthreads", "")));
  return os.toString();
}
 
Example 8
Project: dependensee   File: Main.java   View source code 6 votes vote down vote up
public static void writeImage(String sentence, String outFile, int scale) throws Exception {
    
    LexicalizedParser lp = null;
    try {
        lp = LexicalizedParser.loadModel("edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz");
    } catch (Exception e) {
        System.err.println("Could not load file englishPCFG.ser.gz. Try placing this file in the same directory as Dependencee.jar");
        return;
    }
    
    lp.setOptionFlags(new String[]{"-maxLength", "500", "-retainTmpSubcategories"});
    TokenizerFactory<CoreLabel> tokenizerFactory =
            PTBTokenizer.factory(new CoreLabelTokenFactory(), "");
    List<CoreLabel> wordList = tokenizerFactory.getTokenizer(new StringReader(sentence)).tokenize();
    Tree tree = lp.apply(wordList);
    writeImage(tree, outFile, scale);
    
}
 
Example 9
Project: dependensee   File: MainTest.java   View source code 6 votes vote down vote up
/**
 * Test of writeImage method, of class Main.
 */

@Test
public void testWriteImage() throws Exception {
    String text = "A quick brown fox jumped over the lazy dog.";
    TreebankLanguagePack tlp = new PennTreebankLanguagePack();
    GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
    LexicalizedParser lp = LexicalizedParser.loadModel();
    lp.setOptionFlags(new String[]{"-maxLength", "500", "-retainTmpSubcategories"});
    TokenizerFactory<CoreLabel> tokenizerFactory =
            PTBTokenizer.factory(new CoreLabelTokenFactory(), "");
    List<CoreLabel> wordList = tokenizerFactory.getTokenizer(new StringReader(text)).tokenize();
    Tree tree = lp.apply(wordList);
    GrammaticalStructure gs = gsf.newGrammaticalStructure(tree);
    Collection<TypedDependency> tdl = gs.typedDependenciesCollapsed();
    Main.writeImage(tdl, "image.png", 3);
    assert (new File("image.png").exists());
}
 
Example 10
Project: eventspotter   File: ParserDemo.java   View source code 6 votes vote down vote up
public static void demoDP(LexicalizedParser lp, String filename) {
  // This option shows loading and sentence-segment and tokenizing
  // a file using DocumentPreprocessor
  TreebankLanguagePack tlp = new PennTreebankLanguagePack();
  GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
  // You could also create a tokenizer here (as below) and pass it
  // to DocumentPreprocessor
  for (List<HasWord> sentence : new DocumentPreprocessor(filename)) {
    Tree parse = lp.apply(sentence);
    parse.pennPrint();
    System.out.println();

    GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
    Collection tdl = gs.typedDependenciesCCprocessed(true);
    System.out.println(tdl);
    System.out.println();
  }
}
 
Example 11
Project: stanford-parser   File: ParserDemo.java   View source code 6 votes vote down vote up
public static void demoDP(LexicalizedParser lp, String filename) {
  // This option shows loading and sentence-segment and tokenizing
  // a file using DocumentPreprocessor
  TreebankLanguagePack tlp = new PennTreebankLanguagePack();
  GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
  // You could also create a tokenizer here (as below) and pass it
  // to DocumentPreprocessor
  for (List<HasWord> sentence : new DocumentPreprocessor(filename)) {
    Tree parse = lp.apply(sentence);
    parse.pennPrint();
    System.out.println();

    GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
    Collection tdl = gs.typedDependenciesCCprocessed(true);
    System.out.println(tdl);
    System.out.println();
  }
}
 
Example 12
Project: gateplugin-Stanford_CoreNLP   File: Parser.java   View source code 5 votes vote down vote up
private void instantiateStanfordParser()
    throws ResourceInstantiationException {
  if(stanfordParser != null) return;
  try {
    // String filepath = Files.fileFromURL(parserFile).getAbsolutePath();
    stanfordParser =
        LexicalizedParser.getParserFromSerializedFile(parserFile
            .toExternalForm());
  } catch(Exception e) {
    throw new ResourceInstantiationException(e);
  }
}
 
Example 13
Project: schemas   File: DirectoryParser.java   View source code 5 votes vote down vote up
private void initLexResources() {
  try {
    options = new Options();
    options.testOptions.verbose = true;
    // Parser
    parser = LexicalizedParser.loadModel(_serializedGrammar);
    //parser = new LexicalizedParser(_serializedGrammar, options);
  } catch( Exception ex ) { ex.printStackTrace(); }

  // Dependency tree info
  TreebankLanguagePack tlp = new PennTreebankLanguagePack();
  gsf = tlp.grammaticalStructureFactory();
}
 
Example 14
Project: WikiKreator   File: ParseErrorChecker.java   View source code 5 votes vote down vote up
public static void main(String[] args) throws IOException {
	
	LexicalizedParser parser = LexicalizedParser.loadModel();
	File[] files=new File(fileDir).listFiles();
	int num=0;
	double score=0.0;
	for(File f:files)
	{
		if(f.isDirectory())
			continue;
		BufferedReader br=new BufferedReader(new FileReader(f.getAbsolutePath()));
		String line="";
		while((line=br.readLine())!=null)
		{
			StringTokenizer st=new StringTokenizer(line, "\\.");
			while(st.hasMoreTokens())
			{
				score=score+parser.parse(st.nextToken()).score();
				num++;
			}
			
		}
		System.out.println(score+" for "+f.getName());
		br.close();
		
	}
	
	System.out.println(score+"/"+num);
	
	System.out.println(parser.parse(s).score());

	
}
 
Example 15
Project: weibo-emotion-analyzer   File: Sentence.java   View source code 5 votes vote down vote up
private void setParse(){
    if (this.segtext == null || this.segtext.length() == 0) {
   	    StringBuffer sb = new StringBuffer();
           for(String w : seggedText)
           {
               sb.append(w + " ");
           }
           segtext = sb.toString();
    }
	LexicalizedParser lp=DicModel.loadParser();
	Tree t = lp.parse(segtext);   
    ChineseGrammaticalStructure gs = new ChineseGrammaticalStructure(t);  
    parseResult = gs.typedDependenciesCollapsed();
}
 
Example 16
Project: TASC-Tuples   File: ParserDemo.java   View source code 5 votes vote down vote up
/**
 * The main method demonstrates the easiest way to load a parser. Simply
 * call loadModel and specify the path of a serialized grammar model, which
 * can be a file, a resource on the classpath, or even a URL. For example,
 * this demonstrates loading from the models jar file, which you therefore
 * need to include in the classpath for ParserDemo to work.
 */
public static void main(String[] args) {
	LexicalizedParser lp = LexicalizedParser
			.loadModel("edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz");
	if (args.length > 0) {
		demoDP(lp, args[0]);
	} else {
		demoAPI(lp);
	}
}
 
Example 17
Project: TASC-Tuples   File: ParserDemo.java   View source code 5 votes vote down vote up
/**
 * demoAPI demonstrates other ways of calling the parser with already
 * tokenized text, or in some cases, raw text that needs to be tokenized as
 * a single sentence. Output is handled with a TreePrint object. Note that
 * the options used when creating the TreePrint can determine what results
 * to print out. Once again, one can capture the output by passing a
 * PrintWriter to TreePrint.printTree.
 * 
 * difference: already tokenized text
 * 
 * 
 */
public static void demoAPI(LexicalizedParser lp) {
	// This option shows parsing a list of correctly tokenized words
	String[] sent = { "This", "is", "an", "easy", "sentence", "." };
	List<CoreLabel> rawWords = Sentence.toCoreLabelList(sent);
	Tree parse = lp.apply(rawWords);
	parse.pennPrint();
	System.out.println();

	// This option shows loading and using an explicit tokenizer
	String sent2 = "Hey @Apple, pretty much all your products are amazing. You blow minds every time you launch a new gizmo."
			+ " that said, your hold music is crap";
	TokenizerFactory<CoreLabel> tokenizerFactory = PTBTokenizer.factory(
			new CoreLabelTokenFactory(), "");
	Tokenizer<CoreLabel> tok = tokenizerFactory
			.getTokenizer(new StringReader(sent2));
	List<CoreLabel> rawWords2 = tok.tokenize();
	parse = lp.apply(rawWords2);

	TreebankLanguagePack tlp = new PennTreebankLanguagePack();
	GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
	GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
	List<TypedDependency> tdl = gs.typedDependenciesCCprocessed();
	System.out.println(tdl);
	System.out.println();

	// You can also use a TreePrint object to print trees and dependencies
	TreePrint tp = new TreePrint("penn,typedDependenciesCollapsed");
	tp.printTree(parse);
}
 
Example 18
Project: TASC-Tuples   File: GenerateDependency.java   View source code 5 votes vote down vote up
public static void main(String[] args) {
	// TODO Auto-generated method stub
	LexicalizedParser lp = LexicalizedParser
			.loadModel("edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz");
	if (args.length > 0) {
		String contentfilename = args[0];
		String authorfilename = args[1];
		String dependencyfilename = args[2];
		DependencyParser dependencyParser = new DependencyParser();
		ArrayList<ArrayList<String>> ret = dependencyParser
				.getDependencyByLine(lp, contentfilename, authorfilename);
		try {
			BufferedWriter bw = new BufferedWriter(new FileWriter(
					dependencyfilename));
			for (ArrayList<String> arr : ret) {

				bw.write(arr.get(0) + "\t" + arr.get(1) + "\t" + arr.get(2)
						+ "\t" + arr.get(3) + "\t" + arr.get(4) + "\t"
						+ arr.get(5) + "\t" + arr.get(6) + "\n");
			}
			bw.flush();
			bw.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	} else {
		System.out
				.println("java -jar GenerateDependency.jar contentfilename authorfilename dependencyfilename");
	}
}
 
Example 19
Project: pikes   File: DependenciesBuilder.java   View source code 5 votes vote down vote up
public void init() {
  this.lp = LexicalizedParser.loadModel("edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz",
                                        "-maxLength", "80", "-retainTmpSubcategories");
  this.tlp = new PennTreebankLanguagePack();
  this.gsf = this.tlp.grammaticalStructureFactory();
  //this.parsedTree = new ArrayList<DependencyTree>();
  //this.trees = new ArrayList<Tree>();
}
 
Example 20
Project: ViTA   File: Parser.java   View source code 5 votes vote down vote up
private void instantiateStanfordParser()
  throws ResourceInstantiationException {
  if(stanfordParser != null) return;
  
  try {
    String filepath = Files.fileFromURL(parserFile).getAbsolutePath();
    stanfordParser = LexicalizedParser.getParserFromSerializedFile(filepath);
  }
  catch(Exception e) {
    throw new ResourceInstantiationException(e);
  }
}
 
Example 21
Project: stanford-nlp   File: ParserAnnotator.java   View source code 5 votes vote down vote up
public ParserAnnotator(LexicalizedParser parser, boolean verbose, int maxSent, Function<Tree, Tree> treeMap) {
  VERBOSE = verbose;
  this.BUILD_GRAPHS = parser.getTLPParams().supportsBasicDependencies();
  this.parser = parser;
  this.maxSentenceLength = maxSent;
  this.treeMap = treeMap;
  this.maxParseTime = 0;
  if (this.BUILD_GRAPHS) {
    TreebankLanguagePack tlp = parser.getTLPParams().treebankLanguagePack();
    this.gsf = tlp.grammaticalStructureFactory(tlp.punctuationWordRejectFilter(), tlp.typedDependencyHeadFinder());
  } else {
    this.gsf = null;
  }
  this.nThreads = 1;
}
 
Example 22
Project: stanford-nlp   File: ParserAnnotator.java   View source code 5 votes vote down vote up
private static LexicalizedParser loadModel(String parserLoc,
                                                  boolean verbose,
                                                  String[] flags) {
  if (verbose) {
    System.err.println("Loading Parser Model [" + parserLoc + "] ...");
  }
  LexicalizedParser result = LexicalizedParser.loadModel(parserLoc, flags);
  // lp.setOptionFlags(new String[]{"-outputFormat", "penn,typedDependenciesCollapsed", "-retainTmpSubcategories"});
  // treePrint = lp.getTreePrint();

  return result;
}
 
Example 23
Project: dependensee   File: Main.java   View source code 5 votes vote down vote up
public static Graph getGraph(String sentence) throws Exception {
    LexicalizedParser lp = LexicalizedParser.loadModel("edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz");
    lp.setOptionFlags(new String[]{"-maxLength", "500", "-retainTmpSubcategories"});
    TokenizerFactory<CoreLabel> tokenizerFactory =
            PTBTokenizer.factory(new CoreLabelTokenFactory(), "");
    List<CoreLabel> wordList = tokenizerFactory.getTokenizer(new StringReader(sentence)).tokenize();
    Tree tree = lp.apply(wordList);
    GrammaticalStructure gs = gsf.newGrammaticalStructure(tree);
    Collection<TypedDependency> tdl = gs.typedDependencies();
    return getGraph(tree, tdl);
}
 
Example 24
Project: dependensee   File: Main.java   View source code 5 votes vote down vote up
public static Graph getGraph(String sentence, LexicalizedParser lp) throws Exception {
    TokenizerFactory<CoreLabel> tokenizerFactory =
            PTBTokenizer.factory(new CoreLabelTokenFactory(), "");
    List<CoreLabel> wordList = tokenizerFactory.getTokenizer(new StringReader(sentence)).tokenize();
    Tree tree = lp.apply(wordList);
    GrammaticalStructure gs = gsf.newGrammaticalStructure(tree);
    Collection<TypedDependency> tdl = gs.typedDependencies();
    return getGraph(tree, tdl);
}
 
Example 25
Project: dependensee   File: Main.java   View source code 5 votes vote down vote up
public static void writeImage(String sentence, String outFile, LexicalizedParser lp) throws Exception {
    
    Tree parse;
    try {
        TokenizerFactory<CoreLabel> tokenizerFactory =
            PTBTokenizer.factory(new CoreLabelTokenFactory(), "");
    List<CoreLabel> wordList = tokenizerFactory.getTokenizer(new StringReader(sentence)).tokenize();
        parse = lp.apply(wordList);            
    } catch (Exception e) {
        throw e;
    }
    writeImage(parse, outFile);
    
}
 
Example 26
Project: stanford-thrift   File: StanfordParserThrift.java   View source code 5 votes vote down vote up
private void loadModel(String modelFile)
{
    if (modelFile.equals("") || modelFile == null) {
        parser = LexicalizedParser.loadModel(DefaultPaths.DEFAULT_PARSER_MODEL, new String[]{});
    }
    else {
        parser = LexicalizedParser.loadModel(modelFile, new String[]{});
    }
}
 
Example 27
Project: eventspotter   File: LexicalizedParserServer.java   View source code 5 votes vote down vote up
public LexicalizedParserServer(int port, String model, 
                               LexicalizedParser parser)
  throws IOException
{
  this.port = port;
  this.serverSocket = new ServerSocket(port);
  this.model = model;
  this.parser = parser;
}
 
Example 28
Project: eventspotter   File: LexicalizedParserServer.java   View source code 5 votes vote down vote up
public static void main(String[] args) 
  throws IOException
{
  System.setOut(new PrintStream(System.out, true, "utf-8"));
  System.setErr(new PrintStream(System.err, true, "utf-8"));

  int port = DEFAULT_PORT;
  String model = LexicalizedParser.DEFAULT_PARSER_LOC;

  for (int i = 0; i < args.length; i += 2) {
    if (i + 1 >= args.length) {
      System.err.println("Unspecified argument " + args[i]);
      System.exit(2);
    }
    String arg = args[i];
    if (arg.startsWith("--")) {
      arg = arg.substring(2);
    } else if (arg.startsWith("-")) {
      arg = arg.substring(1);
    }
    if (arg.equalsIgnoreCase("model")) {
      model = args[i + 1];
    } else if (arg.equalsIgnoreCase("port")) {
      port = Integer.valueOf(args[i + 1]);
    }
  }
  
  LexicalizedParserServer server = new LexicalizedParserServer(port, model);
  System.err.println("Server ready!");
  server.listen();
}
 
Example 29
Project: eventspotter   File: ParserPanel.java   View source code 5 votes vote down vote up
@Override
public void run() {
  try {
    if (zipFilename != null) {
      parser = LexicalizedParser.loadModelFromZip(zipFilename, filename);
    } else {
      parser = LexicalizedParser.loadModel(filename);
    }
  } catch (Exception ex) {
    JOptionPane.showMessageDialog(ParserPanel.this, "Error loading parser: " + filename, null, JOptionPane.ERROR_MESSAGE);
    setStatus("Error loading parser");
    parser = null;
  } catch (OutOfMemoryError e) {
    JOptionPane.showMessageDialog(ParserPanel.this, "Could not load parser. Out of memory.", null, JOptionPane.ERROR_MESSAGE);
    setStatus("Error loading parser");
    parser = null;
  }

  stopProgressMonitor();
  if (parser != null) {
    setStatus("Loaded parser.");
    parserFileLabel.setText("Parser: " + filename);
    parseButton.setEnabled(true);
    parseNextButton.setEnabled(true);
    saveOutputButton.setEnabled(true);

    tlp = parser.getOp().langpack();
    encoding = tlp.getEncoding();
  }
}
 
Example 30
Project: eventspotter   File: ParserDemo.java   View source code 5 votes vote down vote up
public static void main(String[] args) {
  LexicalizedParser lp = LexicalizedParser.loadModel("edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz");
  if (args.length > 0) {
    demoDP(lp, args[0]);
  } else {
    demoAPI(lp);
  }
}
 
Example 31
Project: eventspotter   File: ParserDemo.java   View source code 5 votes vote down vote up
public static void demoAPI(LexicalizedParser lp) {
  // This option shows parsing a list of correctly tokenized words
  String[] sent = { "This", "is", "an", "easy", "sentence", "." };
  List<CoreLabel> rawWords = Sentence.toCoreLabelList(sent);
  Tree parse = lp.apply(rawWords);
  parse.pennPrint();
  System.out.println();


  // This option shows loading and using an explicit tokenizer
  String sent2 = "This is another sentence.";
  TokenizerFactory<CoreLabel> tokenizerFactory =
    PTBTokenizer.factory(new CoreLabelTokenFactory(), "");
  List<CoreLabel> rawWords2 =
    tokenizerFactory.getTokenizer(new StringReader(sent2)).tokenize();
  parse = lp.apply(rawWords2);

  TreebankLanguagePack tlp = new PennTreebankLanguagePack();
  GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
  GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
  List<TypedDependency> tdl = gs.typedDependenciesCCprocessed();
  System.out.println(tdl);
  System.out.println();

  TreePrint tp = new TreePrint("penn,typedDependenciesCollapsed");
  tp.printTree(parse);
}
 
Example 32
Project: stanford-parser   File: LexicalizedParserServer.java   View source code 5 votes vote down vote up
public LexicalizedParserServer(int port, String model, 
                               LexicalizedParser parser)
  throws IOException
{
  this.port = port;
  this.serverSocket = new ServerSocket(port);
  this.model = model;
  this.parser = parser;
}
 
Example 33
Project: stanford-parser   File: LexicalizedParserServer.java   View source code 5 votes vote down vote up
public static void main(String[] args) 
  throws IOException
{
  System.setOut(new PrintStream(System.out, true, "utf-8"));
  System.setErr(new PrintStream(System.err, true, "utf-8"));

  int port = DEFAULT_PORT;
  String model = LexicalizedParser.DEFAULT_PARSER_LOC;

  for (int i = 0; i < args.length; i += 2) {
    if (i + 1 >= args.length) {
      System.err.println("Unspecified argument " + args[i]);
      System.exit(2);
    }
    String arg = args[i];
    if (arg.startsWith("--")) {
      arg = arg.substring(2);
    } else if (arg.startsWith("-")) {
      arg = arg.substring(1);
    }
    if (arg.equalsIgnoreCase("model")) {
      model = args[i + 1];
    } else if (arg.equalsIgnoreCase("port")) {
      port = Integer.valueOf(args[i + 1]);
    }
  }
  
  LexicalizedParserServer server = new LexicalizedParserServer(port, model);
  System.err.println("Server ready!");
  server.listen();
}
 
Example 34
Project: stanford-parser   File: ParserPanel.java   View source code 5 votes vote down vote up
@Override
public void run() {
  try {
    if (zipFilename != null) {
      parser = LexicalizedParser.loadModelFromZip(zipFilename, filename);
    } else {
      parser = LexicalizedParser.loadModel(filename);
    }
  } catch (Exception ex) {
    JOptionPane.showMessageDialog(ParserPanel.this, "Error loading parser: " + filename, null, JOptionPane.ERROR_MESSAGE);
    setStatus("Error loading parser");
    parser = null;
  } catch (OutOfMemoryError e) {
    JOptionPane.showMessageDialog(ParserPanel.this, "Could not load parser. Out of memory.", null, JOptionPane.ERROR_MESSAGE);
    setStatus("Error loading parser");
    parser = null;
  }

  stopProgressMonitor();
  if (parser != null) {
    setStatus("Loaded parser.");
    parserFileLabel.setText("Parser: " + filename);
    parseButton.setEnabled(true);
    parseNextButton.setEnabled(true);
    saveOutputButton.setEnabled(true);

    tlp = parser.getOp().langpack();
    encoding = tlp.getEncoding();
  }
}
 
Example 35
Project: stanford-parser   File: ParserDemo.java   View source code 5 votes vote down vote up
public static void main(String[] args) {
  LexicalizedParser lp = LexicalizedParser.loadModel("edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz");
  if (args.length > 0) {
    demoDP(lp, args[0]);
  } else {
    demoAPI(lp);
  }
}
 
Example 36
Project: stanford-parser   File: ParserDemo.java   View source code 5 votes vote down vote up
public static void demoAPI(LexicalizedParser lp) {
  // This option shows parsing a list of correctly tokenized words
  String[] sent = { "This", "is", "an", "easy", "sentence", "." };
  List<CoreLabel> rawWords = Sentence.toCoreLabelList(sent);
  Tree parse = lp.apply(rawWords);
  parse.pennPrint();
  System.out.println();


  // This option shows loading and using an explicit tokenizer
  String sent2 = "This is another sentence.";
  TokenizerFactory<CoreLabel> tokenizerFactory =
    PTBTokenizer.factory(new CoreLabelTokenFactory(), "");
  List<CoreLabel> rawWords2 =
    tokenizerFactory.getTokenizer(new StringReader(sent2)).tokenize();
  parse = lp.apply(rawWords2);

  TreebankLanguagePack tlp = new PennTreebankLanguagePack();
  GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
  GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
  List<TypedDependency> tdl = gs.typedDependenciesCCprocessed();
  System.out.println(tdl);
  System.out.println();

  TreePrint tp = new TreePrint("penn,typedDependenciesCollapsed");
  tp.printTree(parse);
}
 
Example 37
Project: probschemas   File: Ling.java   View source code 4 votes vote down vote up
public static LexicalizedParser createParser(String grammarPath) {
  return LexicalizedParser.loadModel(grammarPath);
}
 
Example 38
Project: event-direct-mts   File: ClusterByChineseWhispers.java   View source code 4 votes vote down vote up
/**
 * 按照事件聚类的结果来对文本中的句子进行聚类
 *
 * @throws IOException
 */
public void clusterSentencesByEvents() throws IOException {
    final File clusterResultDir = new File(this.resultDir);
    // 获取所有的事件聚类结果文件(.read)
    final String[] filenames_cluster_read = clusterResultDir.list(new FilenameFilter() {

        @Override
        public boolean accept(File file, String name) {
            if (name.endsWith(".read")) {
                return true;
            }
            return false;
        }
    });

    // 加载词性标注模型
    OpenNlpPOSTagger.getInstance(this.moduleFilePath);

    //加载依存分析模型
    final String grammar = "edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz";
    final String[] options = { "-maxLength", "80", "-retainTmpSubcategories" };
    final LexicalizedParser lp = LexicalizedParser.loadModel(grammar, options);
    //TreebankLanguagePack tlp = lp.getOp().langpack();
    //GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();

    final ExecutorService executorService = Executors.newFixedThreadPool(this.threadNum);
    final List<Callable<Boolean>> tasks = new ArrayList<Callable<Boolean>>();
    for (final String filename_cluster_read : filenames_cluster_read) {
        //添加任务到任务列表
        tasks.add(new SentenceExtractThread(
                this.resultDir, filename_cluster_read, this.extractedSentencesSaveDir, this.textDir, lp, this.dictPath));
    }

    if(tasks.size() > 0){
        try {
            //执行任务组,所有任务执行完毕之前,主线程阻塞
            final List<Future<Boolean>> futures = executorService.invokeAll(tasks);
            executorService.shutdown();
            if(futures != null){
                for (final Future<Boolean> future : futures) {
                    future.get();
                }
            }
        } catch (InterruptedException | ExecutionException e) {
            this.log.error("执行任务组出错!", e);
            //e.printStackTrace();
        }
    }
}
 
Example 39
Project: event-direct-mts   File: ClusterByChineseWhispers.java   View source code 4 votes vote down vote up
public static void main(String[] args) throws IOException {
    final File clusterResultDir = new File("src/tmp");
    // 获取所有的事件聚类结果文件(.read)
    final String[] filenames_cluster_read = clusterResultDir.list(new FilenameFilter() {

        @Override
        public boolean accept(File file, String name) {
            // TODO Auto-generated method stub
            if (name.endsWith(".read")) {
                return true;
            }
            return false;
        }
    });

    // 加载词性标注模型
    OpenNlpPOSTagger.getInstance("src/en-pos-maxent.bin");

    //加载依存分析模型
    final String grammar = "edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz";
    final String[] options = { "-maxLength", "80", "-retainTmpSubcategories" };
    final LexicalizedParser lp = LexicalizedParser.loadModel(grammar, options);
    //TreebankLanguagePack tlp = lp.getOp().langpack();
    //GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();

    final ExecutorService executorService = Executors.newFixedThreadPool(2);
    final List<Callable<Boolean>> tasks = new ArrayList<Callable<Boolean>>();
    for (final String filename_cluster_read : filenames_cluster_read) {
        //添加任务到任务列表
        tasks.add(new SentenceExtractThread(
                "src/tmp", filename_cluster_read, "src/tmp/extract_sent", "src/tmp/text_dir", lp, "D:/WordNet/2.1/dict"));
    }

    if(tasks.size() > 0){
        try {
            //执行任务组,所有任务执行完毕之前,主线程阻塞
            final List<Future<Boolean>> futures = executorService.invokeAll(tasks);
            executorService.shutdown();
            if(futures != null){
                for (final Future<Boolean> future : futures) {
                    future.get();
                }
            }
        } catch (InterruptedException | ExecutionException e) {
            //log.error("执行任务组出错!", e);
            e.printStackTrace();
        }
    }
}
 
Example 40
Project: schemas   File: Ling.java   View source code 4 votes vote down vote up
public static LexicalizedParser createParser(String grammarPath) {
  return LexicalizedParser.loadModel(grammarPath);
}