forked from spydaz/ClassTokenizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClassTokenizer.vb
682 lines (664 loc) · 28 KB
/
ClassTokenizer.vb
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
''' <summary>
''' Tokenizer For NLP Techniques
''' </summary>
<ComClass(ClassTokenizer.ClassId, ClassTokenizer.InterfaceId, ClassTokenizer.EventsId)>
Public Class ClassTokenizer
Public Const ClassId As String = "2899E490-7702-401C-BAB3-38FF97BC1AC9"
Public Const EventsId As String = "CD994307-F53E-401A-AC6D-3CF8086FD6F1"
Public Const InterfaceId As String = "8B9945F1-5D13-4059-829B-B531310144B5"
'Tokenizer
''' <summary>
''' Returns Characters in String as list
''' </summary>
''' <param name="InputStr"></param>
''' <returns></returns>
Public Function Tokenizer(ByRef InputStr As String) As List(Of String)
Tokenizer = New List(Of String)
InputStr = GetValidTokens(InputStr)
Dim Endstr As Integer = InputStr.Length
For i = 0 To Endstr
Tokenizer.Add(InputStr(i))
Next
End Function
''' <summary>
''' Return Tokens in string divided by seperator
''' </summary>
''' <param name="InputStr"></param>
''' <param name="Divider"> </param>
''' <returns></returns>
Public Function Tokenizer(ByRef InputStr As String, ByRef Divider As String) As List(Of String)
Tokenizer = New List(Of String)
InputStr = GetValidTokens(InputStr)
Dim Tokens() As String = InputStr.Split(Divider)
For Each item In Tokens
Tokenizer.Add(item)
Next
End Function
''' <summary>
''' Gets Frequency of token
''' </summary>
''' <param name="Token"></param>
''' <param name="InputStr"></param>
''' <returns></returns>
Public Function GetTokenFrequency(ByRef Token As String, ByRef InputStr As String) As Integer
GetTokenFrequency = 0
InputStr = GetValidTokens(InputStr)
If InputStr.Contains(Token) = True Then
For Each item In GetWordFrequecys(InputStr, " ")
If item.word = Token Then
GetTokenFrequency = item.frequency
End If
Next
End If
End Function
Private Function GetWordFrequecys(ByVal _Text As String, ByVal Delimiter As String) As List(Of WordFrequecys)
Dim Words As New WordFrequecys
Dim TempArray() As String = _Text.Split(Delimiter)
Dim WordList As New List(Of String)
Dim ListOfWordFrequecys As New List(Of WordFrequecys)
For Each word As String In TempArray
WordList.Add(word)
Next
Dim groups = WordList.GroupBy(Function(value) value)
For Each grp In groups
Words.word = grp(0)
Words.frequency = grp.Count
ListOfWordFrequecys.Add(Words)
Next
Return ListOfWordFrequecys
End Function
''' <summary>
''' each charcter can be defined as a particular token enabling for removal of unwanted token types
''' </summary>
''' <param name="CharStr"></param>
''' <returns></returns>
Public Function GetTokenType(ByRef CharStr As String) As TokenType
For Each item In SeperatorPunctuation
If CharStr = item Then Return TokenType.SeperatorPunctuation
Next
For Each item In GramaticalPunctuation
If CharStr = item Then Return TokenType.GramaticalPunctuation
Next
For Each item In EncapuslationPunctuationStart
If CharStr = item Then Return TokenType.EncapuslationPunctuationStart
Next
For Each item In EncapuslationPunctuationEnd
If CharStr = item Then Return TokenType.EncapuslationPunctuationEnd
Next
For Each item In MoneyPunctuation
If CharStr = item Then Return TokenType.MoneyPunctuation
Next
For Each item In MathPunctuation
If CharStr = item Then Return TokenType.MathPunctuation
Next
For Each item In CodePunctuation
If CharStr = item Then Return TokenType.CodePunctuation
Next
For Each item In AlphaBet
If CharStr = item Then Return TokenType.AlphaBet
Next
For Each item In Number
If CharStr = item Then Return TokenType.Number
Next
Return TokenType.Ignore
End Function
''' <summary>
''' Returns valid tokens only tokens that are not defined are removed
''' </summary>
''' <param name="InputStr"></param>
''' <returns></returns>
Public Function GetValidTokens(ByRef InputStr As String) As String
Dim EndStr As Integer = InputStr.Length
Dim CharStr As String = ""
For i = 0 To EndStr - 1
If GetTokenType(InputStr(i)) <> TokenType.Ignore Then
CharStr = CharStr.AddSuffix(InputStr(i))
Else
End If
Next
Return CharStr
End Function
Public Function AlphanumericOnly(ByRef Str As String) As String
Str = Str.GetValidTokens
Str = RemoveTokenType(Str, TokenType.CodePunctuation)
Str = RemoveTokenType(Str, TokenType.EncapuslationPunctuationEnd)
Str = RemoveTokenType(Str, TokenType.EncapuslationPunctuationStart)
Str = RemoveTokenType(Str, TokenType.MathPunctuation)
Str = RemoveTokenType(Str, TokenType.MoneyPunctuation)
Str = RemoveTokenType(Str, TokenType.GramaticalPunctuation)
Str = Str.Remove(",")
Str = Str.Remove("|")
Str = Str.Remove("_")
Return Str
End Function
''' <summary>
''' Removes Tokens From String by Type
''' </summary>
''' <param name="UserStr"></param>
''' <param name="nType"> </param>
''' <returns></returns>
Public Function RemoveTokenType(ByRef UserStr As String, ByRef nType As TokenType) As String
Select Case nType
Case TokenType.GramaticalPunctuation
For Each item In GramaticalPunctuation
If UCase(UserStr).Contains(UCase(item)) = True Then
UserStr = UCase(UserStr).Remove(UCase(item))
End If
Next
Case TokenType.AlphaBet
For Each item In AlphaBet
If UCase(UserStr).Contains(UCase(item)) = True Then
UserStr = UCase(UserStr).Remove(UCase(item))
End If
Next
Case TokenType.CodePunctuation
For Each item In CodePunctuation
If UCase(UserStr).Contains(UCase(item)) = True Then
UserStr = UCase(UserStr).Remove(UCase(item))
End If
Next
Case TokenType.EncapuslationPunctuationEnd
For Each item In EncapuslationPunctuationEnd
If UCase(UserStr).Contains(UCase(item)) = True Then
UserStr = UCase(UserStr).Remove(UCase(item))
End If
Next
Case TokenType.EncapuslationPunctuationStart
For Each item In EncapuslationPunctuationStart
If UCase(UserStr).Contains(UCase(item)) = True Then
UserStr = UCase(UserStr).Remove(UCase(item))
End If
Next
Case TokenType.Ignore
Case TokenType.MathPunctuation
For Each item In MathPunctuation
If UCase(UserStr).Contains(UCase(item)) = True Then
UserStr = UCase(UserStr).Remove(UCase(item))
End If
Next
Case TokenType.MoneyPunctuation
For Each item In MoneyPunctuation
If UCase(UserStr).Contains(UCase(item)) = True Then
UserStr = UCase(UserStr).Remove(UCase(item))
End If
Next
Case TokenType.Number
For Each item In Number
If UCase(UserStr).Contains(UCase(item)) = True Then
UserStr = UCase(UserStr).Remove(UCase(item))
End If
Next
Case TokenType.SeperatorPunctuation
For Each item In SeperatorPunctuation
If UCase(UserStr).Contains(UCase(item)) = True Then
UserStr = UCase(UserStr).Remove(UCase(item))
End If
Next
End Select
Return UserStr
End Function
'Form Extensions
''' <summary>
''' Counts tokens in string
''' </summary>
''' <param name="InputStr"></param>
''' <param name="Delimiter"></param>
''' <returns></returns>
Public Function CountTokens(ByRef InputStr As String, ByRef Delimiter As String) As Integer
Dim Words() As String = Split(InputStr, Delimiter)
Return Words.Count
End Function
''' <summary>
''' Checks if input contains Ecapuslation Punctuation
''' </summary>
''' <param name="Userinput"></param>
''' <returns></returns>
Public Function ContainsEncapsulated(ByRef Userinput As String) As Boolean
Dim Start = False
Dim Ending = False
ContainsEncapsulated = False
For Each item In EncapuslationPunctuationStart
If Userinput.Contains(item) = True Then Start = True
Next
For Each item In EncapuslationPunctuationEnd
If Userinput.Contains(item) = True Then Ending = True
Next
If Start And Ending = True Then
ContainsEncapsulated = True
End If
End Function
''' <summary>
''' Gets encapsulated items found in userinput
''' </summary>
''' <param name="USerinput"></param>
''' <returns></returns>
Public Function GetEncapsulated(ByRef Userinput As String) As List(Of String)
GetEncapsulated = New List(Of String)
Do Until ContainsEncapsulated(Userinput) = False
GetEncapsulated.Add(ExtractEncapsulated(Userinput))
Loop
End Function
''' <summary>
''' Extracts first Encapsulated located in string
''' </summary>
''' <param name="Userinput"></param>
''' <returns></returns>
Public Function ExtractEncapsulated(ByRef Userinput As String) As String
ExtractEncapsulated = Userinput
If ContainsEncapsulated(ExtractEncapsulated) = True Then
If ExtractEncapsulated.Contains("(") = True And ExtractEncapsulated.Contains(")") = True Then
ExtractEncapsulated = ExtractEncapsulated.ExtractStringBetween("(", ")")
End If
If Userinput.Contains("[") = True And Userinput.Contains("]") = True Then
ExtractEncapsulated = ExtractEncapsulated.ExtractStringBetween("[", "]")
End If
If Userinput.Contains("{") = True And Userinput.Contains("}") = True Then
ExtractEncapsulated = ExtractEncapsulated.ExtractStringBetween("{", "}")
End If
If Userinput.Contains("<") = True And Userinput.Contains(">") = True Then
ExtractEncapsulated = ExtractEncapsulated.ExtractStringBetween("<", ">")
End If
End If
End Function
End Class
''' <summary>
''' Grammer and syntax Components
''' </summary>
Public Module Constituants
''' <summary>
''' Recognized Tokens
''' </summary>
Public Enum TokenType
GramaticalPunctuation
EncapuslationPunctuationStart
EncapuslationPunctuationEnd
MoneyPunctuation
MathPunctuation
CodePunctuation
AlphaBet
Number
SeperatorPunctuation
Ignore
End Enum
Public ReadOnly AlphaBet() As String = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N",
"O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n",
"o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
Public ReadOnly CodePunctuation() As String = {"\", "#", "@", "^"}
Public ReadOnly EncapuslationPunctuationEnd() As String = {"}", "]", ">", ")"}
Public ReadOnly EncapuslationPunctuationStart() As String = {"{", "[", "<", "("}
Public ReadOnly GramaticalPunctuation() As String = {".", "?", "!", ":", ";"}
Public ReadOnly MathPunctuation() As String = {"+", "-", "=", "/", "*", "%", "PLUS", "ADD", "MINUS", "SUBTRACT", "DIVIDE", "DIFFERENCE", "TIMES", "MULTIPLY", "PERCENT", "EQUALS"}
Public ReadOnly MoneyPunctuation() As String = {"£", "$"}
Public ReadOnly Number() As String = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
"30", "40", "50", "60", "70", "80", "90", "00", "000", "0000", "00000", "000000", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen",
"nineteen", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety", "hundred", "thousand", "million", "Billion"}
Public ReadOnly SeperatorPunctuation() = {" ", ",", "|"}
Public Structure WordFrequecys
Public Overrides Function ToString() As String
Dim Str As String = ""
Str = Str & "Word :" & word & vbCrLf
Str = Str & "frequency :" & frequency & vbCrLf
Return Str
End Function
Public frequency As Integer
Public word As String
End Structure
End Module
''' <summary>
''' Extension Methods for tokeninzer Fucntions
''' </summary>
Public Module TokenizerExt
'Form Extensions
''' <summary>
''' Counts tokens in string
''' </summary>
''' <param name="InputStr"></param>
''' <param name="Delimiter"></param>
''' <returns></returns>
<System.Runtime.CompilerServices.Extension>
Public Function CountTokens(ByRef InputStr As String, ByRef Delimiter As String) As Integer
Dim Words() As String = Split(InputStr, Delimiter)
Return Words.Count
End Function
<Runtime.CompilerServices.Extension()>
Public Function AlphanumericOnly(ByRef Str As String) As String
Str = Str.GetValidTokens
Str = RemoveTokenType(Str, TokenType.CodePunctuation)
Str = RemoveTokenType(Str, TokenType.EncapuslationPunctuationEnd)
Str = RemoveTokenType(Str, TokenType.EncapuslationPunctuationStart)
Str = RemoveTokenType(Str, TokenType.MathPunctuation)
Str = RemoveTokenType(Str, TokenType.MoneyPunctuation)
Str = RemoveTokenType(Str, TokenType.GramaticalPunctuation)
Str = Str.Remove(",")
Str = Str.Remove("|")
Str = Str.Remove("_")
Return Str
End Function
''' <summary>
''' Removes Tokens From String by Type
''' </summary>
''' <param name="UserStr"></param>
''' <param name="nType"> </param>
''' <returns></returns>
<Runtime.CompilerServices.Extension()>
Public Function RemoveTokenType(ByRef UserStr As String, ByRef nType As TokenType) As String
Select Case nType
Case TokenType.GramaticalPunctuation
For Each item In GramaticalPunctuation
If UCase(UserStr).Contains(UCase(item)) = True Then
UserStr = UCase(UserStr).Remove(UCase(item))
End If
Next
Case TokenType.AlphaBet
For Each item In AlphaBet
If UCase(UserStr).Contains(UCase(item)) = True Then
UserStr = UCase(UserStr).Remove(UCase(item))
End If
Next
Case TokenType.CodePunctuation
For Each item In CodePunctuation
If UCase(UserStr).Contains(UCase(item)) = True Then
UserStr = UCase(UserStr).Remove(UCase(item))
End If
Next
Case TokenType.EncapuslationPunctuationEnd
For Each item In EncapuslationPunctuationEnd
If UCase(UserStr).Contains(UCase(item)) = True Then
UserStr = UCase(UserStr).Remove(UCase(item))
End If
Next
Case TokenType.EncapuslationPunctuationStart
For Each item In EncapuslationPunctuationStart
If UCase(UserStr).Contains(UCase(item)) = True Then
UserStr = UCase(UserStr).Remove(UCase(item))
End If
Next
Case TokenType.Ignore
Case TokenType.MathPunctuation
For Each item In MathPunctuation
If UCase(UserStr).Contains(UCase(item)) = True Then
UserStr = UCase(UserStr).Remove(UCase(item))
End If
Next
Case TokenType.MoneyPunctuation
For Each item In MoneyPunctuation
If UCase(UserStr).Contains(UCase(item)) = True Then
UserStr = UCase(UserStr).Remove(UCase(item))
End If
Next
Case TokenType.Number
For Each item In Number
If UCase(UserStr).Contains(UCase(item)) = True Then
UserStr = UCase(UserStr).Remove(UCase(item))
End If
Next
Case TokenType.SeperatorPunctuation
For Each item In SeperatorPunctuation
If UCase(UserStr).Contains(UCase(item)) = True Then
UserStr = UCase(UserStr).Remove(UCase(item))
End If
Next
End Select
Return UserStr
End Function
''' <summary>
''' Returns Characters in String as list
''' </summary>
''' <param name="InputStr"></param>
''' <returns></returns>
<Runtime.CompilerServices.Extension()>
Public Function Tokenizer(ByRef InputStr As String) As List(Of String)
Tokenizer = New List(Of String)
InputStr = GetValidTokens(InputStr)
Dim Endstr As Integer = InputStr.Length
For i = 0 To Endstr
Tokenizer.Add(InputStr(i))
Next
End Function
''' <summary>
''' Return Tokens in string divided by seperator
''' </summary>
''' <param name="InputStr"></param>
''' <param name="Divider"> </param>
''' <returns></returns>
<Runtime.CompilerServices.Extension()>
Public Function Tokenizer(ByRef InputStr As String, ByRef Divider As String) As List(Of String)
Tokenizer = New List(Of String)
InputStr = GetValidTokens(InputStr)
Dim Tokens() As String = InputStr.Split(Divider)
For Each item In Tokens
Tokenizer.Add(item)
Next
End Function
<Runtime.CompilerServices.Extension()>
Public Function GetTokenFrequency(ByRef Token As String, ByRef InputStr As String) As Integer
GetTokenFrequency = 0
InputStr = GetValidTokens(InputStr)
If InputStr.Contains(Token) = True Then
For Each item In GetWordFrequecys(InputStr, " ")
If item.word = Token Then
GetTokenFrequency = item.frequency
End If
Next
End If
End Function
Private Function GetWordFrequecys(ByVal _Text As String, ByVal Delimiter As String) As List(Of WordFrequecys)
Dim Words As New WordFrequecys
Dim TempArray() As String = _Text.Split(Delimiter)
Dim WordList As New List(Of String)
Dim ListOfWordFrequecys As New List(Of WordFrequecys)
For Each word As String In TempArray
WordList.Add(word)
Next
Dim groups = WordList.GroupBy(Function(value) value)
For Each grp In groups
Words.word = grp(0)
Words.frequency = grp.Count
ListOfWordFrequecys.Add(Words)
Next
Return ListOfWordFrequecys
End Function
''' <summary>
''' each charcter can be defined as a particular token enabling for removal of unwanted token types
''' </summary>
''' <param name="CharStr"></param>
''' <returns></returns>
<Runtime.CompilerServices.Extension()>
Public Function GetTokenType(ByRef CharStr As String) As TokenType
For Each item In SeperatorPunctuation
If CharStr = item Then Return TokenType.SeperatorPunctuation
Next
For Each item In GramaticalPunctuation
If CharStr = item Then Return TokenType.GramaticalPunctuation
Next
For Each item In EncapuslationPunctuationStart
If CharStr = item Then Return TokenType.EncapuslationPunctuationStart
Next
For Each item In EncapuslationPunctuationEnd
If CharStr = item Then Return TokenType.EncapuslationPunctuationEnd
Next
For Each item In MoneyPunctuation
If CharStr = item Then Return TokenType.MoneyPunctuation
Next
For Each item In MathPunctuation
If CharStr = item Then Return TokenType.MathPunctuation
Next
For Each item In CodePunctuation
If CharStr = item Then Return TokenType.CodePunctuation
Next
For Each item In AlphaBet
If CharStr = item Then Return TokenType.AlphaBet
Next
For Each item In Number
If CharStr = item Then Return TokenType.Number
Next
Return TokenType.Ignore
End Function
''' <summary>
''' Returns valid tokens only tokens that are not defined are removed
''' </summary>
''' <param name="InputStr"></param>
''' <returns></returns>
<Runtime.CompilerServices.Extension()>
Public Function GetValidTokens(ByRef InputStr As String) As String
Dim EndStr As Integer = InputStr.Length
Dim CharStr As String = ""
For i = 0 To EndStr - 1
If GetTokenType(InputStr(i)) <> TokenType.Ignore Then
CharStr = CharStr.AddSuffix(InputStr(i))
Else
End If
Next
Return CharStr
End Function
End Module
''' <summary>
''' Extension methods for string functions
''' </summary>
Public Module StringExtensions
''' <summary>
''' extracts string between defined strings
''' </summary>
''' <param name="value"> base sgtring</param>
''' <param name="strStart">Start string</param>
''' <param name="strEnd"> End string</param>
''' <returns></returns>
<System.Runtime.CompilerServices.Extension()>
Public Function ExtractStringBetween(ByVal value As String, ByVal strStart As String, ByVal strEnd As String) As String
If Not String.IsNullOrEmpty(value) Then
Dim i As Integer = value.IndexOf(strStart)
Dim j As Integer = value.IndexOf(strEnd)
Return value.Substring(i, j - i)
Else
Return value
End If
End Function
''' <summary>
''' Adds string to front of string (no spaces)
''' </summary>
''' <param name="Str"> base string</param>
''' <param name="Prefix">Add before (no spaces)</param>
''' <returns></returns>
<Runtime.CompilerServices.Extension()>
Public Function AddPrefix(ByRef Str As String, ByVal Prefix As String) As String
Return Prefix & Str
End Function
''' <summary>
''' Adds Suffix to String (No Spaces)
''' </summary>
''' <param name="Str"> Base string</param>
''' <param name="Suffix">To be added After</param>
''' <returns></returns>
<Runtime.CompilerServices.Extension()>
Public Function AddSuffix(ByRef Str As String, ByVal Suffix As String) As String
Return Str & Suffix
End Function
''' <summary>
''' Returns The last word in String
''' NOTE: String ois converted to Array then the last element is extracted Count-1
''' </summary>
''' <param name="InputStr"></param>
''' <returns>String</returns>
<System.Runtime.CompilerServices.Extension()>
Public Function ExtractLastWord(ByRef InputStr As String) As String
Dim TempArr() As String = Split(InputStr, " ")
Dim Count As Integer = TempArr.Count - 1
Return TempArr(Count)
End Function
<System.Runtime.CompilerServices.Extension()>
Public Function ExtractSuffix(ByRef InputStr As String) As String
Dim TempArr() As String = Split(InputStr, " ")
Dim Count As Integer = TempArr.Count - 1
Return TempArr(Count)
End Function
<System.Runtime.CompilerServices.Extension()>
Public Function ExtractLastChar(ByRef InputStr As String) As String
ExtractLastChar = Right(InputStr, 1)
End Function
<System.Runtime.CompilerServices.Extension()>
Public Function ExtractFirstWord(ByRef Statement As String) As String
Dim StrArr() As String = Split(Statement, " ")
Return StrArr(0)
End Function
<System.Runtime.CompilerServices.Extension()>
Public Function ExtractPrefix(ByRef Statement As String) As String
Dim StrArr() As String = Split(Statement, " ")
Return StrArr(0)
End Function
End Module
''' <summary>
''' Term Document matrix functions and Extension methods
''' </summary>
Public Module TermDocumentMatrixExtensions
''' <summary>
''' Returns n-Grams
''' </summary>
''' <param name="Str">Input Str</param>
''' <param name="Ngrams">number of Grams</param>
''' <returns></returns>
<Runtime.CompilerServices.Extension()>
Public Function GetNgrms(ByRef Str As String, ByRef Ngrams As Integer) As List(Of String)
Dim NgramArr() As String = Split(Str, " ")
Dim Length As Integer = NgramArr.Count
Dim lst As New List(Of String)
Dim Str2 As String = ""
For i = 0 To Length - Ngrams
Str2 = ""
Dim builder As New System.Text.StringBuilder()
builder.Append(Str2)
For j = 0 To Ngrams - 1
builder.Append(NgramArr(i + j) & " ")
Next
Str2 = builder.ToString()
lst.Add(Str2)
Next
Return lst
End Function
''' <summary>
''' IDF(t) = log_e(Total number of documents / Number of documents with term t in it).
''' </summary>
''' <param name="Word"> </param>
''' <param name="Documents"></param>
''' <returns></returns>
<System.Runtime.CompilerServices.Extension>
Public Function InverseTermDocumentFrequency(ByRef Word As String, ByRef Documents As List(Of String)) As Integer
InverseTermDocumentFrequency = 0
Dim Freqs As New List(Of Integer)
Dim Count As Integer = 0
For Each item In Documents
Freqs.Add(item.TermFrequency(Word))
If item.Contains(Word) = True Then
Count += 1
End If
Next
InverseTermDocumentFrequency = Math.Log(Documents.Count / Count)
End Function
''' <summary>
''' IDF: Inverse Document Frequency, which measures how important a term Is. While computing TF,
''' all terms are considered equally important. However it Is known that certain terms, such
''' As "is", "of", And "that", may appear a lot Of times but have little importance. Thus we
''' need To weigh down the frequent terms While scale up the rare ones, by computing the following:
''' IDF(t) = log_e(Total number of documents / Number of documents with term t in it).
''' </summary>
''' <param name="Userinput"></param>
''' <param name="Term"> </param>
''' <returns></returns>
<System.Runtime.CompilerServices.Extension>
Public Function InverseTermFrequency(ByRef Userinput As String, ByRef Term As String) As Double
Return Math.Log(GetTokenFrequency(Term, Userinput) / Userinput.CountTokens(" "))
End Function
''' <summary>
''' TF: Term Frequency, which measures how frequently a term occurs In a document. Since every
''' document Is different In length, it Is possible that a term would appear much more times
''' In Long documents than shorter ones. Thus, the term frequency Is often divided by the
''' document length (aka. the total number Of terms In the document) As a way Of normalization:
''' TF(t) = (Number of times term t appears in a document) / (Total number of terms in the document).
''' </summary>
''' <param name="InputStr"></param>
''' <param name="Term"> </param>
''' <returns></returns>
<System.Runtime.CompilerServices.Extension>
Public Function TermFrequency(ByRef InputStr As String, ByRef Term As String) As Double
Return Term.GetTokenFrequency(InputStr) / InputStr.CountTokens(" ")
End Function
End Module