Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e98103b1f8 | ||
|
|
ec3ee4ba11 |
@@ -15,9 +15,9 @@
|
||||
<key>CFBundlePackageType</key>
|
||||
<string>APPL</string>
|
||||
<key>CFBundleShortVersionString</key>
|
||||
<string>0.14.0</string>
|
||||
<string>0.14.1</string>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>24</string>
|
||||
<string>25</string>
|
||||
<key>InputMethodConnectionName</key>
|
||||
<string>com.autozhuyin.inputmethod.AutoZhuyin_Connection</string>
|
||||
<key>InputMethodServerControllerClass</key>
|
||||
|
||||
@@ -37,7 +37,9 @@ public final class LearnedPreferences {
|
||||
|
||||
public func record(_ keys: String, as mode: InputMode) {
|
||||
let delta = mode == .zhuyin ? 1 : -1
|
||||
weights[keys] = max(-3, min(3, (weights[keys] ?? 0) + delta))
|
||||
let old = weights[keys] ?? 0
|
||||
// A correction against what was learned replaces it, so one Tab is enough to undo it.
|
||||
weights[keys] = old * delta < 0 ? delta : max(-3, min(3, old + delta))
|
||||
onChange(weights)
|
||||
}
|
||||
|
||||
|
||||
@@ -38,6 +38,9 @@ public final class CompositionEngine {
|
||||
/// Set by Tab / a Shift tap while typing: overrides the classifier for the whole
|
||||
/// buffer. It is a correction, learned on commit.
|
||||
public private(set) var forcedMode: InputMode?
|
||||
/// How many keys there were when the user flipped: that word is what gets learned, not
|
||||
/// whatever was typed after it.
|
||||
private var forcedCount = 0
|
||||
/// English mode, toggled by a Shift tap when nothing is being typed. Keys go straight
|
||||
/// to the app (no composition, no underline) until Shift is tapped again.
|
||||
public private(set) var isEnglishMode = false
|
||||
@@ -120,9 +123,10 @@ public final class CompositionEngine {
|
||||
return Features.dot(w, zhWhole) >= Features.dot(w, word) + Features.dot(w, zhRest)
|
||||
}
|
||||
|
||||
/// Whether keys + space also read as a common Chinese syllable (see `held`).
|
||||
/// Whether keys + space also read as a common Chinese syllable (see `held`). Any Dachen
|
||||
/// keys count, not only letters: "0" is ㄢ (安), "5j;" is ㄓㄨㄤ (裝).
|
||||
private func isAmbiguousWord(_ keys: [Character]) -> Bool {
|
||||
guard keys.allSatisfy({ $0.isASCII && $0.isLetter }) else { return false }
|
||||
guard keys.allSatisfy(DachenLayout.isZhuyinKey) else { return false }
|
||||
let p = ZhuyinParser.parse(keys + [" "])
|
||||
guard p.pending == nil, !p.syllables.isEmpty, p.strayTones == 0, p.foreignKeys == 0 else { return false }
|
||||
var best: [Double] = []
|
||||
@@ -430,6 +434,7 @@ public final class CompositionEngine {
|
||||
/// Forces the whole buffer into the other mode (a correction that is learned).
|
||||
private func flip() {
|
||||
forcedMode = (mode == .zhuyin) ? .literal : .zhuyin
|
||||
forcedCount = keys.count
|
||||
}
|
||||
|
||||
private func handleCharacter(_ c: Character) -> KeyResult {
|
||||
@@ -566,7 +571,7 @@ public final class CompositionEngine {
|
||||
rememberPicks(segs)
|
||||
recentText = String((rerankContext + text).suffix(20))
|
||||
if let forced = forcedMode {
|
||||
classifier.learned.record(String(keys), as: forced)
|
||||
classifier.learned.record(String(keys.prefix(forcedCount)), as: forced)
|
||||
}
|
||||
previousMode = segs.last?.mode
|
||||
previousCommitTime = now()
|
||||
|
||||
@@ -487,7 +487,7 @@ final class HeldWordTests: XCTestCase {
|
||||
func type(_ s: String, into e: CompositionEngine) -> String {
|
||||
var out = ""
|
||||
for c in s {
|
||||
let key: KeyInput = c == "\n" ? .enter : .character(c)
|
||||
let key: KeyInput = c == "\n" ? .enter : c == "\t" ? .tab : .character(c)
|
||||
let r = e.handle(key)
|
||||
if let t = r.commit { out += t } else if !r.handled { out.append(c) }
|
||||
}
|
||||
@@ -511,6 +511,24 @@ final class HeldWordTests: XCTestCase {
|
||||
XCTAssertEqual(e.markedText, "up")
|
||||
XCTAssertEqual(type(" the ", into: e), "up the ")
|
||||
}
|
||||
|
||||
/// Words with digits / punctuation keys are held too: "0 " is 安, "5j; " is 裝.
|
||||
func testHeldWordWithDigitKeys() {
|
||||
let learned = LearnedPreferences(weights: ["0": -2]) // "0" once learned as a number
|
||||
let c = Classifier(lexicon: sharedLexicon, english: sharedEnglish, learned: learned, weights: sharedWeights)
|
||||
XCTAssertEqual(type("0 5j; \n", into: CompositionEngine(classifier: c)), "安裝")
|
||||
XCTAssertEqual(type("0 \n", into: CompositionEngine(classifier: c)), "0 ")
|
||||
}
|
||||
|
||||
/// One Tab undoes a wrong learned preference, and only the word flipped is learned.
|
||||
func testTabOverridesLearned() {
|
||||
let learned = LearnedPreferences(weights: ["0": -2])
|
||||
let c = Classifier(lexicon: sharedLexicon, english: sharedEnglish, learned: learned, weights: sharedWeights)
|
||||
XCTAssertEqual(type("0\t 5j; \n", into: CompositionEngine(classifier: c)), "安裝")
|
||||
XCTAssertEqual(learned.weights["0"], 1)
|
||||
XCTAssertNil(learned.weights["0 5j; "])
|
||||
XCTAssertEqual(type("hello 0 5j; \n", into: CompositionEngine(classifier: c)), "hello 安裝")
|
||||
}
|
||||
}
|
||||
|
||||
final class BacktickSymbolTests: XCTestCase {
|
||||
|
||||
@@ -19,7 +19,9 @@ class LearnedPreferences:
|
||||
|
||||
def record(self, keys, mode):
|
||||
delta = 1 if mode == ZHUYIN else -1
|
||||
self.weights[keys] = max(-3, min(3, self.weights.get(keys, 0) + delta))
|
||||
old = self.weights.get(keys, 0)
|
||||
# A correction against what was learned replaces it, so one Tab is enough to undo it.
|
||||
self.weights[keys] = delta if old * delta < 0 else max(-3, min(3, old + delta))
|
||||
self._on_change(self.weights)
|
||||
|
||||
def clear(self):
|
||||
|
||||
@@ -50,6 +50,7 @@ class CompositionEngine:
|
||||
self.now = now
|
||||
self.keys = []
|
||||
self.forced_mode = None
|
||||
self.forced_count = 0
|
||||
# English mode, toggled by a Shift tap when nothing is being typed: keys go straight
|
||||
# to the app (no composition, no underline) until Shift is tapped again.
|
||||
self.english_mode = False
|
||||
@@ -117,8 +118,9 @@ class CompositionEngine:
|
||||
return dot(w, zh_whole) >= dot(w, word) + dot(w, zh_rest)
|
||||
|
||||
def _is_ambiguous_word(self, keys):
|
||||
"""Whether keys + space also read as a common Chinese syllable (see HOLD_MIN_SCORE)."""
|
||||
if not all(k.isascii() and k.isalpha() for k in keys):
|
||||
"""Whether keys + space also read as a common Chinese syllable (see HOLD_MIN_SCORE).
|
||||
Any Dachen keys count, not only letters: "0" is ㄢ (安), "5j;" is ㄓㄨㄤ (裝)."""
|
||||
if not all(bopomofo.is_zhuyin_key(k) for k in keys):
|
||||
return False
|
||||
p = bopomofo.parse(list(keys) + [" "])
|
||||
if p.pending is not None or not p.syllables or p.stray_tones or p.foreign_keys:
|
||||
@@ -365,6 +367,7 @@ class CompositionEngine:
|
||||
def _flip(self):
|
||||
"""Forces the whole buffer into the other mode (a correction that is learned)."""
|
||||
self.forced_mode = LITERAL if self.mode == ZHUYIN else ZHUYIN
|
||||
self.forced_count = len(self.keys) # that word is learned, not what is typed after it
|
||||
|
||||
def wants(self, key):
|
||||
"""Whether handle(key) would consume the key, without changing state."""
|
||||
@@ -567,7 +570,7 @@ class CompositionEngine:
|
||||
self._remember_picks(segs)
|
||||
self._recent_text = (self._rerank_context() + text)[-20:]
|
||||
if self.forced_mode:
|
||||
self.classifier.learned.record("".join(self.keys), self.forced_mode)
|
||||
self.classifier.learned.record("".join(self.keys[:self.forced_count]), self.forced_mode)
|
||||
self._previous_mode = segs[-1][2] if segs else None
|
||||
self._previous_time = self.now()
|
||||
self.reset()
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "自動注音 AutoZhuyin",
|
||||
"version": "0.14.0",
|
||||
"version": "0.14.1",
|
||||
"guid": "{3895C6E3-6D40-4B4B-B4EA-CEEC75382D23}",
|
||||
"locale": "zh-Hant-TW",
|
||||
"fallbackLocale": "zh-TW",
|
||||
|
||||
@@ -340,6 +340,23 @@ class HeldWordTests(unittest.TestCase):
|
||||
self.assertEqual(e.marked_text, "up")
|
||||
self.assertEqual(type_keys(" the ", e), "up the ")
|
||||
|
||||
def _engine(self, learned):
|
||||
return CompositionEngine(Classifier(LEX, ENG, learned, weights=WEIGHTS))
|
||||
|
||||
def test_held_word_with_digit_keys(self):
|
||||
"""Words with digits / punctuation keys are held too: "0 " is 安, "5j; " is 裝."""
|
||||
learned = LearnedPreferences({"0": -2}) # "0" once learned as a number
|
||||
self.assertEqual(type_keys("0 5j; \n", self._engine(learned)), "安裝")
|
||||
self.assertEqual(type_keys("0 \n", self._engine(learned)), "0 ")
|
||||
|
||||
def test_tab_overrides_learned(self):
|
||||
"""One Tab undoes a wrong learned preference, and only the word flipped is learned."""
|
||||
learned = LearnedPreferences({"0": -2})
|
||||
self.assertEqual(type_keys("0\t 5j; \n", self._engine(learned)), "安裝")
|
||||
self.assertEqual(learned.weights["0"], 1)
|
||||
self.assertNotIn("0 5j; ", learned.weights)
|
||||
self.assertEqual(type_keys("hello 0 5j; \n", self._engine(learned)), "hello 安裝")
|
||||
|
||||
|
||||
class BacktickSymbolTests(unittest.TestCase):
|
||||
def test_forced_symbols(self):
|
||||
|
||||
Reference in New Issue
Block a user