windowcapture
исходный код / Tools/TrainSpellNet.cs

TrainSpellNet.cs

59 строк · 2,326 байт · модуль Tools
 1// TrainSpellNet: trains the SpellNet neural network from dictionary
 2// Compile: csc /out:TrainSpellNet.exe TrainSpellNet.cs ..\Helpers\SpellNet.cs ..\Helpers\Logger.cs
 3// Run: TrainSpellNet.exe
 4
 5using System;
 6using System.IO;
 7using System.Collections.Generic;
 8using WindowCapture.Helpers;
 9
10class TrainSpellNet
11{
12    static void Main()
13    {
14        string dataDir = Path.Combine(Path.GetDirectoryName(
15            System.Reflection.Assembly.GetExecutingAssembly().Location), "..", "Data");
16        if (!Directory.Exists(dataDir))
17            dataDir = Path.Combine(Environment.CurrentDirectory, "Data");
18
19        string dictPath = Path.Combine(dataDir, "dict_ru.txt");
20        if (!File.Exists(dictPath))
21        {
22            Console.WriteLine("dict_ru.txt not found at: " + dictPath);
23            return;
24        }
25
26        Console.WriteLine("Loading dictionary...");
27        var lines = File.ReadAllLines(dictPath);
28        var words = new List<string>();
29        foreach (var l in lines)
30        {
31            string w = l.Trim().ToLower();
32            if (w.Length >= 2 && w.Length <= 20) words.Add(w);
33        }
34        Console.WriteLine("Loaded " + words.Count + " words");
35
36        string savePath = Path.Combine(dataDir, "spellnn.bin");
37        Console.WriteLine("Training SpellNet v2 (20 epochs, trigram model)...");
38        SpellNet.Train(words.ToArray(), savePath, 20);
39
40        // Verify
41        Console.WriteLine("\nVerification:");
42        var net = new SpellNet();
43        net.Load(savePath);
44
45        string[] testCorrect = { "привет", "пожалуйста", "работает", "здравствуйте", "компьютер", "документ" };
46        string[] testWrong = { "прввет", "пожалцуйста", "рабтает", "здравствуйтн", "комьютер", "докумтен" };
47
48        Console.WriteLine("Correct words:");
49        foreach (var w in testCorrect)
50            Console.WriteLine("  " + w + " → " + net.Predict(w).ToString("F3"));
51
52        Console.WriteLine("Misspelled words:");
53        foreach (var w in testWrong)
54            Console.WriteLine("  " + w + " → " + net.Predict(w).ToString("F3"));
55
56        Console.WriteLine("\nDone! Saved to: " + savePath);
57        Console.WriteLine("Size: " + new FileInfo(savePath).Length / 1024 + "KB");
58    }
59}