mirror of
https://github.com/mii443/AzooKeyKanaKanjiConverter.git
synced 2025-12-03 02:58:27 +00:00
Include prediction into learning
This commit is contained in:
@@ -75,6 +75,14 @@ import SwiftUtils
|
||||
self.lastData = candidate.data.last
|
||||
}
|
||||
|
||||
/// 確定操作後、学習メモリをアップデートする関数。
|
||||
/// - Parameters:
|
||||
/// - candidate: 確定された候補。
|
||||
public func updateLearningData(_ candidate: Candidate, with predictionCandidate: PredictionCandidate) {
|
||||
self.converter.dicdataStore.updateLearningData(candidate, with: predictionCandidate)
|
||||
self.lastData = predictionCandidate.lastData
|
||||
}
|
||||
|
||||
/// 賢い変換候補を生成する関数。
|
||||
/// - Parameters:
|
||||
/// - string: 入力されたString
|
||||
|
||||
@@ -614,6 +614,16 @@ public final class DicdataStore {
|
||||
self.learningManager.update(data: candidate.data)
|
||||
}
|
||||
}
|
||||
// 予測変換に基づいて学習を反映する
|
||||
// TODO: previousの扱いを改善したい
|
||||
func updateLearningData(_ candidate: Candidate, with predictionCandidate: PredictionCandidate) {
|
||||
switch predictionCandidate.type {
|
||||
case .additional(data: let data):
|
||||
self.learningManager.update(data: candidate.data, updatePart: data)
|
||||
case .replacement(targetData: let targetData, replacementData: let replacementData):
|
||||
self.learningManager.update(data: candidate.data.dropLast(targetData.count), updatePart: replacementData)
|
||||
}
|
||||
}
|
||||
/// class idから連接確率を得る関数
|
||||
/// - Parameters:
|
||||
/// - former: 左側の語のid
|
||||
|
||||
@@ -696,25 +696,30 @@ final class LearningManager {
|
||||
}
|
||||
|
||||
func update(data: [DicdataElement]) {
|
||||
self.update(data: [], updatePart: data)
|
||||
}
|
||||
|
||||
/// `updatePart`のみを更新する。`data`の部分は更新しない。
|
||||
func update(data: [DicdataElement], updatePart: [DicdataElement]) {
|
||||
if !options.learningType.needUpdateMemory {
|
||||
return
|
||||
}
|
||||
// 単語単位
|
||||
for datum in data where DicdataStore.needWValueMemory(datum) {
|
||||
for datum in updatePart where DicdataStore.needWValueMemory(datum) {
|
||||
guard let chars = Self.keyToChars(datum.ruby, char2UInt8: char2UInt8) else {
|
||||
continue
|
||||
}
|
||||
self.temporaryMemory.memorize(dicdataElement: datum, chars: chars)
|
||||
}
|
||||
|
||||
if data.count == 1 {
|
||||
if data.count + updatePart.count == 1 {
|
||||
return
|
||||
}
|
||||
// 文節単位bigram
|
||||
do {
|
||||
var firstClause: DicdataElement?
|
||||
var secondClause: DicdataElement?
|
||||
for datum in data {
|
||||
for (datum, index) in zip(data.chained(updatePart), 0 ..< data.count + updatePart.count) {
|
||||
if var newFirstClause = firstClause {
|
||||
if var newSecondClause = secondClause {
|
||||
if DicdataStore.isClause(newFirstClause.rcid, datum.lcid) {
|
||||
@@ -730,6 +735,10 @@ final class LearningManager {
|
||||
// firstClauseを押し出す
|
||||
firstClause = secondClause
|
||||
secondClause = datum
|
||||
// 更新対象のindexでなければcontinueする
|
||||
guard data.endIndex <= index else {
|
||||
continue
|
||||
}
|
||||
guard let chars = Self.keyToChars(element.ruby, char2UInt8: char2UInt8) else {
|
||||
continue
|
||||
}
|
||||
@@ -782,6 +791,7 @@ final class LearningManager {
|
||||
}
|
||||
}
|
||||
// 全体
|
||||
let data = data.chained(updatePart)
|
||||
let element = DicdataElement(
|
||||
word: data.reduce(into: "") {$0.append(contentsOf: $1.word)},
|
||||
ruby: data.reduce(into: "") {$0.append(contentsOf: $1.ruby)},
|
||||
|
||||
@@ -52,4 +52,12 @@ public struct PredictionCandidate {
|
||||
case replacement(targetData: [DicdataElement], replacementData: [DicdataElement])
|
||||
}
|
||||
|
||||
var lastData: DicdataElement? {
|
||||
switch self.type {
|
||||
case .additional(let data):
|
||||
return data.last
|
||||
case .replacement(_, let replacementData):
|
||||
return replacementData.last
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user