| 1 | #!/bin/sh |
| 2 | # affix compressor utility for Hunspell |
| 3 | # 2008 (c) László Németh, version 0.3 |
| 4 | # usage: affixcompress sorted_word_list_file [max_affix_rules] |
| 5 | case $# in |
| 6 | 0) echo \ |
| 7 | "affixcompress - compress a huge sorted word list to Hunspell format |
| 8 | Usage: |
| 9 | |
| 10 | LC_ALL=C sort word_list >sorted_word_list |
| 11 | affixcompress sorted_word_list [max_affix_rules] |
| 12 | |
| 13 | Default value of max_affix_rules = 5000 |
| 14 | |
| 15 | Note: output may need manually added affix parameters (SET character_encoding, |
| 16 | TRY suggestion_characters etc., see man(4) hunspell)" |
| 17 | exit 0;; |
| 18 | esac |
| 19 | |
| 20 | MAXAFFIX=${2:-5000} |
| 21 | |
| 22 | # profiling |
| 23 | #AWK="pgawk --profile" |
| 24 | AWK="awk" |
| 25 | if which gawk; then |
| 26 | AWK="gawk" |
| 27 | fi |
| 28 | |
| 29 | rm -f $1.aff $1.dic |
| 30 | cat $1 | $AWK ' |
| 31 | { |
| 32 | # calculate frequent suffixes |
| 33 | A[$1] = 1 |
| 34 | len = length($1) |
| 35 | if (len > 2) { |
| 36 | # print $1, substr($1, 1, len - 1), substr($1, len, 1) >"/dev/stderr" |
| 37 | B[substr($1, 1, len - 1)] = substr($1, len, 1); |
| 38 | } |
| 39 | for(i = 2; i < len; i++) { |
| 40 | r = substr($1, 1, i) |
| 41 | if (i == 2) { |
| 42 | if (prev != r) { |
| 43 | delete A |
| 44 | delete B |
| 45 | print "Deleted roots: ", prev > "/dev/stderr" |
| 46 | A[$1] = 1 |
| 47 | } |
| 48 | prev = r |
| 49 | } |
| 50 | if (A[r]) { |
| 51 | # print $1 ": " r " és "substr($1, i + 1, len - i + 1) >"/dev/stderr" |
| 52 | sfx[substr($1, i + 1, len - i + 1)]++ |
| 53 | } else if (B[r] && B[r] != substr($1, i + 1, 1)) { |
| 54 | r2 = substr($1, i + 1, len - i + 1) |
| 55 | sfy[r2,B[r]]++ |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | END { |
| 60 | for (i in sfx) print i, 0, sfx[i] |
| 61 | for (i in sfy) print i, sfy[i] |
| 62 | } |
| 63 | ' | tr '\034' ' ' >affixcompress0.tmp |
| 64 | sort -rnk 3 affixcompress0.tmp | $AWK '$3 >= 1{print $0}' | |
| 65 | head -$MAXAFFIX >affixcompress1.tmp |
| 66 | cat affixcompress1.tmp | |
| 67 | $AWK ' |
| 68 | function potential_roots() { |
| 69 | # potential roots with most frequent suffixes |
| 70 | for(word in W) if (W[word]==1) { |
| 71 | print word >"word" |
| 72 | len = length(word); |
| 73 | for(i = 2; i < len; i++) { |
| 74 | root = substr(word, 1, i) |
| 75 | suff = substr(word, i + 1, len - i + 1) |
| 76 | if ((W[root]!="") && (sfxfr[suff] > 100)) C[root]++ |
| 77 | if (sfz[suff]) { |
| 78 | l = split(sfz[suff], a) |
| 79 | for (k=1; k <= l; k++) if ((W[root a[k]]!="") && (sfyfr[root a[k]] > 100)) { |
| 80 | C[root a[k]]++ |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | # calculate roots |
| 87 | for(word in W) if (W[word]==1) { |
| 88 | print word >"word2" |
| 89 | len = length(word); |
| 90 | z = 0 |
| 91 | # choose most frequent root (maybe the original word) |
| 92 | max = C[word] |
| 93 | maxword = word |
| 94 | maxsuff = 0 |
| 95 | for(i = 2; i < len; i++) { |
| 96 | root = substr(word, 1, i) |
| 97 | suff = substr(word, i + 1, len - i + 1) |
| 98 | if ((sfx[suff] != "") && (C[root] > max)) { |
| 99 | max = C[root] |
| 100 | maxword = root |
| 101 | maxsuff = sfx[suff] |
| 102 | } |
| 103 | if (sfz[suff] != "") { |
| 104 | l = split(sfz[suff], a) |
| 105 | for (k=1; k <= l; k++) if (C[root a[k]] > max) { |
| 106 | max = C[root a[k]] |
| 107 | maxword = root a[k] |
| 108 | maxsuff = sfy[suff,a[k]] |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | if (max > 0) { |
| 113 | if (maxsuff > 0) print maxword, maxsuff; else print maxword |
| 114 | A[maxword]++ |
| 115 | z=1 |
| 116 | } else { |
| 117 | for(i = 2; i < len; i++) { |
| 118 | root = substr(word, 1, i) |
| 119 | suff = substr(word, i + 1, len - i + 1) |
| 120 | if ((A[root] > 0) && sfx[suff]!="") { |
| 121 | print root, sfx[suff] |
| 122 | z = 1 |
| 123 | break |
| 124 | } |
| 125 | if (sfz[suff]) { |
| 126 | l = split(sfz[suff], a) |
| 127 | for (k=1; k <= l; k++) if (A[root a[k]]!="") { |
| 128 | print root a[k], sfy[suff,a[k]] |
| 129 | z = 1 |
| 130 | break |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | if (z == 0) { |
| 136 | print word |
| 137 | A[word]++ |
| 138 | } |
| 139 | } |
| 140 | delete A |
| 141 | delete C |
| 142 | } |
| 143 | FILENAME == "-" { |
| 144 | if ($2 == 0) { |
| 145 | sfx[$1] = NR |
| 146 | sfxfr[$1] = $3 |
| 147 | } else { |
| 148 | sfy[$1,$2] = NR |
| 149 | sfyfr[$1,$2] = $3 |
| 150 | sfz[$1] = sfz[$1] " " $2 |
| 151 | } |
| 152 | maxsuf = NR |
| 153 | next |
| 154 | } |
| 155 | { |
| 156 | cap = substr($1, 1, 3) |
| 157 | if (cap != prev) { |
| 158 | potential_roots() |
| 159 | delete W |
| 160 | print "Deleted class:", prev > "/dev/stderr" |
| 161 | } |
| 162 | prev = cap |
| 163 | W[$1] = 1 |
| 164 | } |
| 165 | END { |
| 166 | potential_roots() |
| 167 | # write out frequent suffixes |
| 168 | out=FILENAME ".aff" |
| 169 | print "FLAG num" >out |
| 170 | for (i in sfx) if (sfx[i] > 0) { |
| 171 | print "SFX", sfx[i], "Y 1" >out |
| 172 | print "SFX", sfx[i], "0", i, "." >out |
| 173 | } |
| 174 | for (i in sfy) if (sfy[i] > 0) { |
| 175 | print "SFX", sfy[i], "Y 1" >out |
| 176 | split(i, c, "\034"); |
| 177 | print "SFX", sfy[i], c[2], c[1], c[2] >out |
| 178 | } |
| 179 | } |
| 180 | ' - $1 >affixcompress2.tmp |
| 181 | sort -nk 2 affixcompress2.tmp >affixcompress3.tmp |
| 182 | cat affixcompress3.tmp | $AWK -v out="$1.dic" ' |
| 183 | { |
| 184 | if (A[$1]=="") A[$1]=$2; |
| 185 | else if ($2!="") A[$1] = A[$1] "," $2 |
| 186 | } |
| 187 | END { |
| 188 | for (i in A) n++ |
| 189 | print n >out |
| 190 | for (i in A) { |
| 191 | if (A[i]=="") print i |
| 192 | else print i "/" A[i] |
| 193 | } |
| 194 | } |
| 195 | ' | sort >>$1.dic |