Skip to content

Commit

Permalink
Add extra security procedures to insertKey(). (#90)
Browse files Browse the repository at this point in the history
- This also turns Span into struct.
  • Loading branch information
ShikiSuen authored Oct 24, 2022
1 parent 9bc7ef9 commit 8a31f49
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
19 changes: 15 additions & 4 deletions Sources/Megrez/1_Compositor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,14 @@ extension Megrez {
@discardableResult public mutating func insertKey(_ key: String) -> Bool {
guard !key.isEmpty, key != separator, langModel.hasUnigramsFor(key: key) else { return false }
keys.insert(key, at: cursor)
let gridBackup = spans
resizeGrid(at: cursor, do: .expand)
update()
let nodesInserted = update()
// 用來在 langModel.hasUnigramsFor() 結果不準確的時候防呆、恢復被搞壞的 spans。
if nodesInserted == 0 {
spans = gridBackup
return false
}
cursor += 1 // 游標必須得在執行 update() 之後才可以變動。
return true
}
Expand Down Expand Up @@ -219,7 +225,7 @@ extension Megrez.Compositor {
/// (XXXXXXX? <-被砍爛的節點
/// ```
/// - Parameter location: 給定的幅位座標。
func dropWreckedNodes(at location: Int) {
mutating func dropWreckedNodes(at location: Int) {
let location = max(min(location, spans.count), 0) // 防呆
guard !spans.isEmpty else { return }
let affectedLength = Megrez.Compositor.maxSpanLength - 1
Expand All @@ -230,7 +236,7 @@ extension Megrez.Compositor {
}
}

@discardableResult func insertNode(_ node: Node, at location: Int) -> Bool {
@discardableResult mutating func insertNode(_ node: Node, at location: Int) -> Bool {
let location = max(min(location, spans.count - 1), 0) // 防呆
spans[location].append(node: node)
return true
Expand All @@ -254,9 +260,12 @@ extension Megrez.Compositor {
return key == node.key
}

func update() {
/// 根據當前狀況更新整個組字器的節點文脈。
/// - Returns: 新增了多少節點。
@discardableResult mutating func update() -> Int {
let maxSpanLength = Megrez.Compositor.maxSpanLength
let range = max(0, cursor - maxSpanLength)..<min(cursor + maxSpanLength, keys.count)
var nodesInserted = 0
for position in range {
for theLength in 1...min(maxSpanLength, range.upperBound - position) {
let jointKeyArray = getJointKeyArray(range: position..<(position + theLength))
Expand All @@ -268,8 +277,10 @@ extension Megrez.Compositor {
.init(keyArray: jointKeyArray, spanLength: theLength, unigrams: unigrams, keySeparator: separator),
at: position
)
nodesInserted += 1
}
}
return nodesInserted
}

mutating func updateCursorJumpingTables(_ walkedNodes: [Node]) {
Expand Down
8 changes: 4 additions & 4 deletions Sources/Megrez/4_Span.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@

extension Megrez.Compositor {
/// 幅位乃指一組共享起點的節點。
public class Span {
public struct Span {
private var nodes: [Node?] = []
public private(set) var maxLength = 0
private var maxSpanLength: Int { Megrez.Compositor.maxSpanLength }
public init() {
clear()
}

public func clear() {
public mutating func clear() {
nodes.removeAll()
for _ in 0..<maxSpanLength {
nodes.append(nil)
Expand All @@ -24,7 +24,7 @@ extension Megrez.Compositor {
/// 往該幅位塞入一個節點。
/// - Parameter node: 要塞入的節點。
/// - Returns: 該操作是否成功執行。
@discardableResult public func append(node: Node) -> Bool {
@discardableResult public mutating func append(node: Node) -> Bool {
guard (1...maxSpanLength).contains(node.spanLength) else {
return false
}
Expand All @@ -36,7 +36,7 @@ extension Megrez.Compositor {
/// 丟掉任何不小於給定幅位長度的節點。
/// - Parameter length: 給定的幅位長度。
/// - Returns: 該操作是否成功執行。
@discardableResult public func dropNodesOfOrBeyond(length: Int) -> Bool {
@discardableResult public mutating func dropNodesOfOrBeyond(length: Int) -> Bool {
guard (1...maxSpanLength).contains(length) else {
return false
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/MegrezTests/MegrezTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import XCTest
final class MegrezTests: XCTestCase {
func testSpan() throws {
let langModel = SimpleLM(input: strSampleData)
let span = Megrez.Compositor.Span()
var span = Megrez.Compositor.Span()
let n1 = Megrez.Compositor.Node(keyArray: ["gao1"], spanLength: 1, unigrams: langModel.unigramsFor(key: "gao1"))
let n3 = Megrez.Compositor.Node(
keyArray: ["gao1ke1ji4"], spanLength: 3, unigrams: langModel.unigramsFor(key: "gao1ke1ji4")
Expand Down

0 comments on commit 8a31f49

Please sign in to comment.