-
Notifications
You must be signed in to change notification settings - Fork 11
/
UGram.cs
180 lines (164 loc) · 4.22 KB
/
UGram.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
/*
* Created by SharpDevelop.
* User: gheyret
* Date: 2010/01/06
* Time: 8:39
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Collections;
using System.IO;
using System.Text;
namespace UyghurNgram
{
/// <summary>
/// Description of UGram.
/// </summary>
public class UGram: IComparer
{
private TstTree mBuf=new TstTree();
private StringBuilder ngramBuf=new StringBuilder();
private int gGramLen;
public UGram(int gramlen) {
gGramLen=gramlen;
}
public void Save(String name,int freq)
{
try{
StreamWriter ofs=new StreamWriter(name,false,Encoding.UTF8);
mBuf.Save(ofs,freq);
ofs.Close();
}catch(Exception ee){
System.Console.WriteLine(ee.Message);
}
mBuf=null;
mBuf=new TstTree();
}
public int GetCount(){
return mBuf.Count;
}
public void PutString(String soz){
mBuf.Add(soz);
}
//Bu birdin gGramLen ghiche uzunluqtiki gram yasaydu
private void PutXGram(String jumle)
{
int i,j;
String[] sozler=utils.GetSozler(jumle);
int N=gGramLen;
int len;
for(i=0;i<sozler.Length;i++){
ngramBuf.Length=0;
mBuf.Add(sozler[i]);
ngramBuf.Append(sozler[i]);
len=N+i;
if(len>sozler.Length){
len=sozler.Length;
}
for(j=i+1;j<len;j++){
ngramBuf.Append(" "+sozler[j]);
mBuf.Add(ngramBuf.ToString());
}
}
}
//Bu gGramLen uzunluqtiki gram yasaydu
private void PutNGram(String jumle)
{
int i,j;
String[] sozler=utils.GetSozler(jumle);
int N=gGramLen;
if(sozler.Length<N) return ;
for(i=0;i<=sozler.Length-N;i++){
ngramBuf.Length=0;
for(j=i;j<N+i;j++){
ngramBuf.Append(" "+sozler[j]);
}
mBuf.Add(ngramBuf.ToString().Trim());
}
}
public void MakeNGram(String fileName){
try{
System.Console.WriteLine("Make Only "+ gGramLen+" gram using file "+ fileName);
StringBuilder buf=new StringBuilder();
String[] jumle;
StreamReader inr=new StreamReader(fileName,Encoding.UTF8);
String abzas=inr.ReadLine();
while(abzas!=null){
jumle=utils.GetJumle(abzas);
foreach(String jm in jumle){
PutNGram(jm);
}
abzas = inr.ReadLine();
}
inr.Close();
System.Console.WriteLine("Tamam nGram Sani =" +mBuf.Count);
}catch(Exception ee){
System.Console.WriteLine(ee.Message+ " Sani="+mBuf.Count);
}
}
public void MakeNGramOrg(String fileName){
try{
System.Console.WriteLine("Make Only "+ gGramLen+" gram using file "+ fileName);
StringBuilder buf=new StringBuilder();
StreamReader inr=new StreamReader(fileName,Encoding.UTF8);
String jumle=inr.ReadLine();
while(jumle!=null){
if(string.IsNullOrEmpty(jumle)==false){
PutNGram(jumle);
}
jumle = inr.ReadLine();
}
inr.Close();
System.Console.WriteLine("Tamam nGram Sani =" +mBuf.Count);
}catch(Exception ee){
System.Console.WriteLine(ee.Message+ " Sani="+mBuf.Count);
}
}
public void MakeXGram(String fileName){
try{
StringBuilder buf=new StringBuilder();
System.Console.WriteLine(fileName);
StreamReader inr=new StreamReader(fileName,Encoding.UTF8);
String abzas=inr.ReadLine();
String[] jumle;
while(abzas!=null){
jumle=utils.GetJumle(abzas);
foreach(String jm in jumle){
PutXGram(jm);
}
abzas = inr.ReadLine();
}
inr.Close();
}catch(Exception ee){
System.Console.WriteLine(ee.Message+ " Sani="+mBuf.Count);
}
}
public ArrayList GetNGram(int freq)
{
ArrayList sanduq=new ArrayList();
mBuf.GetAll(freq,sanduq);
mBuf=null;
mBuf=new TstTree();
return sanduq;
}
public int Compare(Object a,Object b)
{
String aa=(String)a;
String bb=(String)b;
int frq1=int.Parse(aa.Split('\t')[1]);
int frq2=int.Parse(bb.Split('\t')[1]);
return frq2-frq1;
}
public ArrayList GetSorted(int freq)
{
ArrayList ret=new ArrayList();
mBuf.GetAll(freq,ret);
mBuf=null;
mBuf=new TstTree();
System.Console.WriteLine("Tiziwatidu Jemiy ="+ ret.Count+ " Soz");
ret.Sort(this);
return ret;
}
}
}