This repository has been archived by the owner on Dec 16, 2022. It is now read-only.
/
model.cs
139 lines (112 loc) · 4.14 KB
/
model.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ANDREICSLIB.ClassExtras;
namespace Phrase_Profiler
{
public class textoptions
{
public readonly bool ignoreHTML;
public readonly bool ignoreNonAlpha;
public textoptions(bool ignoreHTML, bool ignoreNonAlpha)
{
this.ignoreHTML = ignoreHTML;
this.ignoreNonAlpha = ignoreNonAlpha;
}
}
public class model
{
public List<List<string>> sentences;
public List<String> loadedFiles;
public model(List<List<string>> sentences,List<string> filenames )
{
this.sentences = sentences;
loadedFiles = filenames;
}
private static bool addCount(ref Dictionary<string, int> dic, string key)
{
bool existed = true;
if (dic.ContainsKey(key) == false)
{
dic.Add(key, 0);
existed = false;
}
dic[key]++;
return existed;
}
public enum responses
{
CantAdd, AddedNoPrevious, AddedToPrevious
}
private responses OKPhrase(ref Dictionary<string, int> dic, string key, List<String> excludedPhrases, List<string> requiredPhrases, int mincount = 1)
{
if ((requiredPhrases.Count > 0 && requiredPhrases.Any(key.Contains) == false)
|| (excludedPhrases.Count > 0 && excludedPhrases.Any(key.Contains)))
return responses.CantAdd;
if (key.Count(char.IsWhiteSpace) < mincount)
return responses.CantAdd;
bool hit = addCount(ref dic, key);
if (hit)
return responses.AddedToPrevious;
return responses.AddedNoPrevious;
}
public Dictionary<string, int> GetTopPhrases(int count, List<String> excludedPhrases, List<string> requiredPhrases, bool min3words, bool cull)
{
var phrases = new Dictionary<string, int>();
int reqspace = 1;
if (min3words)
reqspace = 3;
var parents = new HashSet<String>();
foreach (var s in sentences)
{
//get all combos
var xx = ListExtras.GetAllCombinations(s, 2, -1).OrderByDescending(pair => pair.Count).ToList();
if (xx.Count == 0)
continue;
String fullsentence = null;
foreach (var sl in xx)
{
var sentence = String.Join(" ", sl.ToArray());
if (fullsentence == null)
{
//empty value=no further parent
parents.Add(sentence);
fullsentence = sentence;
}
OKPhrase(ref phrases, sentence, excludedPhrases, requiredPhrases, reqspace);
}
}
var ret = new Dictionary<string, int>();
foreach (var kvp in phrases.OrderByDescending(pair => pair.Value).Take(count))
{
if (cull && kvp.Value == 1 && parents.Contains(kvp.Key))
continue;
ret.Add(kvp.Key, kvp.Value);
}
return ret;
}
public Dictionary<string, int> GetTopWords(int count, List<String> excludedWords, List<string> requiredWords)
{
var words = new Dictionary<string, int>();
foreach (var s in sentences)
{
foreach (var w in s)
{
if (excludedWords.Any(w.Equals))
continue;
if (requiredWords.Count > 0 && requiredWords.Any(w.Contains) == false)
continue;
addCount(ref words, w);
}
}
var ret = new Dictionary<string, int>();
foreach (var kvp in words.OrderByDescending(pair => pair.Value).Take(count))
{
ret.Add(kvp.Key, kvp.Value);
}
return ret;
}
}
}