-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathConsolidationElement.cs
154 lines (147 loc) · 4.83 KB
/
ConsolidationElement.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
//
// ConsolidationElement.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.IO;
using System.Text;
using System.Collections.Generic;
using System.Globalization;
namespace MPAligner
{
public class ConsolidationElement
{
public ConsolidationElement ()
{
}
public string line;
public double prob;
public static void ConsolidateRefTabsep(string inputFile, string outputFile, double threshold)
{
NumberFormatInfo nfi = new NumberFormatInfo ();
nfi.CurrencyDecimalSeparator = ".";
nfi.NumberDecimalSeparator = ".";
nfi.PercentDecimalSeparator = ".";
StreamReader sr = new StreamReader (inputFile, Encoding.UTF8);
StreamWriter sw = new StreamWriter (outputFile, false, new UTF8Encoding (false));
sw.NewLine = "\n";
Dictionary<string,Dictionary<string,int>> added = new Dictionary<string, Dictionary<string, int>> ();
// SRC -> TRG -> ElemList
Dictionary<string,Dictionary<string,List<ConsolidationElement>>> lemmaDict = new Dictionary<string, Dictionary<string, List<ConsolidationElement>>> ();
char[] sep = {'\t'};
while (!sr.EndOfStream) {
string line = sr.ReadLine ();
if (!string.IsNullOrWhiteSpace (line)) {
string[] dataArr = line.Split (sep, StringSplitOptions.None);
if (dataArr.Length == 20) {
double prob = Convert.ToDouble (dataArr [6], nfi);
if (prob >= threshold-0.1) {
string srcKey = dataArr [1] + dataArr [7] + dataArr [8];
string trgKey = dataArr [3] + dataArr [12] + dataArr [13];
string srcLemmaKey = System.Net.WebUtility.HtmlDecode(dataArr [8]);
string trgLemmaKey = System.Net.WebUtility.HtmlDecode(dataArr [13]);
if (srcLemmaKey.Length<3||trgLemmaKey.Length<3) continue;
if (!IsValidLemma(srcLemmaKey)||!IsValidLemma(trgLemmaKey)) continue;
if (!added.ContainsKey (srcKey)) {
added.Add (srcKey, new Dictionary<string, int> ());
}
if (!added [srcKey].ContainsKey (trgKey)) {
added [srcKey].Add (trgKey, 1);
//sw.WriteLine (line);
if (!lemmaDict.ContainsKey (srcLemmaKey)) {
lemmaDict.Add (srcLemmaKey, new Dictionary<string, List<ConsolidationElement>> ());
}
if (!lemmaDict [srcLemmaKey].ContainsKey (trgLemmaKey)) {
lemmaDict [srcLemmaKey].Add (trgLemmaKey, new List<ConsolidationElement> ());
}
ConsolidationElement ce = new ConsolidationElement ();
ce.line = line;
ce.prob = prob;
lemmaDict [srcLemmaKey] [trgLemmaKey].Add (ce);
} else {
added [srcKey] [trgKey]++;
}
}
}
}
}
List<string> srcLemmas = new List<string> (lemmaDict.Keys);
srcLemmas.Sort ();
foreach (string srcLemma in srcLemmas) {
double max = Double.MinValue;
Dictionary<string,double> avgProbDict = new Dictionary<string, double>();
List<string> trgLemmas = new List<string>();
foreach(string trgLemma in lemmaDict[srcLemma].Keys)
{
trgLemmas.Add(trgLemma);
double sum=0;
double count=0;
foreach(ConsolidationElement ce in lemmaDict[srcLemma][trgLemma])
{
sum+=ce.prob;
count++;
}
double score = 0;
if (count>0)
{
score = sum/count;
if (score>max) max = score;
}
avgProbDict.Add(trgLemma,score);
}
trgLemmas.Sort();
double minThr = max - 0.05;
foreach(string trgLemma in trgLemmas)
{
if (avgProbDict[trgLemma]>=minThr&&avgProbDict[trgLemma]>=threshold)
{
List<string> lines = new List<string>();
foreach(ConsolidationElement ce in lemmaDict[srcLemma][trgLemma])
{
lines.Add(ce.line);
}
lines.Sort();
foreach(string line in lines)
{
sw.WriteLine(System.Net.WebUtility.HtmlDecode(line));
}
}
}
}
sw.Close ();
sr.Close ();
}
static bool IsValidLemma (string lemma)
{
if (!string.IsNullOrWhiteSpace (lemma)) {
foreach (char c in lemma)
{
if (Char.IsLetter(c)) continue;
if (Char.IsDigit(c)) continue;
if (Char.IsWhiteSpace(c)) continue;
if (c=='-') continue;
if (c=='‒') continue;
if (c=='–') continue;
if (c=='—') continue;
if (c=='―') continue;
if (c=='\'') continue;
if (Char.IsControl(c)) return false;
if (Char.IsPunctuation(c)) return false;
}
if (lemma.Length > 0 && (Char.IsControl (lemma [0]) || Char.IsPunctuation(lemma [0])||Char.IsControl (lemma [lemma.Length-1]) || Char.IsPunctuation(lemma [lemma.Length-1])))
return false;
return true;
}
return false;
}
}
}