Skip to content

Commit

Permalink
tidy the example code
Browse files Browse the repository at this point in the history
  • Loading branch information
Oneplus committed Jan 15, 2014
1 parent a8294c6 commit 6bb4791
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 164 deletions.
54 changes: 27 additions & 27 deletions examples/cws.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,38 @@
#include "segment_dll.h"

int main(int argc, char * argv[]) {
if (argc < 2) {
std::cerr << "cws [model path] [lexicon_file]" << std::endl;
return 1;
}
if (argc < 2) {
std::cerr << "cws [model path] [lexicon_file]" << std::endl;
return 1;
}

void * engine = 0;
if (argc == 2) {
engine = segmentor_create_segmentor(argv[1]);
} else if (argc == 3) {
engine = segmentor_create_segmentor(argv[1], argv[2]);
}
void * engine = 0;
if (argc == 2) {
engine = segmentor_create_segmentor(argv[1]);
} else if (argc == 3) {
engine = segmentor_create_segmentor(argv[1], argv[2]);
}

if (!engine) {
return -1;
}
std::vector<std::string> words;
if (!engine) {
return -1;
}
std::vector<std::string> words;

const char * suite[2] = {
"What's wrong with you? 别灰心! http://t.cn/zQz0Rn",
"台北真的是天子骄子吗?",};
const char * suite[2] = {
"What's wrong with you? 别灰心! http://t.cn/zQz0Rn",
"台北真的是天子骄子吗?",};

for (int i = 0; i < 2; ++ i) {
words.clear();
int len = segmentor_segment(engine, suite[i], words);
for (int i = 0; i < len; ++ i) {
std::cout << words[i];
if (i+1 == len) std::cout <<std::endl;
else std::cout<< "|";
}
for (int i = 0; i < 2; ++ i) {
words.clear();
int len = segmentor_segment(engine, suite[i], words);
for (int i = 0; i < len; ++ i) {
std::cout << words[i];
if (i+1 == len) std::cout <<std::endl;
else std::cout<< "|";
}
}

segmentor_release_segmentor(engine);
return 0;
segmentor_release_segmentor(engine);
return 0;
}

72 changes: 36 additions & 36 deletions examples/cws_cmdline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,52 +19,52 @@
#include "segment_dll.h"

double get_time(void) {
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec + (tv.tv_usec / 1000000.0);
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec + (tv.tv_usec / 1000000.0);
}

int main(int argc, char * argv[]) {
if (argc < 2) {
std::cerr << "cws [model path] [lexicon_file]" << std::endl;
return 1;
}
if (argc < 2) {
std::cerr << "cws [model path] [lexicon_file]" << std::endl;
return 1;
}

void * engine = 0;
if (argc == 2) {
engine = segmentor_create_segmentor(argv[1]);
} else if (argc == 3) {
engine = segmentor_create_segmentor(argv[1], argv[2]);
}
void * engine = 0;
if (argc == 2) {
engine = segmentor_create_segmentor(argv[1]);
} else if (argc == 3) {
engine = segmentor_create_segmentor(argv[1], argv[2]);
}

if (!engine) {
return -1;
}
std::vector<std::string> words;
std::string sentence;
if (!engine) {
return -1;
}
std::vector<std::string> words;
std::string sentence;

std::cerr << "TRACE: Model is loaded" << std::endl;
double tm = get_time();
std::cerr << "TRACE: Model is loaded" << std::endl;
double tm = get_time();

while (std::getline(std::cin, sentence, '\n')) {
words.clear();
if (sentence.size() == 0) { continue; }
int len = segmentor_segment(engine, sentence, words);
for (int i = 0; i < len; ++ i) {
std::cout << words[i];
if (i+1 == len) std::cout <<std::endl;
else std::cout<< "|";
}
while (std::getline(std::cin, sentence, '\n')) {
words.clear();
if (sentence.size() == 0) { continue; }
int len = segmentor_segment(engine, sentence, words);
for (int i = 0; i < len; ++ i) {
std::cout << words[i];
if (i+1 == len) std::cout <<std::endl;
else std::cout<< "|";
}
}

segmentor_release_segmentor(engine);
segmentor_release_segmentor(engine);

tm = get_time() - tm;
std::cerr << "TRACE: consume "
<< tm
<< " seconds."
<< std::endl;
tm = get_time() - tm;
std::cerr << "TRACE: consume "
<< tm
<< " seconds."
<< std::endl;

return 0;
return 0;
}

5 changes: 4 additions & 1 deletion examples/multi_cws_cmdline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* is not compilable under MSVC
*/
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <vector>
Expand Down Expand Up @@ -91,7 +92,9 @@ void multithreaded_segment( void * args) {

int main(int argc, char ** argv) {
if (argc < 2 || (0 == strcmp(argv[1], "-h"))) {
std::cerr << "Example: ./multi_cws_cmdline [model path] [lexicon file]=NULL threadnum" << std::endl;
std::cerr << "Example: ./multi_cws_cmdline "
<< "[model path] [lexicon file](optional) threadnum"
<< std::endl;
std::cerr << std::endl;
std::cerr << "This program recieve input word sequence from stdin." << std::endl;
std::cerr << "One sentence per line." << std::endl;
Expand Down
70 changes: 35 additions & 35 deletions examples/ner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,40 @@
#include "ner_dll.h"

int main(int argc, char * argv[]) {
if (argc < 2) {
std::cerr << "usage: ./ner [model_path]" << std::endl;
return -1;
}

void * engine = ner_create_recognizer(argv[1]);
if (!engine) {
std::cerr << "failed to load model" << std::endl;
return -1;
}

std::vector<std::string> words;
std::vector<std::string> postags;

words.push_back("中国"); postags.push_back("ns");
words.push_back("国际"); postags.push_back("n");
words.push_back("广播"); postags.push_back("n");
words.push_back("电台"); postags.push_back("n");
words.push_back("创办"); postags.push_back("v");
words.push_back(""); postags.push_back("p");
words.push_back("1941年"); postags.push_back("m");
words.push_back("12月"); postags.push_back("m");
words.push_back("3日"); postags.push_back("m");
words.push_back(""); postags.push_back("wp");

std::vector<std::string> tags;

ner_recognize(engine, words, postags, tags);

for (int i = 0; i < tags.size(); ++ i) {
std::cout << words[i] << "\t" << postags[i] << "\t" << tags[i] << std::endl;
}

ner_release_recognizer(engine);
return 0;
if (argc < 2) {
std::cerr << "usage: ./ner [model_path]" << std::endl;
return -1;
}

void * engine = ner_create_recognizer(argv[1]);
if (!engine) {
std::cerr << "failed to load model" << std::endl;
return -1;
}

std::vector<std::string> words;
std::vector<std::string> postags;

words.push_back("中国"); postags.push_back("ns");
words.push_back("国际"); postags.push_back("n");
words.push_back("广播"); postags.push_back("n");
words.push_back("电台"); postags.push_back("n");
words.push_back("创办"); postags.push_back("v");
words.push_back(""); postags.push_back("p");
words.push_back("1941年"); postags.push_back("m");
words.push_back("12月"); postags.push_back("m");
words.push_back("3日"); postags.push_back("m");
words.push_back(""); postags.push_back("wp");

std::vector<std::string> tags;

ner_recognize(engine, words, postags, tags);

for (int i = 0; i < tags.size(); ++ i) {
std::cout << words[i] << "\t" << postags[i] << "\t" << tags[i] << std::endl;
}

ner_release_recognizer(engine);
return 0;
}

44 changes: 22 additions & 22 deletions examples/par.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,34 @@
#include "parser_dll.h"

int main(int argc, char * argv[]) {
if (argc < 2) {
return -1;
}
if (argc < 2) {
return -1;
}

void * engine = parser_create_parser(argv[1]);
if (!engine) {
return -1;
}
void * engine = parser_create_parser(argv[1]);
if (!engine) {
return -1;
}

std::vector<std::string> words;
std::vector<std::string> postags;
std::vector<std::string> words;
std::vector<std::string> postags;

words.push_back("一把手"); postags.push_back("n");
words.push_back("亲自"); postags.push_back("d");
words.push_back("过问"); postags.push_back("v");
words.push_back(""); postags.push_back("wp");
words.push_back("一把手"); postags.push_back("n");
words.push_back("亲自"); postags.push_back("d");
words.push_back("过问"); postags.push_back("v");
words.push_back(""); postags.push_back("wp");

std::vector<int> heads;
std::vector<std::string> deprels;
std::vector<int> heads;
std::vector<std::string> deprels;

parser_parse(engine, words, postags, heads, deprels);
parser_parse(engine, words, postags, heads, deprels);

for (int i = 0; i < heads.size(); ++ i) {
std::cout << words[i] << "\t" << postags[i] << "\t"
<< heads[i] << "\t" << deprels[i] << std::endl;
}
for (int i = 0; i < heads.size(); ++ i) {
std::cout << words[i] << "\t" << postags[i] << "\t"
<< heads[i] << "\t" << deprels[i] << std::endl;
}

parser_release_parser(engine);
return 0;
parser_release_parser(engine);
return 0;
}

Loading

0 comments on commit 6bb4791

Please sign in to comment.