feat: add forget memory test

This commit is contained in:
Miwa / Ensan
2025-06-01 21:39:33 +09:00
parent 53cb1e3b41
commit 7af6d81ef6
4 changed files with 47 additions and 9 deletions

View File

@ -226,7 +226,7 @@ public final class DicdataStore {
} }
} }
private func perfectMatchLOUDS(query: String, charIDs: [UInt8]) -> [Int] { func perfectMatchLOUDS(query: String, charIDs: [UInt8]) -> [Int] {
guard let louds = self.loadLOUDS(query: query) else { guard let louds = self.loadLOUDS(query: query) else {
return [] return []
} }

View File

@ -626,7 +626,7 @@ final class LearningManager {
debug("Error: louds/charID.chidが存在しません。このエラーは深刻ですが、テスト時には無視できる場合があります。Description: \(error)") debug("Error: louds/charID.chidが存在しません。このエラーは深刻ですが、テスト時には無視できる場合があります。Description: \(error)")
} }
} }
private var char2UInt8: [Character: UInt8] = [:] var char2UInt8: [Character: UInt8] = [:]
static var today: UInt16 { static var today: UInt16 {
UInt16(Int(Date().timeIntervalSince1970) / 86400) - 19000 UInt16(Int(Date().timeIntervalSince1970) / 86400) - 19000
@ -662,6 +662,13 @@ final class LearningManager {
self.options = newOptions self.options = newOptions
return false return false
} }
// `char2Int8`
if newOptions.dictionaryResourceURL != self.options.dictionaryResourceURL {
Self.updateChar2Int8(bundleURL: newOptions.dictionaryResourceURL, target: &self.char2UInt8)
}
//
self.options = newOptions
// //
self.memoryCollapsed = LongTermLearningMemory.memoryCollapsed(directoryURL: newOptions.memoryDirectoryURL) self.memoryCollapsed = LongTermLearningMemory.memoryCollapsed(directoryURL: newOptions.memoryDirectoryURL)
if self.memoryCollapsed && newOptions.learningType.needUsingMemory { if self.memoryCollapsed && newOptions.learningType.needUsingMemory {
@ -670,7 +677,7 @@ final class LearningManager {
tempTrie: TemporalLearningMemoryTrie(), tempTrie: TemporalLearningMemoryTrie(),
directoryURL: newOptions.memoryDirectoryURL, directoryURL: newOptions.memoryDirectoryURL,
maxMemoryCount: newOptions.maxMemoryCount, maxMemoryCount: newOptions.maxMemoryCount,
char2UInt8: char2UInt8 char2UInt8: self.char2UInt8
) )
} catch { } catch {
debug(#file, #function, "automatic merge failed", error) debug(#file, #function, "automatic merge failed", error)
@ -681,12 +688,6 @@ final class LearningManager {
// //
debug(#file, #function, "LearningManager init: Memory Collapsed") debug(#file, #function, "LearningManager init: Memory Collapsed")
} }
// `char2Int8`
if newOptions.dictionaryResourceURL != self.options.dictionaryResourceURL {
Self.updateChar2Int8(bundleURL: newOptions.dictionaryResourceURL, target: &char2UInt8)
}
//
self.options = newOptions
switch self.options.learningType { switch self.options.learningType {
case .inputAndOutput, .onlyOutput: break case .inputAndOutput, .onlyOutput: break

View File

@ -69,5 +69,42 @@ final class LearningMemoryTests: XCTestCase {
let filesAfter = try FileManager.default.contentsOfDirectory(at: dir, includingPropertiesForKeys: nil) let filesAfter = try FileManager.default.contentsOfDirectory(at: dir, includingPropertiesForKeys: nil)
XCTAssertTrue(filesAfter.isEmpty) XCTAssertTrue(filesAfter.isEmpty)
} }
func testForgetMemory() throws {
let dir = FileManager.default.temporaryDirectory.appendingPathComponent("LearningManagerPersistence-\(UUID().uuidString)", isDirectory: true)
try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
defer { try? FileManager.default.removeItem(at: dir) }
let options = self.getOptionsForMemoryTest(memoryDirectoryURL: dir)
let manager = LearningManager()
_ = manager.setRequestOptions(options)
let element = DicdataElement(word: "テスト", ruby: "テスト", cid: CIDData..cid, mid: MIDData..mid, value: -10)
manager.update(data: [element])
manager.save()
let dicdataStore = DicdataStore(requestOptions: options)
dicdataStore.sendToDicdataStore(.setRequestOptions(options))
let charIDs = "テスト".map { dicdataStore.character2charId($0) }
let indices = dicdataStore.perfectMatchLOUDS(query: "memory", charIDs: charIDs)
let dicdata = dicdataStore.getDicdataFromLoudstxt3(identifier: "memory", indices: indices)
XCTAssertFalse(dicdata.isEmpty)
XCTAssertTrue(dicdata.contains { $0.word == element.word && $0.ruby == element.ruby })
dicdataStore.sendToDicdataStore(
.forgetMemory(
Candidate(
text: element.word,
value: element.value(),
correspondingCount: 3,
lastMid: element.mid,
data: [element]
)
)
)
let indices2 = dicdataStore.perfectMatchLOUDS(query: "memory", charIDs: charIDs)
let dicdata2 = dicdataStore.getDicdataFromLoudstxt3(identifier: "memory", indices: indices2)
XCTAssertFalse(dicdata2.contains { $0.word == element.word && $0.ruby == element.ruby })
}
} }