Repo // Megrez v2.0.2 update.

This commit is contained in:
ShikiSuen 2022-08-16 11:10:09 +08:00
parent d0df22b5d4
commit e20f95c4b5
5 changed files with 59 additions and 42 deletions

View File

@ -223,34 +223,6 @@ extension Array where Element == Megrez.Compositor.Node {
}
return counter
}
public func findNode(at cursor: Int, target outCursorPastNode: inout Int) -> Megrez.Compositor.Node? {
guard !isEmpty else { return nil }
let cursor = Swift.max(0, Swift.min(cursor, keys.count))
if cursor == 0, let theFirst = first {
outCursorPastNode = theFirst.spanLength
return theFirst
}
//
if cursor >= keys.count - 1, let theLast = last {
outCursorPastNode = keys.count
return theLast
}
var accumulated = 0
for neta in self {
accumulated += neta.spanLength
if accumulated > cursor {
outCursorPastNode = accumulated
return neta
}
}
//
return nil
}
}
// MARK: - Private Methods

View File

@ -13,7 +13,7 @@ extension Megrez {
///
///
///
public class Compositor {
public struct Compositor {
///
public enum TypingDirection { case front, rear }
///
@ -51,7 +51,7 @@ extension Megrez {
self.separator = separator
}
public func clear() {
public mutating func clear() {
cursor = 0
keys.removeAll()
spans.removeAll()
@ -62,7 +62,7 @@ extension Megrez {
///
/// - Parameter key:
/// - Returns:
@discardableResult public func insertKey(_ key: String) -> Bool {
@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)
resizeGrid(at: cursor, do: .expand)
@ -77,7 +77,7 @@ extension Megrez {
///
/// - Parameter direction:
/// - Returns:
@discardableResult public func dropKey(direction: TypingDirection) -> Bool {
@discardableResult public mutating func dropKey(direction: TypingDirection) -> Bool {
let isBackSpace: Bool = direction == .rear ? true : false
guard cursor != (isBackSpace ? 0 : keys.count) else { return false }
keys.remove(at: cursor - (isBackSpace ? 1 : 0))
@ -90,7 +90,7 @@ extension Megrez {
///
/// - Parameter direction:
/// - Returns:
@discardableResult public func jumpCursorBySpan(to direction: TypingDirection) -> Bool {
@discardableResult public mutating func jumpCursorBySpan(to direction: TypingDirection) -> Bool {
switch direction {
case .front:
if cursor == width { return false }
@ -158,7 +158,7 @@ extension Megrez.Compositor {
/// - Parameters:
/// - location:
/// - action:
func resizeGrid(at location: Int, do action: ResizeBehavior) {
mutating func resizeGrid(at location: Int, do action: ResizeBehavior) {
let location = max(min(location, spans.count), 0) //
switch action {
case .expand:
@ -255,7 +255,7 @@ extension Megrez.Compositor {
}
}
func updateCursorJumpingTables(_ walkedNodes: [Node]) {
mutating func updateCursorJumpingTables(_ walkedNodes: [Node]) {
var cursorRegionMapDict = [Int: Int]()
cursorRegionMapDict[-1] = 0 //
var counter = 0

View File

@ -11,7 +11,7 @@ extension Megrez.Compositor {
/// `G = (V, E)` `O(|V|+|E|)` `G`
/// 使
/// - Returns:
@discardableResult public func walk() -> ([Node], Bool) {
@discardableResult public mutating func walk() -> ([Node], Bool) {
var result = [Node]()
defer {
walkedNodes = result

View File

@ -144,13 +144,17 @@ extension Megrez.Compositor {
guard let overridden = overridden else { return false } //
for i in overridden.spanIndex..<min(spans.count, overridden.spanIndex + overridden.node.spanLength) {
/// A BC
/// A BC
/// A BC 使 A
/// DEF BC A
arrOverlappedNodes = fetchOverlappingNodes(at: i)
for anchor in arrOverlappedNodes {
if anchor.node == overridden.node { continue }
if !overridden.node.key.contains(anchor.node.key) || !overridden.node.value.contains(anchor.node.value) {
anchor.node.reset()
continue
}
anchor.node.overridingScore /= 2
}
}
return true

View File

@ -17,8 +17,8 @@ extension Megrez.Compositor {
/// [("a", -114), ("b", -514), ("c", -1919)]
/// ("c", -114)使
///
/// kOverridingScore
/// - withHighScore: kOverridingScore使
/// overridingScore
/// - withHighScore: overridingScore使
public enum OverrideType: Int {
case withNoOverrides = 0
case withTopUnigramScore = 1
@ -32,7 +32,7 @@ extension Megrez.Compositor {
/// A->bc A B 使0
/// A-B 0
/// c
public static let kOverridingScore: Double = 114_514
public var overridingScore: Double = 114_514
private(set) var key: String
private(set) var keyArray: [String]
@ -81,7 +81,7 @@ extension Megrez.Compositor {
public var score: Double {
guard !unigrams.isEmpty else { return 0 }
switch overrideType {
case .withHighScore: return Megrez.Compositor.Node.kOverridingScore
case .withHighScore: return overridingScore
case .withTopUnigramScore: return unigrams[0].score
default: return currentUnigram.score
}
@ -139,4 +139,45 @@ extension Array where Element == Megrez.Compositor.Node {
///
public var keys: [String] { map(\.key) }
///
/// - Parameters:
/// - cursor:
/// - outCursorPastNode:
/// - Returns:
public func findNode(at cursor: Int, target outCursorPastNode: inout Int) -> Megrez.Compositor.Node? {
guard !isEmpty else { return nil }
let cursor = Swift.max(0, Swift.min(cursor, keys.count))
if cursor == 0, let theFirst = first {
outCursorPastNode = theFirst.spanLength
return theFirst
}
//
if cursor >= keys.count - 1, let theLast = last {
outCursorPastNode = keys.count
return theLast
}
var accumulated = 0
for neta in self {
accumulated += neta.spanLength
if accumulated > cursor {
outCursorPastNode = accumulated
return neta
}
}
//
return nil
}
///
/// - Parameter cursor:
/// - Returns:
public func findNode(at cursor: Int) -> Megrez.Compositor.Node? {
var useless = 0
return findNode(at: cursor, target: &useless)
}
}