-
Notifications
You must be signed in to change notification settings - Fork 16
/
TextUnitBuilder.cs
88 lines (77 loc) · 2.73 KB
/
TextUnitBuilder.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
using System.Collections.Generic;
using System.Linq;
using OpenTextSummarizer.Interfaces;
namespace OpenTextSummarizer
{
internal class TextUnitBuilder : ITextUnitBuilder
{
internal Dictionary m_Rules { get; set; }
public TextUnitBuilder(Dictionary Rules)
{
m_Rules = Rules;
}
public TextUnit Build(string word)
{
var builtTextUnit = new TextUnit();
builtTextUnit.RawValue = word.ToLower();
builtTextUnit.FormattedValue = Format(builtTextUnit.RawValue);
builtTextUnit.Stem = Stem(builtTextUnit.FormattedValue);
if (builtTextUnit.Stem.Length <= 2)
{
builtTextUnit.Stem = builtTextUnit.FormattedValue;
}
return builtTextUnit;
}
internal string Stem(string word)
{
word = ReplaceWord(word, m_Rules.ManualReplacementRules);
word = StripPrefix(word, m_Rules.PrefixRules);
word = StripSuffix(word, m_Rules.SuffixRules);
word = ReplaceWord(word, m_Rules.SynonymRules);
return word;
}
internal string Format(string word)
{
word = StripPrefix(word, m_Rules.Step1PrefixRules);
word = StripSuffix(word, m_Rules.Step1SuffixRules);
return word;
}
public string StripSuffix(string word, Dictionary<string, string> suffixRules)
{
//not simply using .Replace() in this method in case the
//rule.Key exists multiple times in the string.
foreach (KeyValuePair<string, string> rule in suffixRules)
{
if (word.EndsWith(rule.Key))
{
word = word.Substring(0, word.Length - rule.Key.Length) + rule.Value;
}
}
return word;
}
internal string ReplaceWord(string word, Dictionary<string, string> replacementRules)
{
foreach (KeyValuePair<string, string> rule in replacementRules)
{
if (word == rule.Key)
{
return rule.Value;
}
}
return word;
}
internal string StripPrefix(string word, Dictionary<string, string> prefixRules)
{
//not simply using .Replace() in this method in case the
//rule.Key exists multiple times in the string.
foreach (KeyValuePair<string, string> rule in prefixRules)
{
if (word.StartsWith(rule.Key))
{
word = rule.Value + word.Substring(rule.Key.Length);
}
}
return word;
}
}
}