1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/tools/convert_dict/dic_reader.h"
#include <algorithm>
#include <set>
#include "base/file_util.h"
#include "base/string_util.h"
#include "chrome/tools/convert_dict/aff_reader.h"
#include "chrome/tools/convert_dict/hunspell_reader.h"
namespace convert_dict {
namespace {
// Maps each unique word to the unique affix group IDs associated with it.
typedef std::map<std::string, std::set<int> > WordSet;
void SplitDicLine(const std::string& line, std::vector<std::string>* output) {
// We split the line on a slash not preceeded by a backslash. A slash at the
// beginning of the line is not a separator either.
size_t slash_index = line.size();
for (size_t i = 0; i < line.size(); i++) {
if (line[i] == '/' && i > 0 && line[i - 1] != '\\') {
slash_index = i;
break;
}
}
output->clear();
// Everything before the slash index is the first term. We also need to
// convert all escaped slashes ("\/" sequences) to regular slashes.
std::string word = line.substr(0, slash_index);
ReplaceSubstringsAfterOffset(&word, 0, "\\/", "/");
output->push_back(word);
// Everything (if anything) after the slash is the second.
if (slash_index < line.size() - 1)
output->push_back(line.substr(slash_index + 1));
}
// This function reads words from a .dic file, or a .dic_delta file. Note that
// we read 'all' the words in the file, irrespective of the word count given
// in the first non empty line of a .dic file. Also note that, for a .dic_delta
// file, the first line actually does _not_ have the number of words. In order
// to control this, we use the |file_has_word_count_in_the_first_line|
// parameter to tell this method whether the first non empty line in the file
// contains the number of words or not. If it does, skip the first line. If it
// does not, then the first line contains a word.
bool PopulateWordSet(WordSet* word_set, FILE* file, AffReader* aff_reader,
const char* file_type, const char* encoding,
bool file_has_word_count_in_the_first_line) {
printf("Extracting words from %s file\nEncoding: %s\n", file_type, encoding);
int line_number = 0;
while (!feof(file)) {
std::string line = ReadLine(file);
line_number++;
StripComment(&line);
if (line.empty())
continue;
if (file_has_word_count_in_the_first_line) {
// Skip the first nonempty line, this is the line count. We don't bother
// with it and just read all the lines.
file_has_word_count_in_the_first_line = false;
continue;
}
std::vector<std::string> split;
SplitDicLine(line, &split);
if (split.size() == 0 || split.size() > 2) {
printf("Line %d has extra slashes in the %s file\n", line_number,
file_type);
return false;
}
// The first part is the word, the second (optional) part is the affix. We
// always use UTF-8 as the encoding to simplify life.
std::string utf8word;
std::string encoding_string(encoding);
if (encoding_string == "UTF-8") {
utf8word = split[0];
} else if (!aff_reader->EncodingToUTF8(split[0], &utf8word)) {
printf("Unable to convert line %d from %s to UTF-8 in the %s file\n",
line_number, encoding, file_type);
return false;
}
// We always convert the affix to an index. 0 means no affix.
int affix_index = 0;
if (split.size() == 2) {
// Got a rule, which is the stuff after the slash. The line may also have
// an optional term separated by a tab. This is the morphological
// description. We don't care about this (it is used in the tests to
// generate a nice dump), so we remove it.
size_t split1_tab_offset = split[1].find('\t');
if (split1_tab_offset != std::string::npos)
split[1] = split[1].substr(0, split1_tab_offset);
if (aff_reader->has_indexed_affixes())
affix_index = atoi(split[1].c_str());
else
affix_index = aff_reader->GetAFIndexForAFString(split[1]);
}
WordSet::iterator found = word_set->find(utf8word);
if (found == word_set->end()) {
std::set<int> affix_vector;
affix_vector.insert(affix_index);
word_set->insert(std::make_pair(utf8word, affix_vector));
} else {
found->second.insert(affix_index);
}
}
return true;
}
} // namespace
DicReader::DicReader(const std::string& filename) {
file_ = file_util::OpenFile(filename, "r");
additional_words_file_ = file_util::OpenFile(filename + "_delta", "r");
}
DicReader::~DicReader() {
if (file_)
file_util::CloseFile(file_);
if (additional_words_file_)
file_util::CloseFile(additional_words_file_);
}
bool DicReader::Read(AffReader* aff_reader) {
if (!file_)
return false;
WordSet word_set;
// Add words from the dic file to the word set.
// Note that the first line is the word count in the file.
if (!PopulateWordSet(&word_set, file_, aff_reader, "dic",
aff_reader->encoding(), true))
return false;
// Add words from the .dic_delta file to the word set, if it exists.
// The first line is the first word to add. Word count line is not present.
// NOTE: These additional words should be encoded as UTF-8.
if (additional_words_file_ != NULL) {
PopulateWordSet(&word_set, additional_words_file_, aff_reader, "dic delta",
"UTF-8", false);
}
// Make sure the words are sorted, they may be unsorted in the input.
for (WordSet::iterator word = word_set.begin(); word != word_set.end();
++word) {
std::vector<int> affixes;
for (std::set<int>::iterator aff = word->second.begin();
aff != word->second.end(); ++aff)
affixes.push_back(*aff);
// Double check that the affixes are sorted. This isn't strictly necessary
// but it's nice for the file to have a fixed layout.
std::sort(affixes.begin(), affixes.end());
words_.push_back(std::make_pair(word->first, affixes));
}
// Double-check that the words are sorted.
std::sort(words_.begin(), words_.end());
return true;
}
} // namespace convert_dict
|