-
Notifications
You must be signed in to change notification settings - Fork 0
/
MessageAnalysis.cs
160 lines (134 loc) · 4.38 KB
/
MessageAnalysis.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Sharpvin
{
internal class MessageAnalysis
{
public MessageAnalysis() { }
public (bool isCommand, bool isPolite, bool hasNumber) ScanMessage(String msg)
{
bool isCommand = false;
bool hasNumber = false;
for (int i = 0; i < msg.Length; i++)
{
if (msg[i] == 'v' && i > 1)
{
if (msg[i - 1] == 'e' && msg[i - 2] == 'k')
{
isCommand = true;
continue;
}
}
if (Char.IsNumber(msg[i]) && hasNumber == false)
{
hasNumber = true;
continue;
}
}
bool isPolite = msg.Contains("pleas") || msg.Contains("ples") || msg.Contains("pls");
return (isCommand, isPolite, hasNumber);
}
public (double value, String code_from, String code_to) FindConversion(String msg)
{
String[] words = msg.Split(' ');
String code_from = "", code_to = "";
double value = 0, tryvalue = 0;
bool isNumeric, valFound = false, toFound = false;
for (int i = 0; i < words.Length; i++)
{
String w = words[i].Replace('.', ',');
isNumeric = double.TryParse(w, out tryvalue);
if (isNumeric && i < words.Length - 1)
{
if (words[i + 1].Length == 3)
{
value = tryvalue;
valFound = true;
code_from = words[i + 1];
}
else
{
throw new Exception("I didn't understand the request, sorry");
}
}
if (words[i] == "to" && i < words.Length - 1)
{
if (words[i + 1].Length == 3)
{
toFound = true;
code_to = words[i + 1];
}
else
{
throw new Exception("I didn't understand the request, sorry");
}
}
}
if (toFound && valFound)
{
return (value, code_from, code_to);
}
else
{
throw new Exception("I didn't understand the request, sorry");
}
}
public List<String> FindOptions(String msg)
{
int kev_index = msg.IndexOf("kev");
String substring = msg.Substring(kev_index);
String[] words = substring.Split(' ');
String option = "";
List<String> options = new List<String>();
for (int i = 1; i < words.Length; i++)
{
words[i] = words[i].Trim('?');
if (words[i] == "or")
{
if (option != "")
options.Add(option);
option = "";
}
else if (words[i].EndsWith(','))
{
option = option + words[i].Trim(',') + " ";
options.Add(option);
option = "";
}
else
{
option = option + words[i] + " ";
}
}
if (option != "")
options.Add(option);
return options;
}
public String FindQuery(String msg)
{
bool forFound = false;
StringBuilder query = new StringBuilder();
String[] words = msg.Split(" ");
foreach (String word in words)
{
if (word == "for")
{
forFound = true;
continue;
}
if (forFound)
{
query.Append(word);
query.Append(' ');
}
}
if (!forFound)
return "";
else
return query.ToString();
}
}
}