cjieba
cjieba copied to clipboard
在jieba.cpp中增加了一个函数,运行观察到内存一直增长,不知是否有造成内存泄漏
代码如下,主要是调用了x->extractor.Extract,不知这种写法是否妥当。 ` CJiebaWord* ExtractWithoutTags(Jieba handle, const char* sentence, size_t len, const char* tagname, size_t topn) { cppjieba::Jieba* x = (cppjieba::Jieba*)handle; vectorcppjieba::KeywordExtractor::Word words; string wordtag, tagstring; tagstring = tagname; tagstring = tagstring!=""? "," + tagstring + ",":""; x->extractor.Extract(sentence, words, topn5); CJiebaWord res = (CJiebaWord*)malloc(sizeof(CJiebaWord) * (words.size() + 1)); size_t i, j = 0; for (i = 0, j = 0; i < words.size(); i++) { if (j == topn) { break; } assert(words[i].offsets.size() > 0); size_t offset = words[i].offsets[0]; assert(offset < len); if (tagstring != "") { wordtag = x->LookupTag(words[i].word); wordtag = "," + wordtag + ","; if (tagstring.find(wordtag) == string::npos) {
res[j].word = sentence + offset;
res[j].len = words[i].word.size();
j++;
}
}
else {
res[j].word = sentence + offset;
res[j].len = words[i].word.size();
j++;
}
}
res[j].word = NULL;
res[j].len = 0;
return res;
}
`