mirror of
https://github.com/mii443/AzooKeyKanaKanjiConverter.git
synced 2025-12-03 02:58:27 +00:00
refactor: add semantic api
This commit is contained in:
@@ -31,12 +31,7 @@ extension Kana2Kanji {
|
||||
debug("kana2lattice_changed", inputData, counts, previousResult.inputData, count, commonCount)
|
||||
|
||||
// (1)
|
||||
var lattice = Lattice(nodes: previousResult.lattice.nodes.prefix(commonCount).map {(nodes: [LatticeNode]) in
|
||||
nodes.filter {$0.inputRange.endIndex <= commonCount}
|
||||
})
|
||||
while lattice.nodes.last?.isEmpty ?? false {
|
||||
lattice.nodes.removeLast()
|
||||
}
|
||||
var lattice = previousResult.lattice.prefix(commonCount)
|
||||
|
||||
let terminalNodes: Lattice
|
||||
if counts.added == 0 {
|
||||
@@ -86,14 +81,8 @@ extension Kana2Kanji {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
for (index, nodeArray) in addedNodes.nodes.enumerated() where index < lattice.nodes.endIndex {
|
||||
lattice.nodes[index].append(contentsOf: nodeArray)
|
||||
}
|
||||
for nodeArray in addedNodes.nodes.suffix(counts.added) {
|
||||
lattice.nodes.append(nodeArray)
|
||||
}
|
||||
lattice.merge(addedNodes)
|
||||
terminalNodes = addedNodes
|
||||
}
|
||||
|
||||
|
||||
@@ -3,5 +3,26 @@ struct Lattice {
|
||||
self.nodes = []
|
||||
}
|
||||
|
||||
var nodes: [[LatticeNode]]
|
||||
private(set) var nodes: [[LatticeNode]]
|
||||
|
||||
func prefix(_ k: Int) -> Lattice {
|
||||
var lattice = Lattice(nodes: self.nodes.prefix(k).map {(nodes: [LatticeNode]) in
|
||||
nodes.filter {$0.inputRange.endIndex <= k}
|
||||
})
|
||||
while lattice.nodes.last?.isEmpty ?? false {
|
||||
lattice.nodes.removeLast()
|
||||
}
|
||||
return lattice
|
||||
}
|
||||
|
||||
mutating func merge(_ lattice: Lattice) {
|
||||
for (index, nodeArray) in lattice.nodes.enumerated() where index < self.nodes.endIndex {
|
||||
self.nodes[index].append(contentsOf: nodeArray)
|
||||
}
|
||||
if self.nodes.endIndex < lattice.nodes.endIndex {
|
||||
for nodeArray in lattice.nodes[self.nodes.endIndex...] {
|
||||
self.nodes.append(nodeArray)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user