-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathQuoteParser.cs
More file actions
324 lines (281 loc) · 12.1 KB
/
Copy pathQuoteParser.cs
File metadata and controls
324 lines (281 loc) · 12.1 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
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using QuoteParser.Features;
using static QuoteParser.Features.QuoteMarkFeature;
using KeyPhrasesClass = QuoteParser.Features.KeyPhrases;
namespace QuoteParser
{
public class QuoteParser
{
private static Relation GetRelation(int startHeaderLinesIndex, int endHeaderLinesIndex, int quoteMarkIndex)
{
if (quoteMarkIndex < startHeaderLinesIndex)
return Relation.QuoteMarkFirst;
if (quoteMarkIndex > endHeaderLinesIndex)
return Relation.HeaderLinesFirst;
return Relation.QuoteMarkInHeaderLines;
}
private static bool IsTextBetween(int startIndex, int endIndex, IList<QuoteMarkMatchingResult> matchingLines)
{
return RangeHelper.Range(startIndex + 1, endIndex - 1).Any(i => matchingLines[i] == QuoteMarkMatchingResult.NotEmpty);
}
private static bool IsQuotedTextBetween(int startIndex, int endIndex, IList<QuoteMarkMatchingResult> matchingLines)
{
return RangeHelper.Range(startIndex + 1, endIndex - 1).Any(i => matchingLines[i] == QuoteMarkMatchingResult.VNotEmpty);
}
private static bool IsQuoteMarksAroundHeaderLines(int startHeaderLinesIndex,
int endHeaderLinesIndex,
int quoteMarkIndex,
IList<QuoteMarkMatchingResult> matchingLines)
{
bool headerLinesContainsQuoteMarks = RangeHelper.Range(startHeaderLinesIndex, endHeaderLinesIndex).All(i => matchingLines[i].HasQuoteMark);
if (headerLinesContainsQuoteMarks)
return true;
foreach (int i in RangeHelper.Range(endHeaderLinesIndex + 1, quoteMarkIndex))
{
if (matchingLines[i].HasQuoteMark)
return true;
if (matchingLines[i] == QuoteMarkMatchingResult.NotEmpty)
return false;
}
return true;
}
private readonly Builder _builder;
private readonly bool _deleteQuoteMarks;
private readonly bool _recursive;
private readonly QuoteMarkFeature _quoteMarkFeature;
private readonly QuoteHeaderLinesParser _quoteHeaderLinesParser;
private readonly QuoteMarkParser _quoteMarkParser;
private IList<string> _lines = new List<string>();
public class Builder
{
internal int _headerLinesCount = 3;
internal int _multiLineHeaderLinesCount = 6;
internal int _maxQuoteBlocksCount = 3;
internal int _minimumQuoteBlockSize = 7;
internal bool _deleteQuoteMarks = true;
internal bool _recursive;
internal IList<string> _keyPhrases = KeyPhrasesClass.Default;
public Builder HeaderLinesCount(int value)
{
_headerLinesCount = value;
return this;
}
public Builder MultiLineHeaderLinesCount(int value)
{
_multiLineHeaderLinesCount = value;
return this;
}
public Builder MinimumQuoteBlockSize(int value)
{
_minimumQuoteBlockSize = value;
return this;
}
public Builder DeleteQuoteMarks(bool value)
{
_deleteQuoteMarks = value;
return this;
}
public Builder Recursive(bool value)
{
_recursive = value;
return this;
}
public Builder KeyPhrases(IList<string> value)
{
_keyPhrases = value;
return this;
}
public QuoteParser Build()
{
return new QuoteParser(this);
}
}
private QuoteParser(Builder builder)
{
_builder = builder;
_deleteQuoteMarks = builder._deleteQuoteMarks;
_recursive = builder._recursive;
if (!_deleteQuoteMarks && _recursive)
throw new InvalidOperationException("Can't perform recursive parsing without deleting '>'");
_quoteMarkFeature = new QuoteMarkFeature();
_quoteHeaderLinesParser = new QuoteHeaderLinesParser(
builder._headerLinesCount,
builder._multiLineHeaderLinesCount,
builder._keyPhrases
);
_quoteMarkParser = new QuoteMarkParser(
builder._maxQuoteBlocksCount,
builder._minimumQuoteBlockSize
);
}
public Content Parse(string text, bool hasInReplyToEmlHeader = true)
{
return Parse(text.Split(new[] { "\n", "\r\n" }, StringSplitOptions.None), hasInReplyToEmlHeader);
}
public Content Parse(IEnumerable<string> lines, bool hasInReplyToEmlHeader = true)
{
_lines = lines.ToList();
var matchingLines = _quoteMarkFeature.MatchLines(_lines);
var headerLinesIndexes = _quoteHeaderLinesParser.Parse(_lines, matchingLines);
// This condition means: for EMLs without In-Reply-To header or References header
// search for quotation marks(>) only if quoteHeaderLines is null. It works well for
// the test data, but it is weird.. because it skips quotation marks most of the time.
//
// As alternative this condition may be deleted, but it works
// worse with some cases...
var quoteMarkIndex = (!hasInReplyToEmlHeader && headerLinesIndexes != null)
? null
: _quoteMarkParser.Parse(_lines, matchingLines);
if (headerLinesIndexes == null && quoteMarkIndex == null)
return new Content(_lines, null, null);
if (headerLinesIndexes != null && quoteMarkIndex == null)
return CreateContent(headerLinesIndexes.Value.Item1, headerLinesIndexes.Value.Item2 + 1, matchingLines);
if (headerLinesIndexes == null)
return CreateContent(quoteMarkIndex.Value, quoteMarkIndex.Value, matchingLines);
int startHeaderLinesIndex = headerLinesIndexes.Value.Item1;
int endHeaderLineIndex = headerLinesIndexes.Value.Item2;
var relation = GetRelation(
startHeaderLinesIndex,
endHeaderLineIndex,
quoteMarkIndex.Value
);
switch (relation)
{
case Relation.QuoteMarkInHeaderLines:
return CreateContent(
startHeaderLinesIndex,
endHeaderLineIndex + 1,
matchingLines
);
case Relation.QuoteMarkFirst:
return GetContentQuoteMarkFirstCase(
startHeaderLinesIndex,
endHeaderLineIndex,
quoteMarkIndex.Value,
matchingLines
);
case Relation.HeaderLinesFirst:
return GetContentHeaderLinesFirstCase(
startHeaderLinesIndex,
endHeaderLineIndex,
quoteMarkIndex.Value,
matchingLines
);
default:
throw new ArgumentOutOfRangeException();
}
}
private Content GetContentHeaderLinesFirstCase(int startHeaderLinesIndex, int endHeaderLineIndex, int quoteMarkIndex,
IList<QuoteMarkMatchingResult> matchingLines)
{
bool isTextBetween = IsTextBetween(endHeaderLineIndex, quoteMarkIndex, matchingLines);
bool isQuoteMarksAroundHeaderLines = IsQuoteMarksAroundHeaderLines(
startHeaderLinesIndex,
endHeaderLineIndex,
quoteMarkIndex,
matchingLines
);
if (!isTextBetween && !isQuoteMarksAroundHeaderLines)
{
throw new InvalidOperationException($"Relation = {Relation.HeaderLinesFirst}. Both isTextBetween and isHeaderLinesHasQuoteMark are false, but it can't be!");
}
if (isTextBetween && isQuoteMarksAroundHeaderLines)
{
return CreateContent(
quoteMarkIndex,
quoteMarkIndex,
matchingLines
);
}
else
{
return CreateContent(startHeaderLinesIndex, endHeaderLineIndex + 1, matchingLines);
}
}
private Content GetContentQuoteMarkFirstCase(int startHeaderLinesIndex, int endHeaderLineIndex, int quoteMarkIndex,
IList<QuoteMarkMatchingResult> matchingLines)
{
bool isTextBetween = IsTextBetween(quoteMarkIndex, startHeaderLinesIndex, matchingLines);
bool isQuotedTextBetween = IsQuotedTextBetween(quoteMarkIndex, startHeaderLinesIndex, matchingLines);
if (isTextBetween)
{
int i = startHeaderLinesIndex;
while (i > 0 && matchingLines[i - 1] != QuoteMarkMatchingResult.NotEmpty)
--i;
if (i == 0 || matchingLines.SubList(i, _lines.Count).Any(it => it.HasQuoteMark))
{
throw new InvalidOperationException($"Relation = {Relation.QuoteMarkFirst}. Found quoteMark(>), but in the lines after {i - 1} must not be any quote mark!");
}
return CreateContent(startHeaderLinesIndex, endHeaderLineIndex + 1, matchingLines);
}
else if (isQuotedTextBetween)
{
return CreateContent(
quoteMarkIndex,
quoteMarkIndex,
matchingLines
);
}
int startIndex = startHeaderLinesIndex;
if (matchingLines.SubList(startHeaderLinesIndex, endHeaderLineIndex + 1).All(it => it.HasQuoteMark))
{
while (startIndex > 0 && matchingLines[startIndex - 1].HasQuoteMark)
--startIndex;
}
return CreateContent(startIndex, endHeaderLineIndex + 1, matchingLines);
}
private Content CreateContent(int fromIndex,
int toIndex,
IList<QuoteMarkMatchingResult> matchingLines)
{
DeleteQuoteMarks(fromIndex, toIndex, matchingLines);
if (!_recursive)
{
return new Content(
_lines.SubList(0, fromIndex),
new QuoteHeader(fromIndex, toIndex, _lines.SubList(fromIndex, toIndex)),
new Content(
_lines.SubList(toIndex, _lines.LastIndex() - 1),
null,
null
)
);
}
else
{
return new Content(
_lines.SubList(0, fromIndex),
new QuoteHeader(fromIndex, toIndex, _lines.SubList(fromIndex, toIndex)),
Parse(_lines.SubList(toIndex, _lines.LastIndex() + 1))
);
}
}
private void DeleteQuoteMarks(int fromIndex,
int toIndex,
IList<QuoteMarkMatchingResult> matchingLines)
{
if (_deleteQuoteMarks &&
(fromIndex == toIndex ||
CheckQuoteMarkSuggestion(toIndex, _lines, matchingLines)))
{
_lines = _lines.Select((s, i) =>
{
string line = s.TrimStart();
if (i >= fromIndex)
{
if (line.StartsWith("> ")) return line.Substring(2);
if (line.StartsWith(">")) return line.Substring(1);
return s;
}
else
{
return s;
}
}).ToList();
}
}
}
}