-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
203 lines (182 loc) · 9.57 KB
/
Program.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace textmining.demo.virastar.console
{
class Program
{
public static string BaseAddress = "https://api.text-mining.ir/api/"; // "https://localhost:44385/api/";
public static string ApiFilePath = Path.Combine(Directory.GetParent(Directory.GetParent(Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().Location)).FullName).FullName, "api_key.txt");
public static string ApiKey =File.ReadAllText(ApiFilePath) ; // YOUR_API_KEY
private static string GetJWTToken(string apiKey)
{
string jwtToken = string.Empty;
HttpClient client = new HttpClient();
var response = client.GetAsync($"{BaseAddress}Token/GetToken?apikey={apiKey}").Result;
if (response.IsSuccessStatusCode)
{
string res = response.Content.ReadAsStringAsync().Result;
jwtToken = (string)JObject.Parse(res)["token"];
}
return jwtToken;
}
static void Main()
{
using (HttpClient client = new HttpClient {Timeout = TimeSpan.FromSeconds(300)})
{
/**********************************************
************ Step 1: get token ***************
**********************************************/
string bearerToken = GetJWTToken(ApiKey);
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", bearerToken);
/************************************************
*** Step 2: set your virastar configurations ***
************************************************/
var config = new VirastarConfig()
{
SpellConfiguration =
{
LexicalSpellCheckerDistanceThreshold = 3.0,
ContextSpellCheckHighSensitive = false
},
WordConfiguration = { HighSensitiveRefinement = false }
// ...
};
/****************************************************
** Step 3: convert virastar config to json string **
****************************************************/
string jsonStr = JsonConvert.SerializeObject(new
{
Text = "حتما آن ها مومن را احترام مے ڪنند. یگ پنجره ی بزگ وسبز باز میشود . !حضور تان را کرامی می داشتم",
returnOnlyChangedTokens = false,
config.SpellConfiguration,
config.WordConfiguration,
config.CharConfiguration,
config.WritingRuleConfiguration,
config.IgnoreProcessConfiguration,
});
/**********************************************
********* Step 4: call virastar api **********
**********************************************/
var context = new StringContent(jsonStr, Encoding.UTF8, "application/json");
var response = client.PostAsync(BaseAddress + "Virastar/ScanText", context).Result;
Console.WriteLine($"Finished with status:{response.StatusCode}");
if (!response.IsSuccessStatusCode)
{
Console.WriteLine(response.ReasonPhrase);
}
else
{
/**********************************************
*** Step 5: read api response and using it ***
**********************************************/
string resp = response.Content.ReadAsStringAsync().Result;
// Method 1: using json object
JArray jsonArrayObj = JArray.Parse(resp);
var result1 = new StringBuilder();
foreach (JObject tokenInfo in jsonArrayObj.Children<JObject>())
{
result1.Append($"{tokenInfo["OriginalText"]}");
if (tokenInfo["IsChanged"]?.Value<bool>() ?? false)
{
JArray edits = (JArray) tokenInfo["EditList"];
if (edits?.Count > 1)
{
result1.Append($"{{{edits[0]["SuggestedText"]}({edits[0]["Description"]})");
for (int i = 1; i < edits.Count; i++)
{
result1.Append($" - {edits[i]["SuggestedText"]}({edits[i]["Description"]})");
}
result1.Append('}'); //.Remove(result.Length - 3, 3)
}
else if (edits?.Count == 1)
result1.Append($"{{{tokenInfo["NewText"]}({edits[0]["Description"]})}}");
}
}
Console.WriteLine(Environment.NewLine + result1);
// Method 2: deserialize json string to TokenInfo object
var tokens = JsonConvert.DeserializeObject<List<TokenInfo>>(resp);
var result2 = new StringBuilder();
if(tokens != null)
foreach (TokenInfo token in tokens)
{
result2.Append(token.OriginalText);
if (token.IsChanged)
{
if (token.EditList.Count > 1)
{
result2.Append($"{{{token.EditList[0].SuggestedText}({token.EditList[0].Description})");
for (int i = 1; i < token.EditList.Count; i++)
{
result2.Append(
$" - {token.EditList[i].SuggestedText}({token.EditList[i].Description})");
}
result2.Append('}');
}
else if (token.EditList?.Count == 1)
result2.Append($"{{{token.NewText}({token.EditList[0].Description})}}");
}
}
Console.WriteLine(Environment.NewLine + result2);
// Method 3: auto-apply first suggestion of virastar
//var tokens = JsonConvert.DeserializeObject<List<TokenInfo>>(resp);
if (tokens != null)
{
var tokenEdits = tokens.Select(t => new TokenInfoEdit(t)
{ApplyChangeIndex = t.IsChanged ? t.EditList.Count - 1 : -1}).ToList();
var result3 = new StringBuilder();
for (int j = 0; j < tokenEdits.Count; j++)
{
tokenEdits = ApplyTokensListChangesOnEachOther(tokenEdits, j);
result3.Append(tokenEdits[j].GetAppliedChange);
}
Console.WriteLine(Environment.NewLine + result3);
}
} // else response.IsSuccessStatusCode
} // using
Console.WriteLine("Please, Press any key to exit.");
Console.ReadKey();
}
/// <summary>
/// اعمال تغییرات مورد نظر کاربر بر روی یک عنصر از لیست توکنهای خروجی
/// </summary>
/// <remarks>
/// ممکن است انجام اصلاح پیشنهاد شده برای یک توکن منجر به حذف چند توکن بعدی شود (مانند ادغام عبارات مرکب) مثلاً: می شود (3 توکن «می»، « » و «شود») => میشود (1 توکن «میشود») ا
/// </remarks>
/// <param name="tokens"></param>
/// <param name="tokenIndex"></param>
/// <returns></returns>
public static List<TokenInfoEdit> ApplyTokensListChangesOnEachOther(List<TokenInfoEdit> tokens, int tokenIndex)
{
try
{
if (!tokens[tokenIndex].RemoveByOtherTokens && tokens[tokenIndex].ApplyChangeIndex >= 0 &&
tokens[tokenIndex].Token.EditList != null && tokens[tokenIndex].Token.EditList.Count > 0)
{
int removeCnt = tokens[tokenIndex].Token.EditList[tokens[tokenIndex].ApplyChangeIndex].RemoveTokensCount;
if (removeCnt != 0 && tokenIndex + removeCnt < tokens.Count && tokenIndex + removeCnt >= 0)
{
for (int j = 1; j <= Math.Abs(removeCnt); j++)
{
int removeIndex = tokenIndex + Math.Sign(removeCnt) * j;
tokens[removeIndex].RemoveByOtherTokens = true;
//tokens.RemoveAt(removeIndex);
}
}
}
}
catch //(Exception ex)
{
//log.FatalFormat(ex.Message);
}
return tokens;
}
}
}