OSDN Git Service

openJtalk結合
[nlite/nlite.git] / open_jtalk_lib / open_jtalk / mecab / src / context_id.cpp
1 // MeCab -- Yet Another Part-of-Speech and Morphological Analyzer
2 //
3 //
4 //  Copyright(C) 2001-2006 Taku Kudo <taku@chasen.org>
5 //  Copyright(C) 2004-2006 Nippon Telegraph and Telephone Corporation
6 #include <fstream>
7 #include "context_id.h"
8 #include "iconv_utils.h"
9 #include "utils.h"
10
11 namespace {
12
13 using namespace MeCab;
14
15 bool open_map(const char *filename,
16               std::map<std::string, int> *cmap,
17               Iconv *iconv) {
18   std::ifstream ifs(filename);
19   CHECK_DIE(ifs) << "no such file or directory: " << filename;
20   cmap->clear();
21   char *col[2];
22   std::string line;
23   while (std::getline(ifs, line)) {
24     CHECK_DIE(2 == tokenize2(const_cast<char *>(line.c_str()),
25                              " \t", col, 2))
26         << "format error: " << line;
27     std::string pos = col[1];
28     if (iconv) {
29       iconv->convert(&pos);
30     }
31     cmap->insert(std::make_pair<std::string, int>
32                  (pos, std::atoi(col[0])));
33   }
34   return true;
35 }
36
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();
41        it != cmap->end();
42        ++it) it->second = id++;
43   cmap->insert(std::make_pair(bos, 0));
44   return true;
45 }
46
47 bool save(const char* filename,
48           std::map<std::string, int> *cmap) {
49   std::ofstream ofs(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;
54   }
55   return true;
56 }
57 }
58
59 namespace MeCab {
60
61 void ContextID::clear() {
62   left_.clear();
63   right_.clear();
64   left_bos_.clear();
65   right_bos_.clear();
66 }
67
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));
71 }
72
73 void ContextID::addBOS(const char *l, const char *r) {
74   left_bos_ = l;
75   right_bos_ = r;
76 }
77
78 bool ContextID::save(const char* lfile,
79                      const char* rfile) {
80   return (::save(lfile, &left_) && ::save(rfile, &right_));
81 }
82
83 bool ContextID::open(const char *lfile,
84                      const char *rfile,
85                      Iconv *iconv) {
86   return (::open_map(lfile, &left_, iconv) &&
87           ::open_map(rfile, &right_, iconv));
88 }
89
90 bool ContextID::build() {
91   return (::build(&left_, left_bos_) && ::build(&right_, right_bos_));
92 }
93
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;
98   return it->second;
99 }
100
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;
105   return it->second;
106 }
107 }