File: syngroups.cpp

package info (click to toggle)
recoll 1.43.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 16,400 kB
  • sloc: cpp: 103,890; python: 9,349; xml: 7,305; ansic: 6,447; sh: 1,212; perl: 130; makefile: 72
file content (258 lines) | stat: -rw-r--r-- 7,192 bytes parent folder | download | duplicates (2)
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
249
250
251
252
253
254
255
256
257
258
/* Copyright (C) 2014-2019 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.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "autoconfig.h"

#include "syngroups.h"

#include <unordered_map>
#include <fstream>
#include <iostream>
#include <cstring>
#include <algorithm>

#include "log.h"
#include "smallut.h"
#include "pathut.h"


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() {}
    void setpath(const string& fn) {
        path = path_canon(fn);
        path_fileprops(path, &st);
    }
    bool samefile(const string& fn) {
        string p1 = path_canon(fn);
        if (path != p1) {
            return false;
        }
        struct PathStat st1;
        if (path_fileprops(p1, &st1) != 0) {
            return false;
        }
        return st.pst_mtime == st1.pst_mtime && st.pst_size == st1.pst_size;
    }

    void clear() {
        ok = false;
        terms.clear();
        groups.clear();
        multiwords.clear();
        multiwords_maxlen = 0;
        path.clear();
        st.pst_mtime = 0;
        st.pst_size = 0;
    }
    
    bool ok{false};
    // Term to group num 
    std::unordered_map<string, size_t> terms;
    // Group num to group
    vector<vector<string> > groups;

    // Aux: set of multiword synonyms used for generating multiword
    // terms while indexing
    std::set<std::string> multiwords;
    size_t multiwords_maxlen{0};
    
    std::string path;
    struct PathStat st;
};

bool SynGroups::ok() const
{
    return m && m->ok;
}

SynGroups::~SynGroups()
{
    delete m;
}

SynGroups::SynGroups()
    : m(new Internal)
{
}

bool SynGroups::setfile(const string& fn)
{
    LOGDEB("SynGroups::setfile(" << fn << ")\n");
    if (!m) {
        return false;
    }

    if (fn.empty()) {
        m->clear();
        return true;
    }

    if (m->samefile(fn)) {
        LOGDEB("SynGroups::setfile: unchanged: " << fn << endl);
        return true;
    }
    LOGDEB("SynGroups::setfile: parsing file " << fn << endl);
    
    ifstream input;
    input.open(fn.c_str(), ios::in);
    if (!input.is_open()) {
        LOGSYSERR("SynGroups:setfile", "open", fn);
        return false;
    }        

    string cline;
    bool appending = false;
    string line;
    bool eof = false;
    int lnum = 0;
    m->clear();
    for (;;) {
        cline.clear();
        getline(input, cline);
        if (!input.good()) {
            if (input.bad()) {
                LOGERR("Syngroup::setfile(" << fn << "):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++;

        {
            string::size_type pos = cline.find_last_not_of("\n\r");
            if (pos == string::npos) {
                cline.clear();
            } else if (pos != cline.length()-1) {
                cline.erase(pos+1);
            }
        }

        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: " << fn << ": bad line " << lnum << ": " << line << "\n");
            continue;
        }

        if (words.empty())
            continue;
        if (words.size() == 1) {
            LOGERR("Syngroup::setfile(" << fn << "):single term group at line " << lnum << " ??\n");
            continue;
        }
        for (auto& word : words) {
            if (word.find_first_of(" \t") != std::string::npos) {
                vector<string> v;
                stringToTokens(word, v);
                word = tokensToString(v);
            }
            m->terms[word] = m->groups.size();
        }
        m->groups.push_back(words);
        LOGDEB1("SynGroups::setfile: group: [" << stringsToString(m->groups.back()) << "]\n");
    }

    for (const auto& group : m->groups) {
        for (const auto& term : group) {
            // Whitespace was already normalized to single 0x20
            if (term.find(' ') != std::string::npos) {
                size_t cnt = std::count(term.begin(), term.end(), ' ') + 1;
                m->multiwords.insert(term);
                if (m->multiwords_maxlen < cnt) {
                    m->multiwords_maxlen = cnt;
                }
            }
        }
    }
    LOGDEB("SynGroups::setfile: got " << m->groups.size() << " distinct terms. "
           "Multiwords: " << stringsToString(m->multiwords) <<"\n");
    m->ok = true;
    m->setpath(fn);
    return true;
}

vector<string> SynGroups::getgroup(const string& term) const
{
    vector<string> ret;
    if (!ok())
        return ret;

    const auto it1 = m->terms.find(term);
    if (it1 == m->terms.end()) {
        LOGDEB0("SynGroups::getgroup: [" << term << "] not found in map\n");
        return ret;
    }

    size_t idx = it1->second;
    if (idx >= m->groups.size()) {
        LOGERR("SynGroups::getgroup: line index higher than line count !\n");
        return ret;
    }
    LOGDEB0("SynGroups::getgroup: result: " << stringsToString(m->groups[idx]) << '\n');
    return m->groups[idx];
}

const std::set<std::string>& SynGroups::getmultiwords() const
{
    return m->multiwords;
}

size_t SynGroups::getmultiwordsmaxlength() const
{
    return m->multiwords_maxlen;
}

const std::string& SynGroups::getpath() const
{
    static string empty;
    return m ? m->path : empty;
}