2017-10-12 22 views
1

OpenNLPを使用してnl-personTest.binファイルに余分な訓練データを追加しようとしています。 私は、余分なトレーニングデータを追加するためにコードを実行すると、既存のデータを削除し、新しいデータのみを追加するという私の問題です。既存のモデルに訓練データを追加する(ビンファイル)

私は余分なトレーニングデータを置き換える代わりに追加することができますか?

私は、誰でも任意のアイデアがこの問題を解決するための

public class TrainNames 
    { 
    public static void main(String[] args) 
    { 
     train("nl", "person", "namen.txt", "nl-ner-personTest.bin"); 
    } 

    public static String train(String lang, String entity,InputStreamFactory inputStream, FileOutputStream modelStream) { 

     Charset charset = Charset.forName("UTF-8"); 
     TokenNameFinderModel model = null; 
     ObjectStream<NameSample> sampleStream = null; 
     try { 
      ObjectStream<String> lineStream = new PlainTextByLineStream(inputStream, charset); 
      sampleStream = new NameSampleDataStream(lineStream); 
      TokenNameFinderFactory nameFinderFactory = new TokenNameFinderFactory(); 
      model = NameFinderME.train("nl", "person", sampleStream, TrainingParameters.defaultParams(), 
       nameFinderFactory); 
     } catch (FileNotFoundException fio) { 

     } catch (IOException io) { 

     } finally { 
      try { 
       sampleStream.close(); 
      } catch (IOException io) { 

      } 
     } 
     BufferedOutputStream modelOut = null; 
     try { 
      modelOut = new BufferedOutputStream(modelStream); 
      model.serialize(modelOut); 
     } catch (IOException io) { 

     } finally { 
      if (modelOut != null) { 
       try { 
        modelOut.close(); 
       } catch (IOException io) { 

       } 
      } 
     } 
     return "Something goes wrong with training module."; 
    } 

    public static String train(String lang, String entity, String taggedCoprusFile, 
           String modelFile) { 
     try { 
      InputStreamFactory inputStream = new InputStreamFactory() { 
       FileInputStream fileInputStream = new FileInputStream("namen.txt"); 

       public InputStream createInputStream() throws IOException { 
        return fileInputStream; 
       } 
      }; 

      return train(lang, entity, inputStream, 
       new FileOutputStream(modelFile)); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return "Something goes wrong with training module."; 
    } } 

Open NLP NER is not properly trainedからそれを得た)、次のコードを使用しましたか?

正確なトレーニングセットが必要な場合は、少なくとも15Kが必要です。 文章に書類が記載されています。

答えて

0

OpenNLPは既存のバイナリNLPモデルを拡張することをサポートしていないと思います。

利用可能なすべてのトレーニングデータがある場合は、それらをすべて収集してから、すぐにトレーニングします。 SequenceInputStreamを使用できます。私はちょうどあなたの情報のために、別のInputStreamFactory

public String train(String lang, String entity, InputStreamFactory inputStream, FileOutputStream modelStream) { 

    // .... 
    try { 
     ObjectStream<String> lineStream = new PlainTextByLineStream(trainingDataInputStreamFactory(Arrays.asList(
       new File("trainingdata1.txt"), 
       new File("trainingdata2.txt"), 
       new File("trainingdata3.txt") 
     )), charset); 

     // ... 
    } 

    // ... 
} 

private InputStreamFactory trainingDataInputStreamFactory(List<File> trainingFiles) { 
    return new InputStreamFactory() { 
     @Override 
     public InputStream createInputStream() throws IOException { 
      List<InputStream> inputStreams = trainingFiles.stream() 
        .map(f -> { 
         try { 
          return new FileInputStream(f); 
         } catch (FileNotFoundException e) { 
          e.printStackTrace(); 
          return null; 
         } 
        }) 
        .filter(Objects::nonNull) 
        .collect(Collectors.toList()); 

      return new SequenceInputStream(new Vector<>(inputStreams).elements()); 
     } 
    }; 
} 
+0

おかげ@Schrieveslaach – Patrick

+1

@Patrickを使用するように変更例:私はあなたが注釈付きコーパスからのNLPモデルを作成するのに役立つツールセットを開発しています。こちらをご覧ください(https://git.noc.fh-aachen.de/marc.schreiber/Towards-Effective-NLP-Application-Development)。ご不明な点がございましたら、お知らせください。 ;-) – Schrieveslaach

+0

ありがとう、私はそれを見ています。@ Schrieveslaach – Patrick

関連する問題