-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAlignmentInfoElement.cs
268 lines (250 loc) · 10.1 KB
/
AlignmentInfoElement.cs
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
//
// AlignmentInfoElement.cs
//
// Author:
// Mārcis Pinnis <marcis.pinnis@gmail.com>
//
// Copyright (c) 2013 Mārcis Pinnis
//
// This program can be freely used only for scientific and educational purposes.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using System.Xml.Serialization;
using MPFramework;
namespace MPAligner
{
[Serializable]
public class AlignmentInfoElement :IComparable<AlignmentInfoElement>
{
public AlignmentInfoElement ()
{
srcToTrgAlignments = new List<WordAlignmentElement>();
}
public ProcessedTermEntry srcEntry;
public ProcessedTermEntry trgEntry;
[XmlIgnore]
public List<WordAlignmentElement> srcToTrgAlignments;
[XmlIgnore]
public List<WordAlignmentElement> trgToSrcAlignments;
public List<WordAlignmentElement> consolidatedAlignment;
//public List<StringComparisonElement> finalAlignment;
[XmlAttribute("minSrcId")]
public int minSrcId;
[XmlAttribute("minTrgId")]
public int minTrgId;
[XmlAttribute("maxSrcId")]
public int maxSrcId;
[XmlAttribute("maxTrgId")]
public int maxTrgId;
public string srcStrForAlignment;
public string trgStrForAlignment;
public string alignedLowSrcStr;
public string alignedLowTrgStr;
public string srcFile;
public string trgFile;
[XmlAttribute("alingScore")]
public double alignmentScore;
[XmlAttribute("srcMult")]
public double srcMultiplier;
[XmlAttribute("trgMult")]
public double trgMultiplier;
public static void PrintList(string outputFormat, string outputFile, List<AlignmentInfoElement> list, bool printTopSrc, MPAlignerConfigurationLangPairEntry lpeConf = null, string srcLang="", string trgLang="", string collectionId="", string domain="")
{
MPAlignerConfigurationLangPairEntry lpeConfig = lpeConf!=null?lpeConf:new MPAlignerConfigurationLangPairEntry();
if (list == null) return;
Encoding utf8WithoutBom = new UTF8Encoding(false);
//string outFile = outputFile+".xml";
if (outputFormat.ToLower() == "xml")
{
list.Sort();
List<AlignmentInfoElement> newList = new List<AlignmentInfoElement>();
string previousSrc = null;
foreach(AlignmentInfoElement aie in list)
{
string currSrc = GetStrFromEntry(aie.srcEntry.surfaceFormWords, aie.minSrcId, aie.maxSrcId,aie.srcEntry.lemmaSeq).Trim();
string currTrg = GetStrFromEntry(aie.trgEntry.surfaceFormWords, aie.minTrgId, aie.maxTrgId,aie.trgEntry.lemmaSeq).Trim();
if (string.IsNullOrWhiteSpace(currTrg)||string.IsNullOrWhiteSpace(currSrc)) continue;
if ((!printTopSrc || previousSrc!=currSrc.ToLower()) && aie.alignmentScore>=lpeConfig.printThr)
{
newList.Add(aie);
previousSrc = currSrc.ToLower();
}
}
string outStr = MPFramework.MPFrameworkFunctions.SerializeObjectInstance<List<AlignmentInfoElement>>(newList);
File.WriteAllText(outputFile,outStr,utf8WithoutBom);
}
else
{
StreamWriter sw = new StreamWriter(outputFile,false, utf8WithoutBom);
PrintTabsep (sw, outputFormat, list, printTopSrc, lpeConfig, srcLang, trgLang, collectionId, domain);
sw.Close();
}
}
private static void PrintTabsep(StreamWriter sw, string outputFormat, List<AlignmentInfoElement> list, bool printTopSrc, MPAlignerConfigurationLangPairEntry lpeConf, string srcLang="", string trgLang="", string collectionId="", string domain="")
{
NumberFormatInfo nfi = new NumberFormatInfo();
nfi.CurrencyDecimalSeparator = ".";
nfi.NumberDecimalSeparator = ".";
nfi.PercentDecimalSeparator = ".";
bool fullFormat = outputFormat.ToLower()=="tabsep"?false:true;
string prevSrc = null;
list.Sort();
foreach(AlignmentInfoElement aie in list)
{
string currSrc = GetStrFromEntry(aie.srcEntry.surfaceFormWords, aie.minSrcId, aie.maxSrcId,aie.srcEntry.lemmaSeq).Trim();
string srcLemmas = GetStrFromEntry(aie.srcEntry.lemmaSeq, aie.minSrcId, aie.maxSrcId).Trim();
string srcMsd = GetStrFromEntry(aie.srcEntry.msdSeq, aie.minSrcId, aie.maxSrcId).Trim();
string srcNorm = GetStrFromEntry(aie.srcEntry.normSeq, aie.minSrcId, aie.maxSrcId).Trim();
string srcNormMsd = GetStrFromEntry(aie.srcEntry.normMsdSeq, aie.minSrcId, aie.maxSrcId).Trim();
if (string.IsNullOrWhiteSpace(currSrc)) continue;
if ((!printTopSrc || prevSrc!=currSrc.ToLower()) && aie.alignmentScore>=lpeConf.printThr)
{
string currTrg = GetStrFromEntry(aie.trgEntry.surfaceFormWords, aie.minTrgId, aie.maxTrgId,aie.trgEntry.lemmaSeq).Trim();
string trgLemmas = GetStrFromEntry(aie.trgEntry.lemmaSeq, aie.minTrgId, aie.maxTrgId).Trim();
string trgMsd = GetStrFromEntry(aie.trgEntry.msdSeq, aie.minTrgId, aie.maxTrgId).Trim();
string trgNorm = GetStrFromEntry(aie.trgEntry.normSeq, aie.minTrgId, aie.maxTrgId).Trim();
string trgNormMsd = GetStrFromEntry(aie.trgEntry.normMsdSeq, aie.minTrgId, aie.maxTrgId).Trim();
if (string.IsNullOrWhiteSpace(currTrg)) continue;
if (fullFormat)
{
sw.Write (srcLang);
sw.Write("\t");
}
sw.Write(currSrc);
sw.Write("\t");
if (fullFormat)
{
sw.Write (trgLang);
sw.Write("\t");
}
sw.Write(currTrg);
sw.Write("\t");
if (fullFormat)
{
sw.Write (domain);
sw.Write("\t");
sw.Write (collectionId);
sw.Write("\t");
}
sw.Write(aie.alignmentScore.ToString("0.000000",nfi));
if (fullFormat)
{
sw.Write("\t");
sw.Write(srcMsd);
sw.Write("\t");
sw.Write(srcLemmas);
sw.Write("\t");
sw.Write(srcNorm);
sw.Write("\t");
sw.Write(srcNormMsd);
sw.Write("\t");
sw.Write(aie.srcEntry.concordance);
sw.Write("\t");
sw.Write(trgMsd);
sw.Write("\t");
sw.Write(trgLemmas);
sw.Write("\t");
sw.Write(trgNorm);
sw.Write("\t");
sw.Write(trgNormMsd);
sw.Write("\t");
sw.Write(aie.trgEntry.concordance);
sw.Write("\t");
sw.Write(aie.srcFile);
sw.Write("\t");
sw.Write(aie.trgFile);
sw.Write("\t");
}
sw.WriteLine();
prevSrc = currSrc.ToLower();
}
}
}
public static void AppendList(string outputFormat, string outputFile, List<AlignmentInfoElement> list, MPAlignerConfigurationLangPairEntry lpeConf = null, string srcLang="", string trgLang="", string collectionId="", string domain="")
{
MPAlignerConfigurationLangPairEntry lpeConfig = lpeConf!=null?lpeConf:new MPAlignerConfigurationLangPairEntry();
if (list == null) return;
Encoding utf8WithoutBom = new UTF8Encoding(false);
//string outFile = outputFile+".xml";
NumberFormatInfo nfi = new NumberFormatInfo();
nfi.CurrencyDecimalSeparator = ".";
nfi.NumberDecimalSeparator = ".";
nfi.PercentDecimalSeparator = ".";
StreamWriter sw = new StreamWriter(outputFile,true, utf8WithoutBom);
PrintTabsep (sw, outputFormat, list, false, lpeConfig, srcLang, trgLang, collectionId, domain);
sw.Close();
}
public static string GetStrFromEntry (List<string> strList, int minId, int maxId, List<string> lemmaList = null)
{
int min = 0;
int max = 0;
if (strList==null)
{
return "";
}
if (minId<0||maxId<0||maxId>=strList.Count)
{
min=0;
max = strList.Count-1;
}
else
{
min = minId;
max = maxId;
}
StringBuilder sb = new StringBuilder(100);
for (int i=min;i<=max;i++)
{
if (i>=strList.Count) break;
if (sb.Length>0)
{
sb.Append(" ");
}
if (lemmaList==null||i+1>lemmaList.Count)
{
sb.Append(strList[i]);
}
else
{
if (MPFrameworkFunctions.IsAllLower(lemmaList[i]))
{
sb.Append(strList[i].ToLower());
}
else if (MPFrameworkFunctions.IsAllUpper(lemmaList[i]))
{
sb.Append(strList[i].ToUpper());
}
else if (MPFrameworkFunctions.IsFirstUpper(lemmaList[i]))
{
string firstCapital = Char.ToUpper(strList[i][0]).ToString()+strList[i].Substring(1).ToLower();
sb.Append(firstCapital);
}
else
{
sb.Append(strList[i]);
}
}
}
return sb.ToString();
}
#region IComparable[AlignmentInfoElement] implementation
public int CompareTo (AlignmentInfoElement other)
{
string thisStr = AlignmentInfoElement.GetStrFromEntry(this.srcEntry.surfaceFormWords, this.minSrcId, this.maxSrcId);
string otherStr = AlignmentInfoElement.GetStrFromEntry(other.srcEntry.surfaceFormWords, other.minSrcId, other.maxSrcId);
if (thisStr==otherStr)
{
return other.alignmentScore.CompareTo(this.alignmentScore); // From large to small.
}
return thisStr.CompareTo(otherStr); // From A to Z.
}
#endregion
}
}