-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParseDictionary.cs
More file actions
133 lines (80 loc) · 4.35 KB
/
ParseDictionary.cs
File metadata and controls
133 lines (80 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using PluginContracts;
namespace WeightedDictionary
{
//partial class because we're splitting this bad boy up between files
internal class DictParser
{
public DictionaryData ParseDict(DictionaryMetaObject DictionaryToParse)
{
DictionaryData DictData = DictionaryToParse.DictData;
// ____ _ _ ____ _ _ ____ _ ___ _ _ _
// | _ \ ___ _ __ _ _| | __ _| |_ ___ | _ \(_) ___| |_| _ \ __ _| |_ __ _ / _ \| |__ (_) ___ ___| |_
// | |_) / _ \| '_ \| | | | |/ _` | __/ _ \ | | | | |/ __| __| | | |/ _` | __/ _` | | | | | '_ \| |/ _ \/ __| __|
// | __/ (_) | |_) | |_| | | (_| | || __/ | |_| | | (__| |_| |_| | (_| | || (_| | | |_| | |_) | | __/ (__| |_
// |_| \___/| .__/ \__,_|_|\__,_|\__\___| |____/|_|\___|\__|____/ \__,_|\__\__,_| \___/|_.__// |\___|\___|\__|
// |_| |__/
//parse out the the dictionary file
DictData.MaxWords = 0;
DictData.FullDictionary = new Dictionary<int, Dictionary<string, double[]>>();
string[] DicLines = DictionaryToParse.DictionaryRawText.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries);
string[] HeaderLines = DicLines[0].Split(new[] { '\t' });
string[] InterceptLines = DicLines[1].Split(new[] { '\t' });
DictData.NumCats = HeaderLines.Length - 1;
//now that we know the number of categories, we can fill out the arrays
DictData.CatNames = new string[DictData.NumCats];
DictData.InterceptWeights = new double[DictData.NumCats];
//Map Out the Categories
for (int i = 0; i < DictData.NumCats; i++)
{
DictData.CatNames[i] = HeaderLines[i+1];
DictData.InterceptWeights[i] = Double.Parse(InterceptLines[i + 1]);
}
//Map out the dictionary entries
for (int i = 2; i < DicLines.Length; i++)
{
string EntryLine = DicLines[i];
string[] EntryRow = EntryLine.Split(new char[] { '\t' }, StringSplitOptions.None);
if (EntryRow.Length > 1 && !String.IsNullOrWhiteSpace(EntryRow[0])) {
int Words_In_Entry = EntryRow[0].Split(' ').Length;
if (Words_In_Entry > DictData.MaxWords) DictData.MaxWords = Words_In_Entry;
double[] WordWeights = new double[DictData.NumCats];
//this is where we actually map out the word weights from the dictionary file
for (int j = 0; j < DictData.NumCats; j++)
{
if (!String.IsNullOrWhiteSpace(EntryRow[j + 1]))
{
try
{
WordWeights[j] = Double.Parse(EntryRow[j + 1].Trim());
}
catch
{
WordWeights[j] = Double.Epsilon;
}
}
else
{
WordWeights[j] = Double.Epsilon;
}
}
if (DictData.FullDictionary.ContainsKey(Words_In_Entry))
{
if (!DictData.FullDictionary[Words_In_Entry].ContainsKey(EntryRow[0].ToLower()))
DictData.FullDictionary[Words_In_Entry].Add(EntryRow[0].ToLower(), WordWeights);
}
else
{
DictData.FullDictionary.Add(Words_In_Entry, new Dictionary<string, double[]> { { EntryRow[0].ToLower(), WordWeights } });
}
}
}
DictData.DictionaryLoaded = true;
return DictData;
//MessageBox.Show("Your dictionary has been successfully loaded.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}