author | paulson <lp15@cam.ac.uk> |
Mon, 06 May 2024 14:39:33 +0100 | |
changeset 80175 | 200107cdd3ac |
parent 78613 | 60561d28569b |
permissions | -rw-r--r-- |
65367 | 1 |
/* Title: Pure/Tools/spell_checker.scala |
65139 | 2 |
Author: Makarius |
3 |
||
4 |
Spell checker with completion, based on JOrtho (see |
|
68224 | 5 |
https://sourceforge.net/projects/jortho). |
65139 | 6 |
*/ |
7 |
||
8 |
package isabelle |
|
9 |
||
10 |
||
11 |
import java.lang.Class |
|
73909 | 12 |
import java.util.{List => JList} |
65139 | 13 |
|
14 |
import scala.collection.mutable |
|
15 |
import scala.annotation.tailrec |
|
16 |
import scala.collection.immutable.SortedMap |
|
17 |
||
18 |
||
75393 | 19 |
object Spell_Checker { |
65139 | 20 |
/* words within text */ |
21 |
||
75393 | 22 |
def marked_words( |
23 |
base: Text.Offset, |
|
24 |
text: String, |
|
25 |
mark: Text.Info[String] => Boolean |
|
26 |
) : List[Text.Info[String]] = { |
|
65139 | 27 |
val result = new mutable.ListBuffer[Text.Info[String]] |
28 |
var offset = 0 |
|
29 |
||
30 |
def apostrophe(c: Int): Boolean = |
|
31 |
c == '\'' && (offset + 1 == text.length || text(offset + 1) != '\'') |
|
32 |
||
75393 | 33 |
@tailrec def scan(pred: Int => Boolean): Unit = { |
65139 | 34 |
if (offset < text.length) { |
35 |
val c = text.codePointAt(offset) |
|
36 |
if (pred(c)) { |
|
37 |
offset += Character.charCount(c) |
|
38 |
scan(pred) |
|
39 |
} |
|
40 |
} |
|
41 |
} |
|
42 |
||
43 |
while (offset < text.length) { |
|
44 |
scan(c => !Character.isLetter(c)) |
|
45 |
val start = offset |
|
46 |
scan(c => Character.isLetterOrDigit(c) || apostrophe(c)) |
|
47 |
val stop = offset |
|
48 |
if (stop - start >= 2) { |
|
49 |
val info = Text.Info(Text.Range(base + start, base + stop), text.substring(start, stop)) |
|
50 |
if (mark(info)) result += info |
|
51 |
} |
|
52 |
} |
|
53 |
result.toList |
|
54 |
} |
|
55 |
||
75393 | 56 |
def current_word(rendering: Rendering, range: Text.Range): Option[Text.Info[String]] = { |
66116 | 57 |
for { |
58 |
spell_range <- rendering.spell_checker_point(range) |
|
72856 | 59 |
text <- rendering.get_text(spell_range) |
66116 | 60 |
info <- marked_words(spell_range.start, text, info => info.range.overlaps(range)).headOption |
61 |
} yield info |
|
62 |
} |
|
63 |
||
65139 | 64 |
|
65 |
/* dictionaries */ |
|
66 |
||
75393 | 67 |
class Dictionary private[Spell_Checker](val path: Path) { |
71601 | 68 |
val lang: String = path.drop_ext.file_name |
69 |
val user_path: Path = Path.explode("$ISABELLE_HOME_USER/dictionaries") + Path.basic(lang) |
|
65139 | 70 |
override def toString: String = lang |
71 |
} |
|
72 |
||
75393 | 73 |
private object Decl { |
65139 | 74 |
def apply(name: String, include: Boolean): String = |
75 |
if (include) name else "-" + name |
|
76 |
||
75393 | 77 |
def unapply(decl: String): Option[(String, Boolean)] = { |
65139 | 78 |
val decl1 = decl.trim |
79 |
if (decl1 == "" || decl1.startsWith("#")) None |
|
80 |
else |
|
81 |
Library.try_unprefix("-", decl1.trim) match { |
|
82 |
case None => Some((decl1, true)) |
|
83 |
case Some(decl2) => Some((decl2, false)) |
|
84 |
} |
|
85 |
} |
|
86 |
} |
|
87 |
||
76492
e228be7cd375
clarified GUI.Selector, with support for separator as pseudo-entry;
wenzelm
parents:
75393
diff
changeset
|
88 |
lazy val dictionaries: List[Dictionary] = |
65139 | 89 |
for { |
90 |
path <- Path.split(Isabelle_System.getenv("JORTHO_DICTIONARIES")) |
|
91 |
if path.is_file |
|
92 |
} yield new Dictionary(path) |
|
93 |
||
76492
e228be7cd375
clarified GUI.Selector, with support for separator as pseudo-entry;
wenzelm
parents:
75393
diff
changeset
|
94 |
def get_dictionary(lang: String): Option[Dictionary] = dictionaries.find(_.lang == lang) |
e228be7cd375
clarified GUI.Selector, with support for separator as pseudo-entry;
wenzelm
parents:
75393
diff
changeset
|
95 |
|
65139 | 96 |
|
97 |
/* create spell checker */ |
|
98 |
||
99 |
def apply(dictionary: Dictionary): Spell_Checker = new Spell_Checker(dictionary) |
|
100 |
||
101 |
private sealed case class Update(include: Boolean, permanent: Boolean) |
|
102 |
} |
|
103 |
||
104 |
||
75393 | 105 |
class Spell_Checker private(dictionary: Spell_Checker.Dictionary) { |
65139 | 106 |
override def toString: String = dictionary.toString |
107 |
||
108 |
||
109 |
/* main dictionary content */ |
|
110 |
||
111 |
private var dict = new Object |
|
112 |
private var updates = SortedMap.empty[String, Spell_Checker.Update] |
|
113 |
||
114 |
private def included_iterator(): Iterator[String] = |
|
115 |
for { |
|
116 |
(word, upd) <- updates.iterator |
|
117 |
if upd.include |
|
118 |
} yield word |
|
119 |
||
120 |
private def excluded(word: String): Boolean = |
|
121 |
updates.get(word) match { |
|
122 |
case Some(upd) => !upd.include |
|
123 |
case None => false |
|
124 |
} |
|
125 |
||
75393 | 126 |
private def load(): Unit = { |
65139 | 127 |
val main_dictionary = split_lines(File.read_gzip(dictionary.path)) |
128 |
||
129 |
val permanent_updates = |
|
130 |
if (dictionary.user_path.is_file) |
|
131 |
for { |
|
78592 | 132 |
case Spell_Checker.Decl(word, include) <- split_lines(File.read(dictionary.user_path)) |
65139 | 133 |
} yield (word, Spell_Checker.Update(include, true)) |
134 |
else Nil |
|
135 |
||
136 |
updates = |
|
137 |
updates -- (for ((name, upd) <- updates.iterator; if upd.permanent) yield name) ++ |
|
138 |
permanent_updates |
|
139 |
||
140 |
val factory_class = Class.forName("com.inet.jortho.DictionaryFactory") |
|
72973 | 141 |
val factory = Untyped.constructor(factory_class).newInstance() |
65139 | 142 |
|
143 |
val add = Untyped.method(factory_class, "add", classOf[String]) |
|
144 |
||
145 |
for { |
|
146 |
word <- main_dictionary.iterator ++ included_iterator() |
|
147 |
if !excluded(word) |
|
148 |
} add.invoke(factory, word) |
|
149 |
||
150 |
dict = Untyped.method(factory_class, "create").invoke(factory) |
|
151 |
} |
|
152 |
load() |
|
153 |
||
75393 | 154 |
private def save(): Unit = { |
65139 | 155 |
val permanent_decls = |
156 |
(for { |
|
157 |
(word, upd) <- updates.iterator |
|
158 |
if upd.permanent |
|
159 |
} yield Spell_Checker.Decl(word, upd.include)).toList |
|
160 |
||
161 |
if (permanent_decls.nonEmpty || dictionary.user_path.is_file) { |
|
162 |
val header = """# User updates for spell-checker dictionary |
|
163 |
# |
|
164 |
# * each line contains at most one word |
|
165 |
# * extra blanks are ignored |
|
166 |
# * lines starting with "#" are stripped |
|
167 |
# * lines starting with "-" indicate excluded words |
|
168 |
# |
|
169 |
#:mode=text:encoding=UTF-8: |
|
170 |
||
171 |
""" |
|
72375 | 172 |
Isabelle_System.make_directory(dictionary.user_path.expand.dir) |
65139 | 173 |
File.write(dictionary.user_path, header + cat_lines(permanent_decls)) |
174 |
} |
|
175 |
} |
|
176 |
||
75393 | 177 |
def update(word: String, include: Boolean, permanent: Boolean): Unit = { |
65139 | 178 |
updates += (word -> Spell_Checker.Update(include, permanent)) |
179 |
||
180 |
if (include) { |
|
181 |
if (permanent) save() |
|
182 |
Untyped.method(dict.getClass, "add", classOf[String]).invoke(dict, word) |
|
183 |
} |
|
184 |
else { save(); load() } |
|
185 |
} |
|
186 |
||
75393 | 187 |
def reset(): Unit = { |
65139 | 188 |
updates = SortedMap.empty |
189 |
load() |
|
190 |
} |
|
191 |
||
192 |
def reset_enabled(): Int = |
|
71383 | 193 |
updates.valuesIterator.count(upd => !upd.permanent) |
65139 | 194 |
|
195 |
||
196 |
/* check known words */ |
|
197 |
||
198 |
def contains(word: String): Boolean = |
|
199 |
Untyped.method(dict.getClass.getSuperclass, "exist", classOf[String]). |
|
200 |
invoke(dict, word).asInstanceOf[java.lang.Boolean].booleanValue |
|
201 |
||
202 |
def check(word: String): Boolean = |
|
203 |
word match { |
|
78613 | 204 |
case Word.Case(c) if c != Word.Case.lowercase => |
65139 | 205 |
contains(word) || contains(Word.lowercase(word)) |
206 |
case _ => |
|
207 |
contains(word) |
|
208 |
} |
|
209 |
||
210 |
def marked_words(base: Text.Offset, text: String): List[Text.Info[String]] = |
|
211 |
Spell_Checker.marked_words(base, text, info => !check(info.info)) |
|
212 |
||
213 |
||
66117 | 214 |
/* completion: suggestions for unknown words */ |
65139 | 215 |
|
75393 | 216 |
private def suggestions(word: String): Option[List[String]] = { |
65139 | 217 |
val res = |
218 |
Untyped.method(dict.getClass.getSuperclass, "searchSuggestions", classOf[String]). |
|
73909 | 219 |
invoke(dict, word).asInstanceOf[JList[AnyRef]].toArray.toList.map(_.toString) |
65139 | 220 |
if (res.isEmpty) None else Some(res) |
221 |
} |
|
222 |
||
223 |
def complete(word: String): List[String] = |
|
224 |
if (check(word)) Nil |
|
225 |
else { |
|
226 |
val word_case = Word.Case.unapply(word) |
|
227 |
def recover_case(s: String) = |
|
228 |
word_case match { |
|
229 |
case Some(c) => Word.Case(c, s) |
|
230 |
case None => s |
|
231 |
} |
|
232 |
val result = |
|
233 |
word_case match { |
|
78613 | 234 |
case Some(c) if c != Word.Case.lowercase => |
65139 | 235 |
suggestions(word) orElse suggestions(Word.lowercase(word)) |
236 |
case _ => |
|
237 |
suggestions(word) |
|
238 |
} |
|
239 |
result.getOrElse(Nil).map(recover_case) |
|
240 |
} |
|
241 |
||
75393 | 242 |
def completion(rendering: Rendering, caret: Text.Offset): Option[Completion.Result] = { |
66117 | 243 |
val caret_range = rendering.before_caret_range(caret) |
244 |
for { |
|
245 |
word <- Spell_Checker.current_word(rendering, caret_range) |
|
246 |
words = complete(word.info) |
|
247 |
if words.nonEmpty |
|
248 |
descr = "(from dictionary " + quote(dictionary.toString) + ")" |
|
249 |
items = |
|
250 |
words.map(w => Completion.Item(word.range, word.info, "", List(w, descr), w, 0, false)) |
|
251 |
} yield Completion.Result(word.range, word.info, false, items) |
|
252 |
} |
|
65139 | 253 |
} |
254 |
||
75393 | 255 |
class Spell_Checker_Variable { |
65139 | 256 |
private val no_spell_checker: (String, Option[Spell_Checker]) = ("", None) |
257 |
private var current_spell_checker = no_spell_checker |
|
258 |
||
259 |
def get: Option[Spell_Checker] = synchronized { current_spell_checker._2 } |
|
260 |
||
261 |
def update(options: Options): Unit = synchronized { |
|
262 |
if (options.bool("spell_checker")) { |
|
263 |
val lang = options.string("spell_checker_dictionary") |
|
264 |
if (current_spell_checker._1 != lang) { |
|
76492
e228be7cd375
clarified GUI.Selector, with support for separator as pseudo-entry;
wenzelm
parents:
75393
diff
changeset
|
265 |
Spell_Checker.get_dictionary(lang) match { |
65139 | 266 |
case Some(dictionary) => |
267 |
val spell_checker = |
|
268 |
Exn.capture { Spell_Checker(dictionary) } match { |
|
269 |
case Exn.Res(spell_checker) => Some(spell_checker) |
|
270 |
case Exn.Exn(_) => None |
|
271 |
} |
|
272 |
current_spell_checker = (lang, spell_checker) |
|
273 |
case None => |
|
274 |
current_spell_checker = no_spell_checker |
|
275 |
} |
|
276 |
} |
|
277 |
} |
|
278 |
else current_spell_checker = no_spell_checker |
|
279 |
} |
|
280 |
} |