Megrez // Fix an issue that update() can ruin a span unit object.

This commit is contained in:
ShikiSuen 2023-03-06 21:47:57 +08:00
parent ed84c613a0
commit f8abcd59e4
4 changed files with 28 additions and 6 deletions

View File

@ -291,7 +291,7 @@ extension Megrez.Compositor {
//
if unigrams.isEmpty {
if theNode.keyArray.count == 1 { continue }
spans[position].nodes.removeAll { $0 == theNode }
spans[position].nullify(node: theNode)
} else {
theNode.syncingUnigrams(from: unigrams)
}

View File

@ -24,10 +24,7 @@ extension Megrez.Compositor {
/// 0
public func clear() {
nodes.removeAll()
for _ in 0 ..< maxSpanLength {
nodes.append(nil)
}
nodes = .init(repeating: nil, count: maxSpanLength)
maxLength = 0
}
@ -43,6 +40,18 @@ extension Megrez.Compositor {
return true
}
///
/// - Remark: Swift C#
///
///
/// - Parameter node:
public func nullify(node givenNode: Node) {
nodes.enumerated().forEach { index, theNode in
guard theNode == givenNode else { return }
nodes[index] = nil
}
}
///
/// - Parameter length:
/// - Returns:
@ -51,12 +60,14 @@ extension Megrez.Compositor {
return false
}
for i in length ... maxSpanLength {
guard (0 ..< nodes.count).contains(i - 1) else { continue } //
nodes[i - 1] = nil
}
maxLength = 0
guard length > 1 else { return false }
let maxR = length - 2
for i in 0 ... maxR {
guard (0 ..< nodes.count).contains(maxR - i) else { continue } //
if nodes[maxR - i] == nil { continue }
maxLength = maxR - i + 1
break

View File

@ -40,7 +40,10 @@ class SimpleLM: LangModelProtocol {
func trim(key: String, value: String) {
guard var arr = mutDatabase[key] else { return }
arr = arr.compactMap { $0.value == value ? nil : $0 }
guard !arr.isEmpty else { return }
guard !arr.isEmpty else {
mutDatabase[key] = nil
return
}
mutDatabase[key] = arr
}
}

View File

@ -537,5 +537,13 @@ final class MegrezTests: XCTestCase {
let newResult = compositor.walk().0.values.joined()
print(newResult)
XCTAssertEqual([oldResult, newResult], ["年中獎金", "年終獎金"])
compositor.cursor = 4
compositor.dropKey(direction: .rear)
compositor.dropKey(direction: .rear)
theLM.trim(key: "nian2zhong1", value: "年終")
compositor.update(updateExisting: true)
let newResult2 = compositor.walk().0.values
print(newResult2)
XCTAssertEqual(newResult2, ["", ""])
}
}