-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrequencyAnalysisTask.cs
More file actions
82 lines (78 loc) · 2.81 KB
/
FrequencyAnalysisTask.cs
File metadata and controls
82 lines (78 loc) · 2.81 KB
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
using System.Collections.Generic;
using System.Linq;
namespace TextAnalysis
{
static class FrequencyAnalysisTask
{
public static Dictionary<string, string> GetMostFrequentNextWords(List<List<string>> text)
{
var result = new Dictionary<string, string>();
var workDictionary = new Dictionary<string, Dictionary<string, int>>();
foreach (var sentence in text)
{
for (var i = 0; i < sentence.Count - 1; i ++)
{
if (workDictionary.ContainsKey(sentence[i]))
{
if (workDictionary[sentence[i]].ContainsKey(sentence[i + 1]))
{
workDictionary[sentence[i]][sentence[i + 1]]++;
}
else
{
workDictionary[sentence[i]].Add(sentence[i+1],1);
}
}
else
{
var dict = new Dictionary<string, int> {{sentence[i + 1], 1}};
workDictionary.Add(sentence[i], dict);
}
}
for (var i = 0; i < sentence.Count-2; i++)
{
var key = sentence[i] + " " + sentence[i + 1];
if (workDictionary.ContainsKey(key))
{
if (workDictionary[key].ContainsKey(sentence[i + 2]))
{
workDictionary[key][sentence[i + 2]]++;
}
else
{
workDictionary[key].Add(sentence[i + 2], 1);
}
}
else
{
var dict = new Dictionary<string, int> { { sentence[i + 2], 1 } };
workDictionary.Add(key, dict);
}
}
}
foreach (var pair in workDictionary)
{
result.Add(pair.Key, pair.Value.Count == 1 ? pair.Value.Keys.First() : GetValue(pair.Value));
}
return result;
}
static string GetValue(Dictionary<string, int> dict)
{
var word = dict.Keys.First();
var frec = dict[word];
foreach (var pair in dict)
{
if (pair.Value > frec)
{
word = pair.Key;
frec = pair.Value;
}
else if (frec == pair.Value && string.CompareOrdinal(pair.Key, word) < 0)
{
word = pair.Key;
}
}
return word;
}
}
}