File: SpellChecker.awk

package info (click to toggle)
translate-shell 0.9.6.9-1
  • links: PTS, VCS
  • area: contrib
  • in suites: buster
  • size: 536 kB
  • sloc: awk: 5,815; lisp: 54; makefile: 40; sh: 20
file content (107 lines) | stat: -rw-r--r-- 3,107 bytes parent folder | download | duplicates (4)
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
####################################################################
# SpellChecker.awk                                                 #
####################################################################
BEGIN {
    provides("spell")
    provides("aspell")
    provides("hunspell")
}

# Detect external (ispell -a compatible) spell checker.
function spellInit() {
    Ispell = detectProgram("aspell", "--version") ? "aspell" :
        (detectProgram("hunspell", "--version") ? "hunspell" : "")

    if (!Ispell) {
        e("[ERROR] Spell checker (aspell or hunspell) not found.")
        exit 1
    }
}

function aspellInit() {
    if (!(Ispell = detectProgram("aspell", "--version") ? "aspell" : "")) {
        e("[ERROR] Spell checker (aspell) not found.")
        exit 1
    }
}

function hunspellInit() {
    if (!(Ispell = detectProgram("hunspell", "--version") ? "hunspell" : "")) {
        e("[ERROR] Spell checker (hunspell) not found.")
        exit 1
    }
}

# Check a string.
function spellTranslate(text, sl, tl, hl,
                        isVerbose, toSpeech, returnPlaylist, returnIl,
                        ####
                        args, i, j, r, line, group, word, sug) {
    args = " -a" (sl != "auto" ? " -d " sl : "")
    if (system("echo" PIPE Ispell args SUPOUT SUPERR)) {
        e("[ERROR] No dictionary for language: " sl)
        exit 1
    }

    i = 1
    r = ""
    while ((("echo " parameterize(text) PIPE Ispell args SUPERR) |& getline line) > 0) {
        match(line,
              /^& (.*) [[:digit:]]+ [[:digit:]]+: ([^,]+)(, ([^,]+))?(, ([^,]+))?/,
              group)
        if (RSTART) {
            ExitCode = 1 # found a spelling error

            word = group[1]
            sug = "[" group[2]
            if (group[4]) sug = sug "|" group[4]
            if (group[6]) sug = sug "|" group[6]
            sug = sug "]"

            j = i + index(substr(text, i), word) - 1
            r = r substr(text, i, j - i)
            r = r ansi("bold", ansi("red", word)) ansi("yellow", sug)
            i = j + length(word)
        }
    }
    r = r substr(text, i)
    return r
}

function aspellTranslate(text, sl, tl, hl,
                         isVerbose, toSpeech, returnPlaylist, returnIl) {
    return spellTranslate(text, sl, tl, hl)
}

function hunspellTranslate(text, sl, tl, hl,
                           isVerbose, toSpeech, returnPlaylist, returnIl) {
    return spellTranslate(text, sl, tl, hl)
}

function spellTTSUrl(text, tl,    narrator) {
    e("[ERROR] Spell checker does not support TTS.")
    ExitCode = 1
    return
}

function aspellTTSUrl(text, tl,    narrator) {
    return spellTTSUrl(text, tl)
}

function hunspellTTSUrl(text, tl,    narrator) {
    return spellTTSUrl(text, tl)
}

function spellWebTranslateUrl(uri, sl, tl, hl) {
    e("[ERROR] Spell checker does not support web translation.")
    ExitCode = 1
    return
}

function aspellWebTranslateUrl(uri, sl, tl, hl) {
    return spellWebTranslateUrl(uri, sl, tl, hl)
}

function hunspellWebTranslateUrl(uri, sl, tl, hl) {
    return spellWebTranslateUrl(uri, sl, tl, hl)
}