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

« back to all changes in this revision

Viewing changes to converter/lattice.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:
 
1
// Copyright 2010, Google Inc.
 
2
// All rights reserved.
 
3
//
 
4
// Redistribution and use in source and binary forms, with or without
 
5
// modification, are permitted provided that the following conditions are
 
6
// met:
 
7
//
 
8
//     * Redistributions of source code must retain the above copyright
 
9
// notice, this list of conditions and the following disclaimer.
 
10
//     * Redistributions in binary form must reproduce the above
 
11
// copyright notice, this list of conditions and the following disclaimer
 
12
// in the documentation and/or other materials provided with the
 
13
// distribution.
 
14
//     * Neither the name of Google Inc. nor the names of its
 
15
// contributors may be used to endorse or promote products derived from
 
16
// this software without specific prior written permission.
 
17
//
 
18
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
19
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
20
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 
21
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 
22
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
23
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
24
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 
25
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 
26
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
27
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
28
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
29
 
 
30
#include "converter/lattice.h"
 
31
 
 
32
#include <string>
 
33
#include "base/base.h"
 
34
#include "converter/node.h"
 
35
#include "converter/pos_matcher.h"
 
36
 
 
37
namespace mozc {
 
38
 
 
39
namespace {
 
40
 
 
41
Node *InitBOSNode(Lattice *lattice, uint16 length) {
 
42
  Node *bos_node = lattice->NewNode();
 
43
  DCHECK(bos_node);
 
44
  bos_node->rid = 0;
 
45
  bos_node->lid = 0;
 
46
  bos_node->key.clear();
 
47
  bos_node->value = "BOS";
 
48
  bos_node->node_type = Node::BOS_NODE;
 
49
  bos_node->wcost = 0;
 
50
  bos_node->cost = 0;
 
51
  bos_node->begin_pos = length;
 
52
  bos_node->end_pos = length;
 
53
  return bos_node;
 
54
}
 
55
 
 
56
Node *InitEOSNode(Lattice *lattice, uint16 length) {
 
57
  Node *eos_node = lattice->NewNode();
 
58
  DCHECK(eos_node);
 
59
  eos_node->rid = 0;   // pure EOS
 
60
  eos_node->lid = 0;
 
61
  eos_node->key.clear();
 
62
  eos_node->value = "EOS";
 
63
  eos_node->node_type = Node::EOS_NODE;
 
64
  eos_node->wcost = 0;
 
65
  eos_node->cost = 0;
 
66
  eos_node->begin_pos = length;
 
67
  eos_node->end_pos = length;
 
68
 
 
69
  Node *eos_noun_node = lattice->NewNode();
 
70
  DCHECK(eos_noun_node);
 
71
  eos_noun_node->rid = POSMatcher::GetUnknownId();
 
72
  eos_noun_node->lid = POSMatcher::GetUnknownId();
 
73
  eos_noun_node->key.clear();
 
74
  eos_noun_node->value = "EOS";
 
75
  eos_noun_node->node_type = Node::EOS_NODE;
 
76
  eos_noun_node->wcost = 0;
 
77
  eos_noun_node->cost = 0;
 
78
  eos_noun_node->begin_pos = length;
 
79
  eos_noun_node->end_pos = length;
 
80
 
 
81
  // chain nodes
 
82
  eos_node->bnext = eos_noun_node;
 
83
 
 
84
  return eos_node;
 
85
}
 
86
}  // namespace
 
87
 
 
88
class NodeAllocator : public NodeAllocatorInterface {
 
89
 public:
 
90
  NodeAllocator(): node_freelist_(1024) {}
 
91
  virtual ~NodeAllocator() {}
 
92
 
 
93
  virtual Node *NewNode() {
 
94
    Node *node = node_freelist_.Alloc();
 
95
    DCHECK(node);
 
96
    node->Init();
 
97
    return node;
 
98
  }
 
99
 
 
100
  // Free all nodes allocateed by NewNode()
 
101
  void Free(){
 
102
    node_freelist_.Free();
 
103
  }
 
104
 
 
105
 private:
 
106
  FreeList<Node> node_freelist_;
 
107
};
 
108
 
 
109
Lattice::Lattice() : node_allocator_(new NodeAllocator) {}
 
110
 
 
111
Lattice::~Lattice() {}
 
112
 
 
113
NodeAllocatorInterface *Lattice::node_allocator() const {
 
114
  return node_allocator_.get();
 
115
}
 
116
 
 
117
Node *Lattice::NewNode() {
 
118
  return node_allocator_->NewNode();
 
119
}
 
120
 
 
121
Node *Lattice::begin_nodes(size_t pos) const {
 
122
  return begin_nodes_[pos];
 
123
}
 
124
 
 
125
Node *Lattice::end_nodes(size_t pos) const {
 
126
  return end_nodes_[pos];
 
127
}
 
128
 
 
129
void Lattice::SetKey(const string &key) {
 
130
  Clear();
 
131
  key_ = key;
 
132
  const size_t size = key.size();
 
133
  begin_nodes_.resize(size + 4);
 
134
  end_nodes_.resize(size + 4);
 
135
 
 
136
  fill(begin_nodes_.begin(), begin_nodes_.end(),
 
137
       static_cast<Node *>(NULL));
 
138
  fill(end_nodes_.begin(), end_nodes_.end(),
 
139
       static_cast<Node *>(NULL));
 
140
 
 
141
  end_nodes_[0] = InitBOSNode(this,
 
142
                              static_cast<uint16>(0));
 
143
  begin_nodes_[key_.size()] =
 
144
      InitEOSNode(this, static_cast<uint16>(key_.size()));
 
145
}
 
146
 
 
147
Node *Lattice::bos_nodes() const {
 
148
  return end_nodes_[0];
 
149
}
 
150
 
 
151
Node *Lattice::eos_nodes() const {
 
152
  return begin_nodes_[key_.size()];
 
153
}
 
154
 
 
155
void Lattice::Insert(size_t pos, Node *node) {
 
156
  for (Node *rnode = node; rnode != NULL; rnode = rnode->bnext) {
 
157
    rnode->begin_pos = static_cast<uint16>(pos);
 
158
    rnode->end_pos = static_cast<uint16>(pos + rnode->key.size());
 
159
    rnode->prev = NULL;
 
160
    rnode->next = NULL;
 
161
    rnode->cost = 0;
 
162
    const size_t x = rnode->key.size() + pos;
 
163
    rnode->enext = end_nodes_[x];
 
164
    end_nodes_[x] = rnode;
 
165
  }
 
166
 
 
167
  if (begin_nodes_[pos] == NULL) {
 
168
    begin_nodes_[pos] = node;
 
169
  } else {
 
170
    for (Node *rnode = node; rnode != NULL; rnode = rnode->bnext) {
 
171
      if (rnode->bnext == NULL) {
 
172
        rnode->bnext = begin_nodes_[pos];
 
173
        begin_nodes_[pos] = node;
 
174
        break;
 
175
      }
 
176
    }
 
177
  }
 
178
}
 
179
 
 
180
const string &Lattice::key() const {
 
181
  return key_;
 
182
}
 
183
 
 
184
bool Lattice::has_lattice() const {
 
185
  return !begin_nodes_.empty();
 
186
}
 
187
 
 
188
void Lattice::Clear() {
 
189
  key_.clear();
 
190
  begin_nodes_.clear();
 
191
  end_nodes_.clear();
 
192
  node_allocator_->Free();
 
193
}
 
194
}  // namespace mozc