Skip to content

Commit

Permalink
created dicitionary manager
Browse files Browse the repository at this point in the history
  • Loading branch information
p3nGu1nZz committed Apr 2, 2024
1 parent 719e71c commit 8716ac3
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Assets/Scripts/Agents/DictionaryEntry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace DialogosEngine
{
public struct DictionaryEntry
{
public int Id { get; }
public string Word { get; }
public int Frequency { get; set; }

public DictionaryEntry(int id, string word, int frequency)
{
Id = id;
Word = word;
Frequency = frequency;
}
}

}
79 changes: 79 additions & 0 deletions Assets/Scripts/Agents/DictionaryManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;

namespace DialogosEngine
{
public class DictionaryManager
{
public Dictionary<string, (int id, int frequency)> WordDictionary = new();
private int _NextAvailableId = 0;
private const int k_MaxDictionarySize = 10000000;
private const string k_DictionaryName = "dictionary";
private const string k_DictionaryExt = "bin";
private const string k_DateSuffixFormat = "yyMMdd";

private int FrequencyThreshold => 5;

public void UpdateFrequency(string word)
{
if (WordDictionary.ContainsKey(word))
{
var entry = WordDictionary[word];
entry.frequency++;
WordDictionary[word] = entry;
}
else if (WordDictionary.Count < k_MaxDictionarySize)
{
WordDictionary.Add(word, (_NextAvailableId++, 1));
}
}

public void PruneDictionary()
{
var infrequentWords = WordDictionary
.Where(pair => pair.Value.frequency < FrequencyThreshold)
.Select(pair => pair.Key)
.ToList();

foreach (var word in infrequentWords)
{
WordDictionary.Remove(word);
}
}
public void SaveDictionaryToBinaryFile(string directoryPath)
{
string fileName = k_DictionaryName + "." + k_DictionaryExt;
string filePath = Path.Combine(directoryPath, fileName);

if (File.Exists(filePath))
{
string dateSuffix = DateTime.Now.ToString(k_DateSuffixFormat);
string newFileName = $"{k_DictionaryName}.{dateSuffix}.{k_DictionaryExt}";
string newFilePath = Path.Combine(directoryPath, newFileName);
File.Move(filePath, newFilePath);
}

BinaryFormatter formatter = new();
using FileStream stream = new(filePath, FileMode.Create, FileAccess.Write, FileShare.None);
formatter.Serialize(stream, WordDictionary);
}

public void LoadDictionaryFromBinaryFile(string filePath)
{
if (File.Exists(filePath))
{
BinaryFormatter formatter = new BinaryFormatter();
using FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
WordDictionary = (Dictionary<string, (int id, int frequency)>)formatter.Deserialize(stream);
}
else
{
Debug.LogError("Dictionary file not found.");
}
}
}
}

0 comments on commit 8716ac3

Please sign in to comment.