Skip to content

Commit ef86acd

Browse files
committed
Improve Candidate and CandidateType
1 parent e1b76b6 commit ef86acd

File tree

6 files changed

+21
-15
lines changed

6 files changed

+21
-15
lines changed

CoreIME/Sources/CoreIME/Candidate.swift

+13-11
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,12 @@ public enum CandidateType: Int {
55
/// Plain text. Examples: iPad, macOS
66
case text
77

8+
/// Note that `Candidate.text.count == 1` not always true
89
case emoji
910

10-
case emojiSequence
11-
12-
/// Note that `text.count == 1` not always true
11+
/// Note that `Candidate.text.count == 1` not always true
1312
case symbol
1413

15-
case symbolSequence
16-
1714
/// macOS Keyboard composed text. Mainly for PunctuationKey.
1815
case compose
1916
}
@@ -127,13 +124,19 @@ public struct Candidate: Hashable {
127124
return self.type == .cantonese
128125
}
129126

127+
/// type != .cantonese
128+
public var isNotCantonese: Bool {
129+
return self.type != .cantonese
130+
}
131+
130132
/// Concatenated by multiple lexicons
131133
public var isCompound: Bool {
132134
return subNotations.isNotEmpty
133135
}
134136

135137
// Equatable
136138
public static func ==(lhs: Candidate, rhs: Candidate) -> Bool {
139+
guard lhs.type == rhs.type else { return false }
137140
if lhs.isCantonese && rhs.isCantonese {
138141
return lhs.text == rhs.text && lhs.romanization == rhs.romanization
139142
} else {
@@ -151,18 +154,15 @@ public struct Candidate: Hashable {
151154
hasher.combine(text)
152155
case .emoji:
153156
hasher.combine(text)
154-
case .emojiSequence:
155-
hasher.combine(text)
156157
case .symbol:
157158
hasher.combine(text)
158-
case .symbolSequence:
159-
hasher.combine(text)
160159
case .compose:
161160
hasher.combine(text)
162161
}
163162
}
164163

165-
static func +(lhs: Candidate, rhs: Candidate) -> Candidate {
164+
static func +(lhs: Candidate, rhs: Candidate) -> Candidate? {
165+
guard lhs.isCantonese && rhs.isCantonese else { return nil }
166166
let newText: String = lhs.text + rhs.text
167167
let newLexiconText: String = lhs.lexiconText + rhs.lexiconText
168168
let newRomanization: String = lhs.romanization + " " + rhs.romanization
@@ -190,7 +190,9 @@ extension Array where Element == Candidate {
190190

191191
/// Returns a new Candidate by concatenating this Candidate sequence.
192192
/// - Returns: Single, concatenated Candidate.
193-
public func joined() -> Candidate {
193+
public func joined() -> Candidate? {
194+
let isNotAllCantonese: Bool = self.contains(where: \.isNotCantonese)
195+
guard !isNotAllCantonese else { return nil }
194196
let text: String = map(\.text).joined()
195197
let lexiconText: String = map(\.lexiconText).joined()
196198
let romanization: String = map(\.romanization).joined(separator: " ")

CoreIME/Sources/CoreIME/Engine.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ public struct Engine {
251251
guard tailCandidates.isNotEmpty else { return [] }
252252
let headCandidates = primary.filter({ $0.input == headText }).prefix(8)
253253
let combines = headCandidates.map({ head -> [Candidate] in
254-
return tailCandidates.map({ head + $0 })
254+
return tailCandidates.compactMap({ head + $0 })
255255
})
256256
return combines.flatMap({ $0 })
257257
}

CoreIME/Sources/CoreIME/Segmentor.swift

+3
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ public struct Segmentor {
138138
}
139139
case 4 where text == "mama":
140140
return mama
141+
case 4 where text == "mami":
142+
return mami
141143
default:
142144
let rawText: String = text.filter({ !$0.isSeparatorOrTone })
143145
let key: Int = rawText.hash
@@ -163,4 +165,5 @@ public struct Segmentor {
163165
private static let letterO: Segmentation = [[SegmentToken(text: "o", origin: "o")]]
164166
private static let letterM: Segmentation = [[SegmentToken(text: "m", origin: "m")]]
165167
private static let mama: Segmentation = [[SegmentToken(text: "ma", origin: "maa"), SegmentToken(text: "ma", origin: "maa")]]
168+
private static let mami: Segmentation = [[SegmentToken(text: "ma", origin: "maa"), SegmentToken(text: "mi", origin: "mi")]]
166169
}

TypeDuck/Candidate/DisplayCandidate.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ struct DisplayCandidate: Hashable {
1616
return candidate.notation?.comments ?? []
1717
case .text:
1818
return []
19-
case .emoji, .symbol, .emojiSequence, .symbolSequence:
19+
case .emoji, .symbol:
2020
var comments: [Comment] = []
2121
let cantoneseText = candidate.lexiconText
2222
if cantoneseText.isNotEmpty {

TypeDuck/TypeDuckInputController.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ final class TypeDuckInputController: IMKInputController {
211211
switch bufferText.first {
212212
case .none:
213213
if AppSettings.isInputMemoryOn && selectedCandidates.isNotEmpty {
214-
let concatenated: Candidate = selectedCandidates.joined()
214+
let concatenated = selectedCandidates.joined()
215215
UserLexicon.handle(concatenated)
216216
}
217217
selectedCandidates = []

TypeDuck/UserLexicon.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ struct UserLexicon {
2525

2626
// MARK: - Handle Candidate
2727

28-
static func handle(_ candidate: Candidate) {
28+
static func handle(_ candidate: Candidate?) {
29+
guard let candidate else { return }
2930
let word: String = candidate.lexiconText
3031
let romanization: String = candidate.romanization
3132
let id: Int64 = Int64((word + romanization).hash)

0 commit comments

Comments
 (0)