Skip to content

Commit

Permalink
添加一些单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
MaJunhua committed Mar 28, 2017
1 parent 4fb1332 commit 3011d98
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 6 deletions.
12 changes: 8 additions & 4 deletions Makefile
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,28 @@ dst_dir=.
include_dir=include
src_dir=src
bin_dir=.
test_dir=test
thulac=g++ -O3 -march=native -I $(include_dir)

# all: $(bin_dir)/thulac_test $(bin_dir)/train_c $(bin_dir)/thulac
all: $(bin_dir)/thulac $(bin_dir)/train_c
all: $(bin_dir)/thulac $(bin_dir)/train_c $(bin_dir)/thulac_test

$(bin_dir)/thulac: $(src_dir)/thulac.cc $(include_dir)/*.h
$(thulac) $(src_dir)/thulac.cc -o $(bin_dir)/thulac

$(bin_dir)/train_c: $(src_dir)/train_c.cc $(include_dir)/*.h
$(thulac) -o $(bin_dir)/train_c $(src_dir)/train_c.cc

$(bin_dir)/thulac_test: $(src_dir)/thulac_test.cc $(include_dir)/*.h
$(thulac) -o $(bin_dir)/thulac_test $(src_dir)/thulac_test.cc
$(bin_dir)/thulac_test: $(test_dir)/test_case.cpp $(include_dir)/*.h
$(thulac) -o $(bin_dir)/thulac_test $(test_dir)/test_case.cpp

# $(bin_dir)/thulac_test: $(src_dir)/thulac_test.cc $(include_dir)/*.h
# $(thulac) -o $(bin_dir)/thulac_test $(src_dir)/thulac_test.cc

clean:
rm -f $(bin_dir)/thulac
rm -f $(bin_dir)/train_c
rm -f $(bin_dir)/thulac_test
rm -f $(bin_dir)/thulac_test

pack:
tar -czvf THULAC_lite_c++_v1.tar.gz src Makefile doc README.md
18 changes: 16 additions & 2 deletions include/thulac.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class THULAC {
public:
int init(const char* model_path = NULL, const char* user_path = NULL, int just_seg = 0, int t2s = 0, int ufilter = 0, char separator = '_');
void deinit();
int cut(const std::string&, THULAC_result&);
int cut(const std::string&, THULAC_result&);
std::string toString(const THULAC_result&);
THULAC() {
user_specified_dict_name=NULL;
model_path_char=NULL;
Expand Down Expand Up @@ -247,7 +248,9 @@ int THULAC::cut(const std::string &in, THULAC_result& result) {
for(int j = 0; j < tagged.size(); j++) {
ous.str("");
ous << tagged[j].word;
result.push_back(std::make_pair<std::string, std::string>(ous.str(), tagged[j].tag));
std::string s = tagged[j].tag;
// char * ss = s;
result.push_back(std::make_pair<std::string, std::string>(ous.str(), s.c_str()));
}

}
Expand All @@ -269,5 +272,16 @@ int THULAC::cut(const std::string &in, THULAC_result& result) {
}


std::string THULAC::toString(const THULAC_result& result) {
std::string output = "";
for(auto i : result) {
if(i.first == "\n") continue;
if(seg_only) output += i.first + i.second + " ";
else output += i.first + char(separator) + i.second + " ";
}
output.erase(output.size() - 1, 1);
return output;
}


#endif
56 changes: 56 additions & 0 deletions test/test_case.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <iostream>
#include <string>
#include "thulac.h"
#include "thulac_test.h"


int main() {
THULAC lac;
std::string case_name;
{

case_name = "检查单分词模型模型";
lac.init("/program/python/python-git/thulac/models",NULL,1);
THULAC_TEST test = THULAC_TEST(&lac, case_name);
test.testEqual("我爱北京天安门", "我 爱 北京 天安门");
test.testEqual("小明喜欢玩炉石传说", "小明 喜欢 玩 炉石 传说");
}

{
case_name = "检查带词性标注的模型";
lac.init("/program/python/python-git/thulac/models",NULL,0);
THULAC_TEST test = THULAC_TEST(&lac, case_name);
test.testEqual("我爱北京天安门", "我_r 爱_v 北京_ns 天安门_ns");
test.testEqual("小明喜欢玩炉石传说", "小明_np 喜欢_v 玩_v 炉石_n 传说_n");
}

{
case_name = "检查deli分隔符参数";
lac.init("/program/python/python-git/thulac/models",NULL,0, 0, 0,'#');
THULAC_TEST test = THULAC_TEST(&lac, case_name);
test.testEqual("我爱北京天安门", "我#r 爱#v 北京#ns 天安门#ns");
test.testEqual("小明喜欢玩炉石传说", "小明#np 喜欢#v 玩#v 炉石#n 传说#n");
}

{
case_name = "检查T2S分隔符参数";
lac.init("/program/python/python-git/thulac/models",NULL,1, 1);
THULAC_TEST test = THULAC_TEST(&lac, case_name);
test.testEqual("我愛北京天安門", "我 爱 北京 天安门");
test.testEqual("小明喜歡玩爐石傳說", "小明 喜欢 玩 炉石 传说");
}

{
case_name = "检查ufilter参数";
lac.init("/program/python/python-git/thulac/models",NULL,1, 0, 1);
THULAC_TEST test = THULAC_TEST(&lac, case_name);
test.testEqual("我可以爱北京天安门", "我 爱 北京 天安门");
}


THULAC_TEST::reportAll();
return 0;
}



82 changes: 82 additions & 0 deletions test/thulac_test.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include <string>
#include <vector>
#include <iostream>
#include <utility>
#include "../include/thulac.h"
class THULAC_TEST {
public:
void report(const std::string &s);
void testEqual(const std::string &raw, const std::string &standard);
THULAC_TEST(THULAC *lac);
THULAC_TEST(THULAC *lac, std::string name);
static void reportAll();
private:
std::vector<std::string> errorMsg;
THULAC *lac;
std::string caseName;
bool equals(const std::string &result, const std::string &standard);
void report_error(const std::string &e);
static std::vector<std::pair<std::string, std::string> > allErrorMsg;

};

THULAC_TEST::THULAC_TEST(THULAC *lac) {
this->lac = lac;


}

THULAC_TEST::THULAC_TEST(THULAC *lac, std::string caseName) {
this->lac = lac;
this->caseName = caseName;
}

bool THULAC_TEST::equals(const std::string &result, const std::string &standard) {
return result == standard;
}

void THULAC_TEST::report_error(const std::string &e) {
errorMsg.push_back(e);
allErrorMsg.push_back(std::make_pair(caseName, e));
}

void THULAC_TEST::testEqual(const std::string &raw, const std::string &standard) {
THULAC_result result;
lac->cut(raw, result);
std::string s_result = lac->toString(result);
if(equals(s_result, standard)) {
std::cout << ".";
return;
}
std::cout << "E";
std::string error = "不匹配:" + s_result + "" + standard;
report_error(error);
return;
}

void THULAC_TEST::report(const std::string &s) {
std::cout << std::endl << s << ":" << std::endl;
if(errorMsg.size() == 0) {
std::cout << "恭喜!所有case通过测试" << std::endl;
}
else{
for(auto i : errorMsg) {

std::cout << i << std::endl;
}

}
return;
}

void THULAC_TEST::reportAll() {
std::cout << "\n测试结果:\n";
if(allErrorMsg.size() == 0) std::cout << "恭喜!所有case通过测试" << std::endl;
else {
for (auto i : allErrorMsg) {
std::cout << i.first << "case中:\n" << i.second << std::endl;
}
}
}

std::vector<std::pair<std::string, std::string> > THULAC_TEST::allErrorMsg = std::vector<std::pair<std::string, std::string> >();

0 comments on commit 3011d98

Please sign in to comment.