提高基于Stanford-tagger程序的性能

Ame*_*eer 8 java nlp pos-tagger stanford-nlp

我刚刚实现了一个在Java中使用Stanford POS标记器的程序.

我使用了几KB大小的输入文件,由几百个单词组成.我甚至将堆大小设置为600 MB.

但它仍然很慢,有时会耗尽堆内存.如何提高执行速度和内存性能?我希望能够使用几MB作为输入.

  public static void postag(String args) throws ClassNotFoundException

  {

     try

     {

     File filein=new File("c://input.txt");

     String content = FileUtils.readFileToString(filein);

     MaxentTagger tagger = new MaxentTagger("postagging/wsj-0-18-bidirectional-distsim.tagger");

     String tagged = tagger.tagString(content);

        try 
        {
            File file = new File("c://output.txt");
            if (!file.exists()) 
            {
                file.createNewFile();
            } 

            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write("\n"+tagged);
            bw.close();

            }
              catch (IOException e) 
              {
                    e.printStackTrace();
               }

     } catch (IOException e1)
     {
         e1.printStackTrace();
     }

 }
Run Code Online (Sandbox Code Playgroud)

Chr*_*ing 7

主要的第一条建议是使用wsj-0-18-left3words-distsim.tagger(或者可能更好的,english-left3words-distsim.tagger在最近的版本中,对于一般文本),而不是wsj-0-18-bidirectional-distsim.tagger.虽然双向恶搞的标注性能分数更好,这是大约6倍还慢,大约两倍的内存.图FWIW:在2012 MacBook Pro上,当给予足够的文本以"预热"时,left3words标记器将以每秒约35000字的速度标记文本.

关于内存使用的另一条建议是,如果你有大量的文本,请确保将它传递给tagString()合理大小的块,而不是全部作为一个巨大的String,因为整个String将被立即标记化,添加到内存要求.