1
// Copyright 2010, Google Inc.
2
// All rights reserved.
4
// Redistribution and use in source and binary forms, with or without
5
// modification, are permitted provided that the following conditions are
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
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.
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.
30
#include "converter/lattice.h"
33
#include "base/base.h"
34
#include "converter/node.h"
35
#include "converter/pos_matcher.h"
41
Node *InitBOSNode(Lattice *lattice, uint16 length) {
42
Node *bos_node = lattice->NewNode();
46
bos_node->key.clear();
47
bos_node->value = "BOS";
48
bos_node->node_type = Node::BOS_NODE;
51
bos_node->begin_pos = length;
52
bos_node->end_pos = length;
56
Node *InitEOSNode(Lattice *lattice, uint16 length) {
57
Node *eos_node = lattice->NewNode();
59
eos_node->rid = 0; // pure EOS
61
eos_node->key.clear();
62
eos_node->value = "EOS";
63
eos_node->node_type = Node::EOS_NODE;
66
eos_node->begin_pos = length;
67
eos_node->end_pos = length;
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;
82
eos_node->bnext = eos_noun_node;
88
class NodeAllocator : public NodeAllocatorInterface {
90
NodeAllocator(): node_freelist_(1024) {}
91
virtual ~NodeAllocator() {}
93
virtual Node *NewNode() {
94
Node *node = node_freelist_.Alloc();
100
// Free all nodes allocateed by NewNode()
102
node_freelist_.Free();
106
FreeList<Node> node_freelist_;
109
Lattice::Lattice() : node_allocator_(new NodeAllocator) {}
111
Lattice::~Lattice() {}
113
NodeAllocatorInterface *Lattice::node_allocator() const {
114
return node_allocator_.get();
117
Node *Lattice::NewNode() {
118
return node_allocator_->NewNode();
121
Node *Lattice::begin_nodes(size_t pos) const {
122
return begin_nodes_[pos];
125
Node *Lattice::end_nodes(size_t pos) const {
126
return end_nodes_[pos];
129
void Lattice::SetKey(const string &key) {
132
const size_t size = key.size();
133
begin_nodes_.resize(size + 4);
134
end_nodes_.resize(size + 4);
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));
141
end_nodes_[0] = InitBOSNode(this,
142
static_cast<uint16>(0));
143
begin_nodes_[key_.size()] =
144
InitEOSNode(this, static_cast<uint16>(key_.size()));
147
Node *Lattice::bos_nodes() const {
148
return end_nodes_[0];
151
Node *Lattice::eos_nodes() const {
152
return begin_nodes_[key_.size()];
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());
162
const size_t x = rnode->key.size() + pos;
163
rnode->enext = end_nodes_[x];
164
end_nodes_[x] = rnode;
167
if (begin_nodes_[pos] == NULL) {
168
begin_nodes_[pos] = node;
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;
180
const string &Lattice::key() const {
184
bool Lattice::has_lattice() const {
185
return !begin_nodes_.empty();
188
void Lattice::Clear() {
190
begin_nodes_.clear();
192
node_allocator_->Free();