Grammalecte  Check-in [ec295f726f]

Overview
Comment:[graphspell] new suggestion mechanism for Javascript too
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk | graphspell
Files: files | file ages | folders
SHA3-256: ec295f726fa6ef8aa64ca1da7f22d2f36b6d67b3392dc571013ba37d87c1a5ff
User & Date: olr on 2025-09-14 14:31:40
Other Links: manifest | tags
Context
2025-09-14
16:01
[fr] config.ini: dictionnaires 7.7, Grammalecte 2.3 Leaf check-in: a74fcc98bb user: olr tags: trunk, fr
14:31
[graphspell] new suggestion mechanism for Javascript too check-in: ec295f726f user: olr tags: trunk, graphspell
13:10
[build][core][lo] update minimal requirements: Python 3.7, LO 6.4 check-in: 3e14557ae0 user: olr tags: trunk, core, build, lo
Changes

Modified graphspell-js/ibdawg.js from [9375ebc47a] to [abe9c98a1b].

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
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







-
+



-
-
+

-
+
-
-
-
+








-
-
+
+
-
-
-
+
+

-
-
-
-
+
-
-
-
-
-
+
+
+
+



+



-
+
+

-
-
-
-
-
-
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-








class SuggResult {
    // Structure for storing, classifying and filtering suggestions

    constructor (sWord, nSuggLimit=10, nDistLimit=-1) {
        this.sWord = sWord;
        this.sSimplifiedWord = str_transform.simplifyWord(sWord);
        this.nDistLimit = (nDistLimit >= 0) ? nDistLimit :  Math.floor(sWord.length / 3) + 1;
        this.nDistLimit = (nDistLimit >= 0) ? nDistLimit :  Math.floor(sWord.length / 3) + 1; // maximum accepted distance, used in suggest()
        this.nMinDist = 1000;
        // Temporary sets
        this.aAllSugg = new Set();  // All suggestions, even the one rejected
        this.dGoodSugg = new Map(); // Acceptable suggestions
        this.dBestSugg = new Map(); // Best suggestions
        this.dAccSugg = new Map();  // Accepted suggestions
        // Parameters
        this.nSuggLimit = nSuggLimit;
        this.nSuggLimit = nSuggLimit;           // number of returned suggestions
        this.nSuggLimitExt = nSuggLimit + 2;                // we add few entries in case suggestions merge after casing modifications
        this.nBestSuggLimit = Math.floor(nSuggLimit * 2);   // n times the requested limit
        this.nGoodSuggLimit = nSuggLimit * 15;              // n times the requested limit
        this.nTempSuggLimit = nSuggLimit * 6;   // limit of accepted suggestions (ends search over this limit)
    }

