Skip to content

Commit

Permalink
trying but no finisehd
Browse files Browse the repository at this point in the history
  • Loading branch information
yanyiwu committed Sep 3, 2016
1 parent 0ad4710 commit 73e13a3
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 0 deletions.
27 changes: 27 additions & 0 deletions extractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ static char** ConvertWords(const std::vector<std::string>& words) {
return res;
}

static struct CWordWeight* ConvertWords(const std::vector<std::pair<std::string, double> >& words) {
struct CWordWeight* res = (struct CWordWeight*)malloc(sizeof(struct CWordWeight) * (words.size() + 1));
for (size_t i = 0; i < words.size(); i++) {
res[i].word = (char*)malloc(sizeof(char) * (words[i].first.length() + 1));
strcpy(res[i].word, words[i].first.c_str());
res[i].weight = words[i].second;
}
res[words.size()].word = NULL;
return res;
}

Extractor NewExtractor(const char* dict_path, const char* hmm_path, const char* user_dict, const char* idf_path, const char* stop_word_path) {
return (Extractor)(new cppjieba::KeywordExtractor(dict_path, hmm_path, idf_path, stop_word_path, user_dict));
Expand All @@ -23,6 +33,23 @@ void FreeExtractor(Extractor x) {
delete (cppjieba::KeywordExtractor*)x;
}

struct CWordWeight* ExtractWithWeight(Extractor handle, const char* sentence, int top_k) {
std::vector<std::pair<std::string, double> > words;
((cppjieba::KeywordExtractor*)handle)->Extract(sentence, words, top_k);
struct CWordWeight* res = ConvertWords(words);
return res;
}

void FreeWordWeights(struct CWordWeight* wws) {
struct CWordWeight* x = wws;
while (x && x->word) {
free(x->word);
x->word = NULL;
x++;
}
free(wws);
}

char** Extract(Extractor handle, const char* sentence, int top_k) {
std::vector<std::string> words;
((cppjieba::KeywordExtractor*)handle)->Extract(sentence, words, top_k);
Expand Down
16 changes: 16 additions & 0 deletions extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,19 @@ func (x *Extractor) Extract(s string, topk int) []string {
defer C.FreeWords(words)
return res
}

type WordWeight struct {
word string
weight float64
}

func (x *Extractor) ExtractWithWeight(s string, topk int) []WordWeight {
cstr := C.CString(s)
defer C.free(unsafe.Pointer(cstr))
//var words *C.struct_CWordWeight = C.ExtractWithWeight(x.extractor, cstr, C.int(topk))
words := C.ExtractWithWeight(x.extractor, cstr, C.int(topk))
p := unsafe.Pointer(words)
res := cwordweights((*C.struct_CWordWeight)(p))
defer C.FreeWordWeights(words)
return res
}
7 changes: 7 additions & 0 deletions extractor.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ typedef void* Extractor;
Extractor NewExtractor(const char* dict_path, const char* hmm_path, const char* user_dict, const char* idf_path, const char* stop_word_path);
void FreeExtractor(Extractor);

struct CWordWeight {
char* word;
double weight;
};

char** Extract(Extractor handle, const char* sentence, int top_k);
struct CWordWeight* ExtractWithWeight(Extractor handle, const char* sentence, int top_k);
void FreeWordWeights(struct CWordWeight* wws);

#endif // CJIEBA_EXTRACTOR_H
1 change: 1 addition & 0 deletions util.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ void FreeWords(char** words) {
}
free(words);
}

28 changes: 28 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@ func cstrings(x **C.char) []string {
return s
}

func cwordweights(x *C.struct_CWordWeight) []WordWeight {
var s []WordWeight
eltSize := unsafe.Sizeof(*x)
for *x != nil {
ww := WordWeight{
C.GoString(((C.struct_CWordWeight)(*x)).word),
(*x).weight,
}
s = append(s, ww)
x = (*C.struct_CWordWeight)(unsafe.Pointer(uintptr(unsafe.Pointer(x)) + eltSize))
}
return s
}

func convertWords(s string, words *C.Word) []Word {
result := make([]Word, 0)
x := words
Expand All @@ -43,3 +57,17 @@ func convertWords(s string, words *C.Word) []Word {
}
return result
}

//func cwordweights(x unsafe.Pointer) []WordWeight {
// var s []WordWeight
// eltSize := 16
// for (*(*C.char))(x) != nil {
// ww := WordWeight{
// C.GoString((*C.char))(x)),
// (*x).weight,
// }
// s = append(s, ww)
// x = (*C.struct_CWordWeight)(unsafe.Pointer(uintptr(unsafe.Pointer(x)) + eltSize))
// }
// return s
//}

0 comments on commit 73e13a3

Please sign in to comment.