This repository was archived by the owner on Aug 18, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConversion.cs
More file actions
192 lines (164 loc) · 6.01 KB
/
Conversion.cs
File metadata and controls
192 lines (164 loc) · 6.01 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using NReco.Text;
namespace DotNetKKC;
public class Conversion
{
AhoCorasickDoubleArrayTrie<int> doubleArrayTrie;
List<(int, int, int, string)> dictionary = new List<(int, int, int, string)>();
int[,] connectionCost = null!;
public Conversion(string filePath)
{
doubleArrayTrie = new AhoCorasickDoubleArrayTrie<int>();
Init(filePath);
}
void Init(string filePath)
{
LoadDoubleArray(filePath);
CreateDictionary();
}
void LoadDoubleArray(string filePath)
{
using var fileStream = new FileStream(filePath, FileMode.Open);
doubleArrayTrie.Load(fileStream);
}
void CreateDictionary()
{
int index = 0;
for (int i = 0; i < 10; ++i)
{
var filePath = Path.Combine("dic", $"dictionary{i:00}.txt");
var result = File.ReadAllLines(filePath);
foreach (var line in result)
{
var fields = line.Split('\t');
dictionary.Add((Convert.ToInt32(fields[1]), Convert.ToInt32(fields[2]), Convert.ToInt32(fields[3]), fields[4]));
index++;
}
}
var costFilePath = Path.Combine("dic", "connection_single_column.txt");
var lines = File.ReadAllLines(costFilePath);
var idLength = Convert.ToInt32(lines[0]);
connectionCost = new int[idLength, idLength];
for (int i = 0; i < idLength; ++i)
{
for (int j = 0; j < idLength; ++j)
{
connectionCost[i, j] = Convert.ToInt32(lines[idLength * i + j + 1]);
}
}
}
public Dictionary<string, double> GetConversion(string text, int n = 10)
{
// Console.WriteLine($"Convert: {text}");
var result = doubleArrayTrie.ParseText(text);
List<Node>[] parentNodeList = new List<Node>[text.Length + 1]; //Contain BOS
Node bos = new Node("", 0, 0, 0); //BOS RightID = 0, cost = 0
Lattice lattice = new Lattice();
parentNodeList[0] = new List<Node>();
parentNodeList[0].Add(bos);
foreach (var hit in result)
{
var node = new Node(dictionary[hit.Value].Item4,
dictionary[hit.Value].Item1, dictionary[hit.Value].Item2, dictionary[hit.Value].Item3);
if (parentNodeList[hit.Begin + hit.Length] == null)
parentNodeList[hit.Begin + hit.Length] = new List<Node>();
parentNodeList[hit.Begin + hit.Length].Add(node);
foreach (var parent in parentNodeList[hit.Begin])
{
lattice.AddEdge(parent, node, connectionCost[parent.rightId, node.leftId]);
}
}
Node eos = new Node("", 0, 0, 0);
foreach (var parent in parentNodeList[text.Length])
{
lattice.AddEdge(parent, eos, connectionCost[parent.rightId, eos.leftId]);
}
var bestCosts = SetBestCosts(lattice, bos);
return GetNBestConversion(lattice, bestCosts, bos, eos, n);
}
Dictionary<Node, double> SetBestCosts(Lattice lattice, Node bos)
{
Dictionary<Node, double> bestCosts = new();
bestCosts[bos] = 0;
foreach (var edge in lattice.edges)
{
var from = edge.fromNode;
var to = edge.toNode;
var cost = bestCosts.TryGetValue(from, out var fromCost) ? fromCost + edge.cost + to.cost : double.PositiveInfinity;
if (!bestCosts.ContainsKey(to) || cost < bestCosts[to])
{
bestCosts[to] = cost;
}
}
return bestCosts;
}
Dictionary<string, double> GetNBestConversion(Lattice lattice, Dictionary<Node, double> bestCosts, Node bos, Node eos, int n)
{
//node, text, BackwardCost, HeuristicCost
PriorityQueue<(Node, string, double), double> queue = new();
Dictionary<string, double> results = new();
queue.Enqueue((eos, eos.key, 0), eos.cost);
while (queue.TryDequeue(out (Node, string, double) item, out double cost))
{
if (item.Item1 == bos)
{
if (!results.ContainsKey(item.Item2))
{
results.Add(item.Item2, cost);
}
}
else
{
foreach (var edge in lattice.inEdges[item.Item1])
{
double backwardCost = item.Item1.cost + item.Item3 + edge.cost;
double heuristicCost = backwardCost + bestCosts[edge.fromNode];
queue.Enqueue((edge.fromNode, item.Item1.key + item.Item2, backwardCost), heuristicCost);
}
}
if (results.Count >= n) break;
}
return results;
}
}
public class DoubleArrayGenerator
{
List<KeyValuePair<string, int>> dic = new List<KeyValuePair<string, int>>();
AhoCorasickDoubleArrayTrie<int>? doubleArrayTrie;
string _dictionaryPath;
string _doubleArrayPath;
public DoubleArrayGenerator(string dictionaryPath, string doubleArrayPath)
{
_dictionaryPath = dictionaryPath;
_doubleArrayPath = doubleArrayPath;
}
public void Build()
{
LoadMozcDictionary();
BuildDoubleArrayFile();
}
void LoadMozcDictionary()
{
int index = 0;
for(int i = 0; i < 10; ++i)
{
var filePath = Path.Combine(_dictionaryPath, $"dictionary{i:00}.txt");
var result = File.ReadAllLines(filePath);
foreach(var line in result)
{
var fields = line.Split('\t');
dic.Add(new KeyValuePair<string, int>(fields[0], index));
index++;
}
}
}
void BuildDoubleArrayFile()
{
doubleArrayTrie = new AhoCorasickDoubleArrayTrie<int>(dic);
using var fileStream = new FileStream(_doubleArrayPath, FileMode.Create);
doubleArrayTrie.Save(fileStream, true);
}
}