56547
|
1 |
/* Title: Tools/jEdit/src/spell_checker.scala
|
|
2 |
Author: Makarius
|
|
3 |
|
|
4 |
Spell-checker based on JOrtho (see http://sourceforge.net/projects/jortho).
|
|
5 |
*/
|
|
6 |
|
|
7 |
package isabelle.jedit
|
|
8 |
|
|
9 |
|
|
10 |
import isabelle._
|
|
11 |
|
|
12 |
import java.lang.Class
|
|
13 |
import java.net.URL
|
56549
|
14 |
import java.util.Locale
|
|
15 |
import java.text.BreakIterator
|
|
16 |
|
|
17 |
import scala.collection.mutable
|
56547
|
18 |
|
|
19 |
|
|
20 |
object Spell_Checker
|
|
21 |
{
|
|
22 |
def known_dictionaries: List[String] =
|
|
23 |
space_explode(',', Isabelle_System.getenv_strict("JORTHO_DICTIONARIES"))
|
|
24 |
|
56549
|
25 |
def apply(lang: String): Spell_Checker =
|
|
26 |
if (known_dictionaries.contains(lang))
|
56547
|
27 |
new Spell_Checker(
|
56549
|
28 |
lang, Locale.forLanguageTag(lang),
|
|
29 |
classOf[com.inet.jortho.SpellChecker].getResource("dictionary_" + lang + ".ortho"))
|
|
30 |
else error("Unknown spell-checker dictionary for " + quote(lang))
|
56547
|
31 |
|
56549
|
32 |
def apply(lang: String, locale: Locale, dict: URL): Spell_Checker =
|
|
33 |
new Spell_Checker(lang, locale, dict)
|
56547
|
34 |
}
|
|
35 |
|
56549
|
36 |
class Spell_Checker private(lang: String, locale: Locale, dict: URL)
|
56547
|
37 |
{
|
56549
|
38 |
override def toString: String = "Spell_Checker(" + lang + ")"
|
56547
|
39 |
|
|
40 |
private val dictionary =
|
|
41 |
{
|
|
42 |
val factory_class = Class.forName("com.inet.jortho.DictionaryFactory")
|
|
43 |
val factory_cons = factory_class.getConstructor()
|
|
44 |
factory_cons.setAccessible(true)
|
|
45 |
val factory = factory_cons.newInstance()
|
|
46 |
|
|
47 |
val load_word_list = factory_class.getDeclaredMethod("loadWordList", classOf[URL])
|
|
48 |
load_word_list.setAccessible(true)
|
|
49 |
load_word_list.invoke(factory, dict)
|
|
50 |
|
|
51 |
val create = factory_class.getDeclaredMethod("create")
|
|
52 |
create.setAccessible(true)
|
|
53 |
create.invoke(factory)
|
|
54 |
}
|
|
55 |
|
|
56 |
def load(file_name: String)
|
|
57 |
{
|
|
58 |
val m = dictionary.getClass.getDeclaredMethod("load", classOf[String])
|
|
59 |
m.setAccessible(true)
|
|
60 |
m.invoke(dictionary, file_name)
|
|
61 |
}
|
|
62 |
|
|
63 |
def save(file_name: String)
|
|
64 |
{
|
|
65 |
val m = dictionary.getClass.getDeclaredMethod("save", classOf[String])
|
|
66 |
m.setAccessible(true)
|
|
67 |
m.invoke(dictionary, file_name)
|
|
68 |
}
|
|
69 |
|
|
70 |
def add(word: String)
|
|
71 |
{
|
|
72 |
val m = dictionary.getClass.getDeclaredMethod("add", classOf[String])
|
|
73 |
m.setAccessible(true)
|
|
74 |
m.invoke(dictionary, word)
|
|
75 |
}
|
|
76 |
|
56552
|
77 |
def contains(word: String): Boolean =
|
56547
|
78 |
{
|
|
79 |
val m = dictionary.getClass.getSuperclass.getDeclaredMethod("exist", classOf[String])
|
|
80 |
m.setAccessible(true)
|
|
81 |
m.invoke(dictionary, word).asInstanceOf[java.lang.Boolean].booleanValue
|
|
82 |
}
|
|
83 |
|
56552
|
84 |
def check(word: String): Boolean =
|
|
85 |
contains(word) ||
|
|
86 |
Library.is_all_caps(word) && contains(Library.lowercase(word, locale)) ||
|
|
87 |
Library.is_capitalized(word) &&
|
|
88 |
(contains(Library.lowercase(word, locale)) || contains(Library.uppercase(word, locale)))
|
|
89 |
|
56547
|
90 |
def complete(word: String): List[String] =
|
|
91 |
{
|
|
92 |
val m = dictionary.getClass.getSuperclass.
|
|
93 |
getDeclaredMethod("searchSuggestions", classOf[String])
|
|
94 |
m.setAccessible(true)
|
|
95 |
m.invoke(dictionary, word).asInstanceOf[java.util.List[AnyRef]].toArray.toList.map(_.toString)
|
|
96 |
}
|
56549
|
97 |
|
|
98 |
def bad_words(text: String): List[Text.Range] =
|
|
99 |
{
|
|
100 |
val result = new mutable.ListBuffer[Text.Range]
|
|
101 |
|
|
102 |
val it = BreakIterator.getWordInstance(locale)
|
|
103 |
it.setText(text)
|
|
104 |
|
|
105 |
var i = 0
|
|
106 |
var j = it.next
|
|
107 |
while (j != BreakIterator.DONE) {
|
|
108 |
val word = text.substring(i, j)
|
|
109 |
if (word.length >= 2 && Character.isLetter(word(0)) && !check(word))
|
|
110 |
result += Text.Range(i, j)
|
|
111 |
i = j
|
|
112 |
j = it.next
|
|
113 |
}
|
|
114 |
result.toList
|
|
115 |
}
|
56547
|
116 |
}
|
|
117 |
|