Skip to content

Commit

Permalink
init Extractor in go
Browse files Browse the repository at this point in the history
  • Loading branch information
yanyiwu committed Jan 13, 2016
1 parent 0f9fc60 commit a4d8e9e
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 17 deletions.
24 changes: 20 additions & 4 deletions extractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,30 @@ extern "C" {
}

#include "cppjieba/KeywordExtractor.hpp"
#include "cppjieba/Jieba.hpp"

Extractor NewExtractor(const Jieba j, const char* idf_path, const char* stop_word_path) {
const cppjieba::Jieba* x = (const cppjieba::Jieba*)j;
return (Extractor)(new cppjieba::KeywordExtractor(x->GetDictTrie(), x->GetHMMModel(), idf_path, stop_word_path));
static char** ConvertWords(const std::vector<std::string>& words) {
char ** res = (char**)malloc(sizeof(char*) * (words.size() + 1));
for (size_t i = 0; i < words.size(); i++) {
res[i] = (char*)malloc(sizeof(char) * (words[i].length() + 1));
strcpy(res[i], words[i].c_str());
}
res[words.size()] = NULL;
return res;
}


Extractor NewExtractor(const Jieba handle, const char* idf_path, const char* stop_word_path) {
const cppjieba::Jieba* x = (const cppjieba::Jieba*)handle;
return (Extractor)(new cppjieba::KeywordExtractor(*x, idf_path, stop_word_path));
}

void FreeExtractor(Extractor x) {
delete (cppjieba::KeywordExtractor*)x;
}

char** Extract(Extractor handle, const char* sentence, int top_k) {
std::vector<std::string> words;
((cppjieba::KeywordExtractor*)handle)->Extract(sentence, words, top_k);
char** res = ConvertWords(words);
return res;
}
8 changes: 8 additions & 0 deletions extractor.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
package gojieba

/*
#cgo CXXFLAGS: -I./deps -DLOGGING_LEVEL=WARNING -O3 -Wall
#include <stdlib.h>
#include "extractor.h"
*/
import "C"

type Extractor struct {
extractor C.Extractor
}
4 changes: 3 additions & 1 deletion extractor.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

typedef void* Extractor;

Extractor NewExtractor(const Jieba j, const char* idf_path, const char* stop_word_path);
Extractor NewExtractor(const Jieba handle, const char* idf_path, const char* stop_word_path);
void FreeExtractor(Extractor);

char** Extract(Extractor handle, const char* sentence, int top_k);

#endif // CJIEBA_EXTRACTOR_H
9 changes: 0 additions & 9 deletions jieba.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,3 @@ char** CutForSearch(Jieba x, const char* sentence, int is_hmm_used) {
return res;
}

void FreeWords(char** words) {
char** x = words;
while (x && *x) {
free(*x);
*x = NULL;
x ++;
}
free(words);
}
6 changes: 3 additions & 3 deletions jieba.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef CJIEBA_JIEBA_H
#define CJIEBA_JIEBA_H

#include "util.h"

typedef void* Jieba;

Jieba NewJieba(const char* dict_path, const char* hmm_path, const char* user_dict);
Expand All @@ -10,6 +12,4 @@ char** Cut(Jieba handle, const char* sentence, int is_hmm_used);
char** CutAll(Jieba handle, const char* sentence);
char** CutForSearch(Jieba handle, const char* sentence, int is_hmm_used);

void FreeWords(char** words);

#endif
#endif // CJIEBA_JIEBA_H
12 changes: 12 additions & 0 deletions util.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "util.h"
#include <stdlib.h>

void FreeWords(char** words) {
char** x = words;
while (x && *x) {
free(*x);
*x = NULL;
x ++;
}
free(words);
}
6 changes: 6 additions & 0 deletions util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef CJIEBA_UTIL_H
#define CJIEBA_UTIL_H

void FreeWords(char** words);

#endif // CJIEBA_UTIL_H

0 comments on commit a4d8e9e

Please sign in to comment.