Include prediction into learning

This commit is contained in:
ensan-hcl
2023-09-23 00:19:42 +09:00
parent a1dbbbab3b
commit 958593dccb
4 changed files with 39 additions and 3 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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
// indexcontinue
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)},

View File

@@ -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
}
}
}