diff --git a/src/main/java/com/hankcs/hanlp/model/crf/CRFSegmenter.java b/src/main/java/com/hankcs/hanlp/model/crf/CRFSegmenter.java index 353ac0c56..e53a3d82d 100644 --- a/src/main/java/com/hankcs/hanlp/model/crf/CRFSegmenter.java +++ b/src/main/java/com/hankcs/hanlp/model/crf/CRFSegmenter.java @@ -23,6 +23,7 @@ import java.io.BufferedWriter; import java.io.IOException; +import java.io.InputStream; import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedList; @@ -50,6 +51,11 @@ public CRFSegmenter(String modelPath) throws IOException perceptronSegmenter = new PerceptronSegmenter(this.model); } } + + public CRFSegmenter(InputStream inputStream) throws IOException { + super(inputStream); + perceptronSegmenter = new PerceptronSegmenter(this.model); + } @Override protected void convertCorpus(Sentence sentence, BufferedWriter bw) throws IOException diff --git a/src/main/java/com/hankcs/hanlp/model/crf/CRFTagger.java b/src/main/java/com/hankcs/hanlp/model/crf/CRFTagger.java index a5506caf7..63483f3db 100644 --- a/src/main/java/com/hankcs/hanlp/model/crf/CRFTagger.java +++ b/src/main/java/com/hankcs/hanlp/model/crf/CRFTagger.java @@ -20,6 +20,7 @@ import com.hankcs.hanlp.model.perceptron.utility.Utility; import java.io.BufferedWriter; +import java.io.InputStream; import java.io.File; import java.io.IOException; import java.util.Date; @@ -40,6 +41,10 @@ public CRFTagger(String modelPath) throws IOException if (modelPath == null) return; // 训练模式 model = new LogLinearModel(modelPath); } + + public CRFTagger(InputStream is) throws IOException { + model = new LogLinearModel(is); + } /** * 训练 diff --git a/src/main/java/com/hankcs/hanlp/model/crf/LogLinearModel.java b/src/main/java/com/hankcs/hanlp/model/crf/LogLinearModel.java index bcca2ce7e..21d3fdeda 100644 --- a/src/main/java/com/hankcs/hanlp/model/crf/LogLinearModel.java +++ b/src/main/java/com/hankcs/hanlp/model/crf/LogLinearModel.java @@ -24,6 +24,7 @@ import com.hankcs.hanlp.utility.Predefine; import java.io.DataOutputStream; +import java.io.InputStream; import java.io.IOException; import java.util.*; @@ -113,6 +114,13 @@ public LogLinearModel(String txtFile, String binFile) throws IOException super(null, null); convert(txtFile, binFile); } + + public LogLinearModel(InputStream is) throws IOException + { + super(null, null); + ByteArrayStream byteArray = ByteArrayOtherStream.createByteArrayOtherStream(inputStream); + load(byteArray); + } private void convert(String txtFile, String binFile) throws IOException {