1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt Linguist of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#ifndef SIMTEXTH_H
30#define SIMTEXTH_H
31
32const int textSimilarityThreshold = 190;
33
34#include <QString>
35#include <QList>
36
37QT_BEGIN_NAMESPACE
38
39class Translator;
40
41struct Candidate
42{
43 Candidate() {}
44 Candidate(const QString &c, const QString &s, const QString &d, const QString &t)
45 : context(c), source(s), disambiguation(d), translation(t)
46 {}
47
48 QString context;
49 QString source;
50 QString disambiguation;
51 QString translation;
52};
53
54inline bool operator==( const Candidate& c, const Candidate& d ) {
55 return c.translation == d.translation && c.source == d.source && c.context == d.context
56 && c.disambiguation == d.disambiguation;
57}
58inline bool operator!=( const Candidate& c, const Candidate& d ) {
59 return !operator==( c, d );
60}
61
62typedef QList<Candidate> CandidateList;
63
64struct CoMatrix
65{
66 CoMatrix(const QString &str);
67 CoMatrix() {}
68
69 /*
70 The matrix has 20 * 20 = 400 entries. This requires 50 bytes, or 13
71 words. Some operations are performed on words for more efficiency.
72 */
73 union {
74 quint8 b[52];
75 quint32 w[13];
76 };
77};
78
79/**
80 * This class is more efficient for searching through a large array of candidate strings, since we only
81 * have to construct the CoMatrix for the \a stringToMatch once,
82 * after that we just call getSimilarityScore(strCandidate).
83 * \sa getSimilarityScore
84 */
85class StringSimilarityMatcher {
86public:
87 StringSimilarityMatcher(const QString &stringToMatch);
88 int getSimilarityScore(const QString &strCandidate);
89
90private:
91 CoMatrix m_cm;
92 int m_length;
93};
94
95/**
96 * Checks how similar two strings are.
97 * The return value is the score, and a higher score is more similar
98 * than one with a low score.
99 * Linguist considers a score over 190 to be a good match.
100 * \sa StringSimilarityMatcher
101 */
102static inline int getSimilarityScore(const QString &str1, const QString &str2)
103{
104 return StringSimilarityMatcher(str1).getSimilarityScore(strCandidate: str2);
105}
106
107CandidateList similarTextHeuristicCandidates( const Translator *tor,
108 const QString &text,
109 int maxCandidates );
110
111QT_END_NAMESPACE
112
113#endif
114

source code of qttools/src/linguist/shared/simtexth.h