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

Neoltp4j support customized model #27

Open
wants to merge 2 commits into
base: neoltp4j
Choose a base branch
from
Open
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
42 changes: 39 additions & 3 deletions src/main/c++/edu_hit_ir_ltp4j_Segmentor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
#include <vector>

static void* segmentor = NULL;
static bool using_customized_model = false;

inline void release_segmentor(){
if(using_customized_model)
customized_segmentor_release_segmentor(segmentor);
else
segmentor_release_segmentor(segmentor);
using_customized_model = false;
}

JNIEXPORT jint JNICALL Java_edu_hit_ir_ltp4j_Segmentor_create__Ljava_lang_String_2
(JNIEnv* env, jobject obj, jstring model_path) {
Expand All @@ -14,13 +23,14 @@ JNIEXPORT jint JNICALL Java_edu_hit_ir_ltp4j_Segmentor_create__Ljava_lang_String
if(!segmentor){
segmentor = segmentor_create_segmentor(str);
} else{
segmentor_release_segmentor(segmentor);
release_segmentor();
segmentor = segmentor_create_segmentor(str);
}

env->ReleaseStringUTFChars( model_path, str);

if(segmentor) {
using_customized_model = false;
return 1;
}

Expand All @@ -36,14 +46,40 @@ JNIEXPORT jint JNICALL Java_edu_hit_ir_ltp4j_Segmentor_create__Ljava_lang_String
if(!segmentor){
segmentor = segmentor_create_segmentor(str_model,str_lexicon);
} else{
segmentor_release_segmentor(segmentor);
release_segmentor();
segmentor = segmentor_create_segmentor(str_model,str_lexicon);
}

env->ReleaseStringUTFChars( model_path, str_model);
env->ReleaseStringUTFChars( lexicon_path, str_lexicon);

if(segmentor) {
using_customized_model = false;
return 1;
}
return -1;
}

JNIEXPORT jint JNICALL Java_edu_hit_ir_ltp4j_Segmentor_create__Ljava_lang_String_2Ljava_lang_String_2Ljava_lang_String_2
(JNIEnv* env, jobject obj, jstring baseline_model_path, jstring customized_model_path, jstring lexicon_path) {

const char* str_baseline_model = env->GetStringUTFChars( baseline_model_path, 0);
const char* str_customized_model = env->GetStringUTFChars( customized_model_path, 0);
const char* str_lexicon = env->GetStringUTFChars( lexicon_path , 0);

if(!segmentor){
segmentor = customized_segmentor_create_segmentor(str_baseline_model, str_customized_model, str_lexicon);
} else{
release_segmentor();
segmentor = customized_segmentor_create_segmentor(str_baseline_model, str_customized_model, str_lexicon);
}

env->ReleaseStringUTFChars( baseline_model_path, str_baseline_model);
env->ReleaseStringUTFChars( customized_model_path, str_customized_model);
env->ReleaseStringUTFChars( lexicon_path, str_lexicon);

if(segmentor) {
using_customized_model = true;
return 1;
}
return -1;
Expand Down Expand Up @@ -71,7 +107,7 @@ JNIEXPORT jint JNICALL Java_edu_hit_ir_ltp4j_Segmentor_segment

JNIEXPORT void JNICALL Java_edu_hit_ir_ltp4j_Segmentor_release
(JNIEnv* env, jobject obj) {
segmentor_release_segmentor(segmentor);
release_segmentor();
segmentor = NULL;
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/edu/hit/ir/ltp4j/Segmentor.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class Segmentor {

public final native int create(String modelPath);
public final native int create(String modelPath, String lexiconPath);
public final native int create(String baselineModelPath, String customizedModelPath, String lexiconPath);
public final native int segment(String sent, List<String> words);
public final native void release();
}
Expand Down