My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
Samples  
Bamboo Samples Show
Featured
Updated Feb 4, 2010 by ifen...@gmail.com

分词Sample

#include <iostream>
#include <vector>
#include <cstdlib>
#include "bamboo.hxx"
using namespace bamboo;
int main()
{
        void *handle = bamboo_init("crf_seg","/opt/bamboo/etc/crf_seg.conf");
        char *t = NULL;
        const char * text = "我爱北京天安门";
        bamboo_setopt(handle, BAMBOO_OPTION_TEXT, const_cast<char *>(text));
        t = bamboo_parse(handle);
        printf("==> %s\n", t);
        free(t);
        bamboo_clean(handle);
        return 0;
}
<?php
  bamboo_setopt("crf_seg", BAMBOO_OPTION_TEXT, "我爱北京天安门");
  echo bamboo_parse("crf_seg")."\n";
?>

词性标注Sample

#include <iostream>
#include <vector>
#include <cstdlib>
#include "bamboo.hxx"
using namespace bamboo;
int main()
{
        void *handle = bamboo_init("crf_pos","/opt/bamboo/etc/crf_pos.conf");
        char *t = NULL;
        const char * text = "我爱北京天安门";
        bamboo_setopt(handle, BAMBOO_OPTION_TEXT, const_cast<char *>(text));
        t = bamboo_parse(handle);
        printf("==> %s\n", t);
        free(t);
        bamboo_clean(handle);
        return 0;
}

命名实体识别Sample

#include <iostream>
#include <fstream>
#include <vector>
#include <cstdlib>
#include <unistd.h>
#include "bamboo.hxx"
using namespace bamboo;
#define USAGE "regress_ner -t nr|ns|nt [-f input_file]"
int main(int argc, char * argv[])
{
        const char * ner_type = NULL, * input_file = NULL;
        int opt;
        while( (opt=getopt(argc, argv, "t:f:")) != -1) {
                switch(opt) {
                case 't':
                        ner_type = optarg;
                        break;
                case 'f':
                        input_file = optarg;
                        break;
                case '?':
                        printf("%s\n", USAGE);
                        exit(1);
                }
        }
        if(!ner_type) {
                printf("%s\n", USAGE);
                exit(1);
        }
        std::istream * is;
        std::ifstream fin;
        if(!input_file) {
                is = & std::cin;
        } else {
                fin.open(input_file);
                is = & fin;
        }
        std::string text = "";
        while(!is->eof()) {
                std::string line;
                getline(*is, line);
                text += line + "\n";
        }
        if(input_file) {
                fin.close();
        }
        std::vector<Token *> v;
        std::vector<Token *>::iterator it;
        char parser_name[16];
        sprintf(parser_name, "crf_ner_%s", ner_type);
        ParserFactory *factory = ParserFactory::get_instance();
        Parser *p = factory->create(parser_name);
        p->setopt(BAMBOO_OPTION_TEXT, const_cast<char *>(text.c_str()));
        p->parse(v);
        for (it = v.begin(); it != v.end(); it++) {
                std::cout << (*it)->get_orig_token() << std::endl;
                delete *it;
        }
        delete p;
        return 0;
}
<?php
  bamboo_setopt("crf_ner_nr", BAMBOO_OPTION_TEXT, "姚明和刘翔");
  echo bamboo_parse("crf_ner_nr")."\n";
  bamboo_setopt("crf_ner_ns", BAMBOO_OPTION_TEXT, "我爱北京天安门");
  echo bamboo_parse("crf_ner_ns")."\n";
  bamboo_setopt("crf_ner_nt", BAMBOO_OPTION_TEXT, "北京公安局破获重大案件");
  echo bamboo_parse("crf_ner_nt")."\n";
?>

主题词抽取Sample

#include <iostream>
#include <fstream>
#include <vector>
#include <cstdlib>
#include <unistd.h>
#include "bamboo.hxx"
using namespace bamboo;
#define USAGE "regress_ner -f input_file"
int main(int argc, char * argv[])
{
        const char * input_file = NULL;
        std::istream * is;
        std::ifstream fin;
        int opt;
        while( (opt=getopt(argc, argv, "f:")) != -1) {
                switch(opt) {
                case 'f':
                        input_file = optarg;
                        break;
                case '?':
                        printf("%s\n", USAGE);
                        exit(1);
                }
        }
        if(!input_file) {
                is = & std::cin;
        } else {
                fin.open(input_file);
                is = & fin;
        }
        std::string title = "", text = "";
        size_t i = 0;
        while(!is->eof()) {
                std::string line;
                getline(*is, line);
                if(i == 0) {
                        title = line;
                }
                else {
                        text += line + "\n";
                }
                ++i;
        }
        if(input_file) {
                fin.close();
        }
        std::vector<Token *> v;
        std::vector<Token *>::iterator it;
        ParserFactory *factory = ParserFactory::get_instance();
        Parser *p = factory->create("keyword");
        p->setopt(BAMBOO_OPTION_TITLE, const_cast<char *>(title.c_str()));
        p->setopt(BAMBOO_OPTION_TEXT, const_cast<char *>(text.c_str()));
        p->parse(v);
        for (it = v.begin(); it != v.end(); it++) {
                std::cout << (*it)->get_orig_token() << std::endl;
                delete *it;
        }
        delete p;
        return 0;
}
Comment by wjange...@gmail.com, Jun 24, 2009

我爱五星红旗

Comment by teloon198888, May 18, 2010

你好,安装完测试第一个分词的测试代码是遇到如下的错误,请问如何解决?(在/usr/include目录下有bamboo目录,其中有 bamboo_defs.h bamboo.hxx parser_factory.hxx parser.hxx token.hxx)

test.cpp:4:22: error: bamboo.hxx: No such file or directory 
test.cpp:5: error: ‘bamboo’ is not a namespace-name 
test.cpp:5: error: expected namespace-name before ‘;’ token 
test.cpp: In function ‘int main()’: 
test.cpp:8: error: ‘bamboo_init’ was not declared in this scope
test.cpp:11: error: ‘BAMBOO_OPTION_TEXT’ was not declared in this scope test.cpp:11: error: ‘bamboo_setopt’ was not declared in this scope 
test.cpp:12: error: ‘bamboo_parse’ was not declared in this scope 
test.cpp:15: error: ‘bamboo_clean’ was not declared in this scope

Sign in to add a comment
Powered by Google Project Hosting