-
Notifications
You must be signed in to change notification settings - Fork 16
/
SummarizingEngine.cs
137 lines (122 loc) · 5.47 KB
/
SummarizingEngine.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
using OpenTextSummarizer.Interfaces;
using System;
using System.Linq;
namespace OpenTextSummarizer
{
/// <summary>
/// Orchestrates the different parts of the summarizing algorithm
/// </summary>
internal class SummarizingEngine
{
/// <summary>
/// Runs the content parsing part of the summarizing algorithm
/// </summary>
/// <param name="contentProvider"></param>
/// <param name="contentParser"></param>
/// <returns></returns>
public ParsedDocument ParseContent(IContentProvider contentProvider, IContentParser contentParser)
{
if (contentProvider == null)
{
throw new ArgumentNullException("contentProvider");
}
if (contentParser == null)
{
throw new ArgumentNullException("contentParser");
}
var resultingParsedDocument = new ParsedDocument();
resultingParsedDocument.Sentences = contentParser.SplitContentIntoSentences(contentProvider.Content);
if (resultingParsedDocument.Sentences == null)
{
throw new InvalidOperationException(string.Format("{0}.SplitContentIntoSentences must not return null", contentProvider.GetType().FullName));
}
foreach (var workingSentence in resultingParsedDocument.Sentences)
{
workingSentence.TextUnits = contentParser.SplitSentenceIntoTextUnits(workingSentence.OriginalSentence);
if (workingSentence.TextUnits == null)
{
throw new InvalidOperationException(string.Format("{0}.SplitSentenceIntoTextUnits must not return null", contentProvider.GetType().FullName));
}
}
return resultingParsedDocument;
}
/// <summary>
/// Runs the content analyzis part of the summarizing algorithm
/// </summary>
/// <param name="parsedDocument"></param>
/// <param name="contentAnalyzer"></param>
/// <returns></returns>
public AnalyzedDocument AnalyzeParsedContent(ParsedDocument parsedDocument, IContentAnalyzer contentAnalyzer)
{
if (parsedDocument == null)
{
throw new ArgumentNullException("parsedDocument");
}
if (contentAnalyzer == null)
{
throw new ArgumentNullException("contentAnalyzer");
}
var importantTextUnits = contentAnalyzer.GetImportantTextUnits(parsedDocument.Sentences);
if (importantTextUnits == null)
{
throw new InvalidOperationException(string.Format("{0}.GetImportantTextUnits must not return null", contentAnalyzer.GetType().FullName));
}
var scoredSentences = contentAnalyzer.ScoreSentences(parsedDocument.Sentences, importantTextUnits);
if (scoredSentences == null)
{
throw new InvalidOperationException(string.Format("{0}.ScoreSentences must not return null", contentAnalyzer.GetType().FullName));
}
return new AnalyzedDocument() { ScoredTextUnits = importantTextUnits.OrderByDescending(tus => tus.Score).ToList(), ScoredSentences = scoredSentences.OrderByDescending(ss => ss.Score).ToList() };
}
/// <summary>
/// Runs the content summarizing part of the summarizing algorithm
/// </summary>
/// <param name="analyzedDocument"></param>
/// <param name="contentSummarizer"></param>
/// <param name="arguments"></param>
/// <returns></returns>
public SummarizedDocument SummarizeAnalysedContent(AnalyzedDocument analyzedDocument, IContentSummarizer contentSummarizer, ISummarizerArguments arguments)
{
if (analyzedDocument == null)
{
throw new ArgumentNullException("analyzedDocument");
}
if (contentSummarizer == null)
{
throw new ArgumentNullException("contentSummarizer");
}
if (arguments == null)
{
throw new ArgumentNullException("arguments");
}
// Range adjustment
if (arguments.FilteringConceptsCap < 0)
{
arguments.FilteringConceptsCap = 0;
}
if (arguments.MaxSummarySentences < 0)
{
arguments.MaxSummarySentences = 0;
}
if (arguments.MaxSummarySizeInPercent < 0)
{
arguments.MaxSummarySizeInPercent = 0;
}
if (arguments.MaxSummarySizeInPercent > 100)
{
arguments.MaxSummarySizeInPercent = 100;
}
var summarizedConcepts = contentSummarizer.GetConcepts(analyzedDocument, arguments);
if (summarizedConcepts == null)
{
throw new InvalidOperationException(string.Format("{0}.GetConcepts must not return null", contentSummarizer.GetType().FullName));
}
var summarizedSentences = contentSummarizer.GetSentences(analyzedDocument, arguments);
if (summarizedSentences == null)
{
throw new InvalidOperationException(string.Format("{0}.GetSentences must not return null", contentSummarizer.GetType().FullName));
}
return new SummarizedDocument() { Concepts = summarizedConcepts, Sentences = summarizedSentences };
}
}
}