Parent: [d8e8ce] (diff)

Child: [744d14] (diff)

Download this file

syngroups.cpp    249 lines (212 with data), 5.8 kB

  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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/* Copyright (C) 2014 J.F.Dockes
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef TEST_SYNGROUPS
#include "autoconfig.h"
#include "syngroups.h"
#include "debuglog.h"
#include "smallut.h"
#include <errno.h>
#include UNORDERED_MAP_INCLUDE
#include <fstream>
#include <iostream>
#include <cstring>
using namespace std;
// Note that we are storing each term twice. I don't think that the
// size could possibly be a serious issue, but if it was, we could
// reduce the storage a bit by storing (short hash)-> vector<int>
// correspondances in the direct map, and then checking all the
// resulting groups for the input word.
//
// As it is, a word can only index one group (the last it is found
// in). It can be part of several groups though (appear in
// expansions). I really don't know what we should do with multiple
// groups anyway
class SynGroups::Internal {
public:
Internal() : ok(false) {
}
bool ok;
// Term to group num
STD_UNORDERED_MAP<string, int> terms;
// Group num to group
STD_UNORDERED_MAP<int, vector<string> > groups;
};
bool SynGroups::ok()
{
return m && m->ok;
}
SynGroups::~SynGroups()
{
delete m;
}
const int LL = 1024;
SynGroups::SynGroups()
: m(new Internal)
{
}
bool SynGroups::setfile(const string& fn)
{
LOGDEB(("SynGroups::setfile(%s)\n", fn.c_str()));
if (!m) {
LOGERR(("SynGroups:setfile:: new Internal failed: no mem ?\n"));
return false;
}
// Don't set ok to true.
if (fn.empty())
return true;
ifstream input;
input.open(fn.c_str(), ios::in);
if (!input.is_open()) {
LOGERR(("SynGroups:setfile:: could not open %s errno %d\n",
fn.c_str(), errno));
return false;
}
char cline[LL];
bool appending = false;
string line;
bool eof = false;
int lnum = 0;
for (;;) {
cline[0] = 0;
input.getline(cline, LL-1);
if (!input.good()) {
if (input.bad()) {
LOGDEB(("Parse: input.bad()\n"));
return false;
}
// Must be eof ? But maybe we have a partial line which
// must be processed. This happens if the last line before
// eof ends with a backslash, or there is no final \n
eof = true;
}
lnum++;
{
size_t ll = strlen(cline);
while (ll > 0 && (cline[ll-1] == '\n' || cline[ll-1] == '\r')) {
cline[ll-1] = 0;
ll--;
}
}
if (appending)
line += cline;
else
line = cline;
// Note that we trim whitespace before checking for backslash-eol
// This avoids invisible whitespace problems.
trimstring(line);
if (line.empty() || line.at(0) == '#') {
if (eof)
break;
continue;
}
if (line[line.length() - 1] == '\\') {
line.erase(line.length() - 1);
appending = true;
continue;
}
appending = false;
vector<string> words;
if (!stringToStrings(line, words)) {
LOGERR(("SynGroups:setfile: %s: bad line %d: %s\n",
fn.c_str(), lnum, line.c_str()));
continue;
}
if (words.empty())
continue;
if (words.size() == 1) {
LOGDEB(("SynGroups:setfile: single term group at line %d ??\n",
lnum));
continue;
}
m->groups[lnum] = words;
for (vector<string>::const_iterator it = words.begin();
it != words.end(); it++) {
m->terms[*it] = lnum;
}
LOGDEB(("SynGroups::setfile: group: [%s]\n",
stringsToString(m->groups[lnum]).c_str()));
}
m->ok = true;
return true;
}
vector<string> SynGroups::getgroup(const string& term)
{
vector<string> ret;
if (!ok())
return ret;
STD_UNORDERED_MAP<string, int>::const_iterator it1 = m->terms.find(term);
if (it1 == m->terms.end()) {
LOGDEB1(("SynGroups::getgroup: [%s] not found in direct map\n",
term.c_str()));
return ret;
}
// Group num to group
STD_UNORDERED_MAP<int, vector<string> >::const_iterator it2 =
m->groups.find(it1->second);
if (it2 == m->groups.end()) {
LOGERR(("SynGroups::getgroup: %s found in direct map but no group\n",
term.c_str()));
return ret;
}
return it2->second;
}
#else
#include "syngroups.h"
#include <string>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <cstdio>
using namespace std;
static char *thisprog;
static char usage [] =
"syngroups <synfilename> <word>\n"
" \n\n"
;
static void Usage(void)
{
fprintf(stderr, "%s: usage:\n%s", thisprog, usage);
exit(1);
}
static int op_flags;
#define OPT_MOINS 0x1
#define OPT_s 0x2
#define OPT_b 0x4
int main(int argc, char **argv)
{
thisprog = argv[0];
argc--; argv++;
if (argc != 2) {
Usage();
}
string fn = *argv++;argc--;
string word = *argv++;argc--;
SynGroups syns(fn);
if (!syns.ok()) {
cerr << "Initialization failed\n";
return 1;
}
vector<string> group = syns.getgroup(word);
cout << group.size() << " terms in group\n";
for (vector<string>::const_iterator it = group.begin();
it != group.end(); it++) {
cout << "[" << *it << "] ";
}
cout << endl;
return 0;
}
#endif