Switch to unified view

a b/src/rcldb/synfamily.h
1
/* Copyright (C) 2012 J.F.Dockes
2
 *   This program is free software; you can redistribute it and/or modify
3
 *   it under the terms of the GNU General Public License as published by
4
 *   the Free Software Foundation; either version 2 of the License, or
5
 *   (at your option) any later version.
6
 *
7
 *   This program is distributed in the hope that it will be useful,
8
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 *   GNU General Public License for more details.
11
 *
12
 *   You should have received a copy of the GNU General Public License
13
 *   along with this program; if not, write to the
14
 *   Free Software Foundation, Inc.,
15
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
16
 */
17
#ifndef _SYNFAMILY_H_INCLUDED_
18
#define _SYNFAMILY_H_INCLUDED_
19
20
/**
21
 * The Xapian synonyms mechanism can be used for many things beyond actual
22
 * synonyms, anything that would turn a string into a group of equivalents.
23
 * Unfortunately, it has only one keyspace. 
24
 * This class partitions the Xapian synonyms keyspace by using prefixes and
25
 * can provide different applications each with a family of keyspaces.
26
 * Two characters are reserved by the class and should not be used inside 
27
 * either family or member names: ':' and ';'
28
 * A synonym key for family "stemdb", member "french", key "thisstem" 
29
 * looks like:
30
 *  :stemdb:french:stem  -> stem siblings
31
 * A special entry is used to list all the members for a family, e.g.:
32
 *  :stemdb;members  -> french, english ...
33
 */
34
35
#include <string>
36
#include <vector>
37
38
#include "xapian.h"
39
40
namespace Rcl {
41
42
class XapSynFamily {
43
public:
44
    /** 
45
     * Construct from readable xapian database and family name (ie: Stm)
46
     */
47
    XapSynFamily(Xapian::Database xdb, const std::string& familyname)
48
  : m_rdb(xdb)
49
    {
50
  m_prefix1 = string(":") + familyname;
51
    }
52
53
    /** Expand one term (e.g.: familier) inside one family number (e.g: french)
54
     */
55
    bool synExpand(const std::string& fammember,
56
         const std::string& term,
57
         std::vector<std::string>& result);
58
59
    /** Retrieve all members of this family (e.g: french english german...) */
60
    bool getMembers(std::vector<std::string>&);
61
62
    /** debug: list map for one member to stdout */
63
    bool listMap(const std::string& fam); 
64
65
protected:
66
    Xapian::Database m_rdb;
67
    std::string m_prefix1;
68
    string entryprefix(const string& member)
69
    {
70
  return m_prefix1 + ":" + member + ":";
71
    }
72
    string memberskey()
73
    {
74
  return m_prefix1 + ";" + "members";
75
    }
76
77
};
78
79
class XapWritableSynFamily : public XapSynFamily {
80
public:
81
    /** Construct with Xapian db open for r/w */
82
    XapWritableSynFamily(Xapian::WritableDatabase db, const std::string& pfx)
83
  : XapSynFamily(db, pfx),  m_wdb(db)
84
    {
85
    }
86
87
    /** Delete all entries for one member (e.g. french), and remove from list
88
     * of members */
89
    bool deleteMember(const std::string& membername);
90
91
    /** Add to list of members. Idempotent, does not affect actual expansions */
92
    bool createMember(const std::string& membername);
93
94
    /** Add expansion list for term inside family member (e.g., inside
95
     *  the french member, add expansion for familier -> familier,
96
     * familierement, ... */
97
    bool addSynonyms(const string& membername, 
98
           const string& term, const vector<string>& trans);
99
100
protected:
101
    Xapian::WritableDatabase m_wdb;
102
};
103
104
105
}
106
107
#endif /* _SYNFAMILY_H_INCLUDED_ */