    addSugg (sSugg) {
        // add a suggestion
        if (this.aAllSugg.has(sSugg)) {
            return;
        }
        this.aAllSugg.add(sSugg);
        // jaro 0->1 1 les chaines sont égale
        let nDistJaro = 1 - str_transform.distanceJaroWinkler(this.sSimplifiedWord, str_transform.simplifyWord(sSugg));
        //console.log("Grammalecte: " + sSugg);
        let nSimDist = str_transform.distanceSift4(this.sSimplifiedWord, str_transform.simplifyWord(sSugg));
        let nDist = Math.floor(nDistJaro * 10);
        if (nDist < this.nMinDist) {
            this.nMinDist = nDist;
        if (nSimDist < this.nMinDist) {
            this.nMinDist = nSimDist;
        }
        if (nDistJaro < .11) {        // Best suggestions
            this.dBestSugg.set(sSugg, Math.round(nDistJaro*1000));
            if (this.dBestSugg.size > this.nBestSuggLimit) {
                this.nDistLimit = -1; // make suggest() to end search
        if (nSimDist <= this.nMinDist+1) {
            }
        } else if (nDistJaro < .33) { // Good suggestions
            this.dGoodSugg.set(sSugg, Math.round(nDistJaro*1000));
            if (this.dGoodSugg.size > this.nGoodSuggLimit) {
                this.nDistLimit = -1; // make suggest() to end search
            let nDist = Math.min(str_transform.distanceDamerauLevenshtein(this.sWord, sSugg), str_transform.distanceDamerauLevenshtein(this.sSimplifiedWord, str_transform.simplifyWord(sSugg)));
            this.dAccSugg.set(sSugg, Math.min(nDist, nSimDist+1));
            if (this.dAccSugg.size  > this.nTempSuggLimit) {
                this.nDistLimit = -1; // suggest() ends searching when this variable = -1
            }
        }
        this.nDistLimit = Math.min(this.nDistLimit, this.nMinDist+1);
        //console.log(this.dAccSugg);
    }

    getSuggestions () {
        // return a list of suggestions
        //// return a list of suggestions
        // sort according to distance
        let lRes = [];
        if (this.dBestSugg.size > 0) {
            // sort only with simplified words
            let lResTmp = [...this.dBestSugg.entries()].sort((a, b) => { return a[1] - b[1]; });
            let nSize = Math.min(this.nSuggLimitExt, lResTmp.length);
            for (let i=0;  i < nSize;  i++){
                lRes.push(lResTmp[i][0]);
        let lResTmp = [...this.dAccSugg.entries()].sort((a, b) => { return a[1] - b[1]; });
        let nSize = Math.min(this.nSuggLimit, lResTmp.length);
        for (let i=0;  i < nSize;  i++){
            lRes.push(lResTmp[i][0]);
            }
        }
        if (lRes.length < this.nSuggLimitExt) {
            // sort with simplified words and original word
            let lResTmp = [...this.dGoodSugg.entries()].sort((a, b) => {
                // Low precision to rely more on simplified words
                let nJaroA = Math.round(str_transform.distanceJaroWinkler(this.sWord, a[0]) * 10);
                let nJaroB = Math.round(str_transform.distanceJaroWinkler(this.sWord, b[0]) * 10);
                if (nJaroA == nJaroB) {
                    return a[1] - b[1];     // warning: both lists are NOT sorted the same way (key: a-b)
                } else {
                    return nJaroB - nJaroA; // warning: both lists are NOT sorted the same way (key: b-a)
                }
            }).slice(0, this.nSuggLimitExt);
            let nSize = Math.min(this.nSuggLimitExt, lResTmp.length);
            for (let i=0;  i < nSize;  i++){
                lRes.push(lResTmp[i][0]);
            }
        }
        // casing
        if (this.sWord.gl_isUpperCase()) {
            lRes = lRes.map((sSugg) => { return sSugg.toUpperCase(); });
            lRes = [...new Set(lRes)];
        }
        else if (this.sWord.slice(0,1).gl_isUpperCase()) {

Modified graphspell/ibdawg.py from [979c697260] to [a6af4bb96b].

39
40
41
42
43
44
45
46

47
48
49
50
51
52
53
39
40
41
42
43
44
45

46
47
48
49
50
51
52
53







-
+








class SuggResult:
    """Structure for storing, classifying and filtering suggestions"""

    def __init__ (self, sWord, nSuggLimit=10, nDistLimit=-1):
        self.sWord = sWord
        self.sSimplifiedWord = st.simplifyWord(sWord)
        self.nDistLimit = nDistLimit  if nDistLimit >= 0  else  (len(sWord) // 3) + 1 # used in suggest()
        self.nDistLimit = nDistLimit  if nDistLimit >= 0  else  (len(sWord) // 3) + 1 # maximum accepted distance, used in suggest()
        self.nMinDist = 1000
        # Temporary sets
        self.aAllSugg = set()   # All suggestions, even the one rejected
        self.dAccSugg = {}      # Accepted suggestions
        # Parameters
        self.nSuggLimit = nSuggLimit            # number of returned suggestions
        self.nTempSuggLimit = nSuggLimit * 6    # limit of accepted suggestions (ends search over this limit)