~ubuntu-branches/ubuntu/oneiric/mozc/oneiric

« back to all changes in this revision

Viewing changes to converter/connector.cc

  • Committer: Bazaar Package Importer
  • Author(s): Nobuhiro Iwamatsu
  • Date: 2010-07-14 03:26:47 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100714032647-13qjisj6m8cm8jdx
Tags: 0.12.410.102-1
* New upstream release (Closes: #588971).
  - Add mozc-server, mozc-utils-gui and scim-mozc packages.
* Update debian/rules.
  Add --gypdir option to build_mozc.py.
* Update debian/control.
  - Bumped standards-version to 3.9.0.
  - Update description.
* Add mozc icon (Closes: #588972).
* Add patch which revises issue 18.
  ibus_mozc_issue18.patch
* kFreeBSD build support.
  support_kfreebsd.patch

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
28
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
29
 
30
 
#include <algorithm>
31
 
#include <string>
32
 
#include <vector>
33
30
#include "base/base.h"
34
 
#include "base/file_stream.h"
35
 
#include "base/util.h"
36
 
#include "converter/connector.h"
37
 
#include "storage/sparse_array_image.h"
 
31
#include "base/singleton.h"
 
32
#include "converter/connector_interface.h"
 
33
#include "converter/sparse_connector.h"
38
34
 
39
35
namespace mozc {
40
36
 
41
 
ConnectorInterface *ConnectorInterface::OpenFromArray(const char *ptr,
42
 
                                                      size_t size) {
43
 
  const int16 *image = reinterpret_cast<const int16*>(ptr);
44
 
  if (image[0] == kDenseConnectorMagic) {
45
 
    VLOG(1) << "DenseConector";
46
 
    return new DenseConnector(ptr, size);
47
 
  } else if (image[0] == kSparseConnectorMagic) {
48
 
    VLOG(1) << "SparseConector";
49
 
    return new SparseConnector(ptr, size);
50
 
  }
51
 
  LOG(FATAL) << "unsuported Connector image";
52
 
  return NULL;
53
 
}
54
 
 
55
 
///////////////////////////////////////////////////////////////////////
56
 
// DenseConnector implementations
57
 
DenseConnector::DenseConnector(const char *ptr, size_t size)
58
 
    : matrix_(NULL), lsize_(0), rsize_(0) {
59
 
  matrix_ = reinterpret_cast<const int16*>(ptr);
60
 
  lsize_  = static_cast<int>(matrix_[2]);
61
 
  rsize_  = static_cast<int>(matrix_[3]);
62
 
  CHECK(static_cast<int>(lsize_ * rsize_ + 4) ==
63
 
        size / sizeof(matrix_[0]))
64
 
      << "connection table is broken";
65
 
  matrix_ += 4;
66
 
}
67
 
 
68
 
DenseConnector::~DenseConnector() {}
69
 
 
70
 
void DenseConnector::CompileImage(const int16 *matrix,
71
 
                                  uint16 lsize, uint16 rsize,
72
 
                                  const char *binary_file) {
73
 
  OutputFileStream ofs(binary_file, ios::binary|ios::out);
74
 
  CHECK(ofs) << "permission denied: " << binary_file;
75
 
  uint16 magic = kDenseConnectorMagic;
76
 
  ofs.write(reinterpret_cast<const char*>(&magic), sizeof(magic));
77
 
  // write one more value to align matrix image in 4 bytes.
78
 
  magic = 0;
79
 
  ofs.write(reinterpret_cast<const char*>(&magic), sizeof(magic));
80
 
  ofs.write(reinterpret_cast<const char*>(&lsize), sizeof(lsize));
81
 
  ofs.write(reinterpret_cast<const char*>(&rsize), sizeof(rsize));
82
 
  ofs.write(reinterpret_cast<const char*>(&matrix[0]),
83
 
            sizeof(matrix[0]) * lsize * rsize);
84
 
  ofs.close();
85
 
}
86
 
 
87
 
///////////////////////////////////////////////////////////////////////
88
 
// SparseConnector implementations
89
 
SparseConnector::SparseConnector(const char *ptr, size_t size)
90
 
    : default_cost_(NULL) {
91
 
  // |magic(2byte)|null(2byte)|lsize(2byte)|rsize(2byte)
92
 
  // |default_cost..|sparse_image
93
 
  const size_t kHeaderSize = 8;
94
 
  CHECK_GT(size, kHeaderSize);
95
 
  const uint16 *image = reinterpret_cast<const uint16*>(ptr);
96
 
  const uint16 lsize = image[2];
97
 
  ptr += kHeaderSize;
98
 
 
99
 
  default_cost_ = reinterpret_cast<const int16 *>(ptr);
100
 
  const size_t default_cost_size = sizeof(default_cost_[0]) * lsize;
101
 
 
102
 
  ptr += default_cost_size;
103
 
 
104
 
  CHECK_GT(size, default_cost_size + kHeaderSize);
105
 
 
106
 
  const size_t array_image_size= size - kHeaderSize - default_cost_size;
107
 
  array_image_.reset(new SparseArrayImage(ptr, array_image_size));
108
 
}
109
 
 
110
 
SparseConnector::~SparseConnector() {}
111
 
 
112
 
int SparseConnector::GetTransitionCost(uint16 rid, uint16 lid) const {
113
 
  const int pos = array_image_->Peek(EncodeKey(rid, lid));
114
 
  if (pos == SparseArrayImage::kInvalidValueIndex) {
115
 
    return default_cost_[rid];
116
 
  }
117
 
  return array_image_->GetValue(pos);
118
 
}
119
 
 
120
 
void SparseConnector::CompileImage(const int16 *matrix,
121
 
                                   uint16 lsize, uint16 rsize,
122
 
                                   const char *binary_file) {
123
 
  LOG(INFO) << "compiling matrix with " << lsize * rsize;
124
 
 
125
 
  SparseArrayBuilder builder;
126
 
  vector<int16> default_cost(lsize);
127
 
  fill(default_cost.begin(), default_cost.end(), static_cast<int16>(0));
128
 
 
129
 
  for (int lid = 0; lid < lsize; ++lid) {
130
 
    for (int rid = 0; rid < rsize; ++rid) {
131
 
      const int16 c = matrix[lid + lsize * rid];
132
 
      if (ConnectorInterface::kInvalidCost != c) {
133
 
        default_cost[lid] = max(default_cost[lid], c);  // save default cost
134
 
      }
135
 
    }
136
 
  }
137
 
 
138
 
  for (int lid = 0; lid < lsize; ++lid) {
139
 
    for (int rid = 0; rid < rsize; ++rid) {
140
 
      const int16 c = matrix[lid + lsize * rid];
141
 
      if (c != default_cost[lid]) {
142
 
        builder.AddValue(EncodeKey(lid, rid), c);
143
 
      }
144
 
    }
145
 
  }
146
 
 
147
 
  builder.Build();
148
 
 
149
 
  OutputFileStream ofs(binary_file, ios::binary|ios::out);
150
 
  CHECK(ofs) << "permission denied: " << binary_file;
151
 
 
152
 
  CHECK_EQ(lsize, default_cost.size());
153
 
 
154
 
  uint16 magic = kSparseConnectorMagic;
155
 
  ofs.write(reinterpret_cast<const char*>(&magic), sizeof(magic));
156
 
  // write one more value to align matrix image in 4 bytes.
157
 
  magic = 0;
158
 
  ofs.write(reinterpret_cast<const char*>(&magic), sizeof(magic));
159
 
  ofs.write(reinterpret_cast<const char*>(&lsize), sizeof(lsize));
160
 
  ofs.write(reinterpret_cast<const char*>(&rsize), sizeof(rsize));
161
 
  ofs.write(reinterpret_cast<const char*>(&default_cost[0]),
162
 
            sizeof(default_cost[0]) * default_cost.size());
163
 
  ofs.write(builder.GetImage(), builder.GetSize());
164
 
  ofs.close();
 
37
namespace {
 
38
 
 
39
#include "converter/embedded_connection_data.h"
 
40
 
 
41
class SparseConnectorInitializer {
 
42
 public:
 
43
  SparseConnectorInitializer()
 
44
      : connector_(new SparseConnector(
 
45
          kConnectionData_data, kConnectionData_size)) {}
 
46
  virtual ~SparseConnectorInitializer() {
 
47
    delete connector_;
 
48
  }
 
49
 
 
50
  SparseConnector *Get() const {
 
51
    return connector_;
 
52
  }
 
53
 
 
54
 private:
 
55
  SparseConnector *connector_;
 
56
};
 
57
 
 
58
ConnectorInterface *g_connector = NULL;
 
59
}  // namespace
 
60
 
 
61
ConnectorInterface *ConnectorFactory::GetConnector() {
 
62
  if (g_connector == NULL) {
 
63
    return Singleton<SparseConnectorInitializer>::get()->Get();
 
64
  } else {
 
65
    return g_connector;
 
66
  }
165
67
}
166
68
}  // namespace mozc