1 // MeCab -- Yet Another Part-of-Speech and Morphological Analyzer
4 // Copyright(C) 2001-2006 Taku Kudo <taku@chasen.org>
5 // Copyright(C) 2004-2006 Nippon Telegraph and Telephone Corporation
7 #include "context_id.h"
8 #include "iconv_utils.h"
13 using namespace MeCab;
15 bool open_map(const char *filename,
16 std::map<std::string, int> *cmap,
18 std::ifstream ifs(WPATH(filename));
19 CHECK_DIE(ifs) << "no such file or directory: " << filename;
23 while (std::getline(ifs, line)) {
24 CHECK_DIE(2 == tokenize2(const_cast<char *>(line.c_str()),
26 << "format error: " << line;
27 std::string pos = col[1];
31 cmap->insert(std::pair<std::string, int>
32 (pos, std::atoi(col[0])));
37 bool build(std::map<std::string, int> *cmap,
38 const std::string &bos) {
39 int id = 1; // for BOS/EOS
40 for (std::map<std::string, int>::iterator it = cmap->begin();
42 ++it) it->second = id++;
43 cmap->insert(std::make_pair(bos, 0));
47 bool save(const char* filename,
48 std::map<std::string, int> *cmap) {
49 std::ofstream ofs(WPATH(filename));
50 CHECK_DIE(ofs) << "permission denied: " << filename;
51 for (std::map<std::string, int>::const_iterator it = cmap->begin();
52 it != cmap->end(); ++it) {
53 ofs << it->second << " " << it->first << std::endl;
61 void ContextID::clear() {
68 void ContextID::add(const char *l, const char *r) {
69 left_.insert(std::make_pair(std::string(l), 1));
70 right_.insert(std::make_pair(std::string(r), 1));
73 void ContextID::addBOS(const char *l, const char *r) {
78 bool ContextID::save(const char* lfile,
80 return (::save(lfile, &left_) && ::save(rfile, &right_));
83 bool ContextID::open(const char *lfile,
86 return (::open_map(lfile, &left_, iconv) &&
87 ::open_map(rfile, &right_, iconv));
90 bool ContextID::build() {
91 return (::build(&left_, left_bos_) && ::build(&right_, right_bos_));
94 int ContextID::lid(const char *l) const {
95 std::map<std::string, int>::const_iterator it = left_.find(l);
96 CHECK_DIE(it != left_.end())
97 << "cannot find LEFT-ID for " << l;
101 int ContextID::rid(const char *r) const {
102 std::map<std::string, int>::const_iterator it = right_.find(r);
103 CHECK_DIE(it != right_.end())
104 << "cannot find RIGHT-ID for " << r;