Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.x #1579

Open
wants to merge 3 commits into
base: 1.x
Choose a base branch
from
Open

1.x #1579

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/main/java/com/hankcs/hanlp/model/crf/CRFSegmenter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/hankcs/hanlp/model/crf/CRFTagger.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

/**
* 训练
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/hankcs/hanlp/model/crf/LogLinearModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand Down Expand Up @@ -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
{
Expand Down