forked from JetBrains/email-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuoteHeaderLinesParser.kt
287 lines (235 loc) · 9.93 KB
/
QuoteHeaderLinesParser.kt
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
/*
* Copyright 2016-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package quoteParser
import quoteParser.features.*
internal class QuoteHeaderLinesParser(val headerLinesCount: Int = 3,
val multiLIneHeaderLinesCount: Int = 6,
keyPhrases: List<String> = KeyPhrases.default) {
// For single line headers
private val featureSet: Array<AbstractQuoteFeature>
private val sufficientFeatureCount: Int
private val maxFeatureCount: Int
private var foundFeatureMap: MutableMap<String, Int> = mutableMapOf()
// ------
// For multi line and FWD headers
private var middleColonCount = 0
private val middleColonFeature = MiddleColonFeature()
private var lineMatchesMiddleColon = false
private var firstMiddleColonLineIndex = -1
// ------
// For phraseFeature
private var foundPhraseFeature = false
private var phraseFeatureLineIndex = -1
private val phraseFeature: PhraseFeature
// ------
private var lines: List<String> = listOf()
init {
this.phraseFeature = PhraseFeature(keyPhrases)
this.featureSet = arrayOf(
DateFeature(),
TimeFeature(),
EmailFeature(),
LastColonFeature()
)
this.maxFeatureCount = this.featureSet.size
this.sufficientFeatureCount = 2
}
private fun prepare() {
this.foundFeatureMap.clear()
this.middleColonCount = 0
this.lineMatchesMiddleColon = false
this.firstMiddleColonLineIndex = -1
this.foundPhraseFeature = false
this.phraseFeatureLineIndex = -1
}
internal fun parse(lines: List<String>,
matchedLinesQuoteMark: List<QuoteMarkMatchingResult> =
QuoteMarkFeature().matchLines(lines)): Pair<Int, Int>? {
this.prepare()
this.lines = lines
this.lines.forEachIndexed { lineIndex, line ->
this.resetSingleLineFeatures(oldLineIndex = lineIndex - this.headerLinesCount, all = false)
var anyFeatureMatches = false
this.featureSet.forEach { feature ->
if (feature.matches(line)) {
this.updateSingleLineFeature(lineIndex, feature)
anyFeatureMatches = true
}
}
if (this.middleColonFeature.matches(line)) {
this.updateMultiLineFeature(lineIndex)
anyFeatureMatches = true
}
if (this.phraseFeature.matches(line)) {
this.updatePhraseFeature(lineIndex)
}
if (anyFeatureMatches) {
this.resetFeatures(oldLineIndex = lineIndex - this.headerLinesCount)
} else {
this.resetFeatures(all = true)
}
if (this.headerFound(matchedLinesQuoteMark)) {
return@parse this.identifyHeader()
}
}
return null
}
private fun updatePhraseFeature(lineIndex: Int) {
this.foundPhraseFeature = true
this.phraseFeatureLineIndex = lineIndex
}
private fun headerFound(matchedLinesQuoteMark: List<QuoteMarkMatchingResult>) =
when {
this.foundPhraseFeature -> true
this.foundFeatureMap.size > this.sufficientFeatureCount -> true
this.foundFeatureMap.size == this.sufficientFeatureCount
&& this.checkForSecondaryFeatures(matchedLinesQuoteMark) -> true
else -> false
}
private fun checkForSecondaryFeatures(matchedLinesQuoteMark: List<QuoteMarkMatchingResult>): Boolean {
val sortedIndexes = this.foundFeatureMap.values.sorted()
val startIdx = sortedIndexes.first()
val endIdx = sortedIndexes.last()
return when {
checkMiddleColonSuggestion(startIdx, endIdx, this.lines, this.middleColonFeature) -> true
checkQuoteMarkSuggestion(endIdx, this.lines, matchedLinesQuoteMark) -> true
else -> false
}
}
private fun updateSingleLineFeature(lineIndex: Int, feature: AbstractQuoteFeature) {
if (feature is LastColonFeature) {
// LastColonFeature cannot be the first feature of the quote.
if (this.foundFeatureMap.size == 0) {
return
}
val sortedIndexes = this.foundFeatureMap.values.sorted()
val startIdx = sortedIndexes.first()
val endIdx = sortedIndexes.last()
// LastColonFeature cannot be in multi line header.
if (checkMiddleColonSuggestion(startIdx, endIdx, this.lines, this.middleColonFeature)) {
val headerLinesCount = endIdx - startIdx + 1
// It seems like MiddleColonFeature instead LastColonFeature.
if (this.middleColonCount <= headerLinesCount) {
this.updateMultiLineFeature(lineIndex)
// If line contains the real MiddleColonFeature
// then we should decrease its count.
if (this.middleColonFeature.matches(this.lines[lineIndex])) {
this.middleColonCount--
}
}
return
}
}
this.foundFeatureMap[feature.name] = lineIndex
}
private fun updateMultiLineFeature(lineIndex: Int) {
this.lineMatchesMiddleColon = true
if (this.middleColonCount == this.multiLIneHeaderLinesCount) {
this.firstMiddleColonLineIndex++
} else {
if (this.middleColonCount == 0) {
this.firstMiddleColonLineIndex = lineIndex
}
this.middleColonCount++
}
}
private fun resetFeatures(oldLineIndex: Int = -1, all: Boolean = false) {
this.resetSingleLineFeatures(oldLineIndex, all)
this.resetMultiLineFeatures()
}
private fun resetMultiLineFeatures(shouldReset: Boolean = true) {
if (!this.lineMatchesMiddleColon && shouldReset) {
this.middleColonCount = 0
}
this.lineMatchesMiddleColon = false
}
private fun resetSingleLineFeatures(oldLineIndex: Int, all: Boolean) {
if (all) {
this.foundFeatureMap.clear()
} else {
val temp: MutableMap<String, Int> = mutableMapOf()
this.foundFeatureMap.filterTo(temp) {
it.value > oldLineIndex
}
this.foundFeatureMap = temp
}
}
private fun identifyHeader(): Pair<Int, Int>? {
var fromIndex = Int.MAX_VALUE
var toIndex = Int.MIN_VALUE
if (this.foundPhraseFeature) {
fromIndex = this.phraseFeatureLineIndex
toIndex = this.phraseFeatureLineIndex
} else {
this.foundFeatureMap.forEach {
fromIndex = Math.min(fromIndex, it.value)
toIndex = Math.max(toIndex, it.value)
}
while (this.checkForAllRemainingFeatures(fromIndex, toIndex)) {
toIndex++
}
if (this.checkForMultiLineHeader(fromIndex, toIndex)) {
fromIndex = this.firstMiddleColonLineIndex
toIndex = this.firstMiddleColonLineIndex + this.middleColonCount - 1
}
}
return Pair(fromIndex, toIndex)
}
// If sufficient count of features had been found in less then HEADER_LINES_COUNT
// lines try to check others not found features in the following line.
private fun checkForAllRemainingFeatures(fromIndex: Int, toIndex: Int): Boolean {
val remainingFeatures = this.featureSet.filter { !this.foundFeatureMap.containsKey(it.name) }
if (remainingFeatures.isNotEmpty() && // One suggestion is not found.
toIndex < this.lines.size - 1 && // There is the following line to check.
toIndex - fromIndex + 1 < this.headerLinesCount) { // Found suggestions are placed in less than HEADER_LINES_COUNT lines.
val lineIndex = toIndex + 1
var anyFeatureMatches = false
remainingFeatures.forEach { feature ->
if (feature.matches(this.lines[lineIndex])) {
this.updateSingleLineFeature(lineIndex, feature)
anyFeatureMatches = true
}
}
if (anyFeatureMatches) {
if (this.middleColonFeature.matches(this.lines[lineIndex])) {
this.updateMultiLineFeature(lineIndex)
}
this.resetMultiLineFeatures()
return@checkForAllRemainingFeatures true
}
}
return false
}
// Check if there is a multi line header or FWD
private fun checkForMultiLineHeader(fromIndex: Int, toIndex: Int): Boolean {
if (this.middleColonCount >= toIndex - fromIndex + 1) {
var cnt = this.multiLIneHeaderLinesCount - this.middleColonCount
var lineIndex = toIndex + 1
while (cnt > 0 && lineIndex < this.lines.size) {
if (this.middleColonFeature.matches(this.lines[lineIndex])) {
this.updateMultiLineFeature(lineIndex)
} else {
break
}
lineIndex++
cnt--
}
return this.firstMiddleColonLineIndex <= fromIndex &&
this.firstMiddleColonLineIndex + this.middleColonCount - 1 >= toIndex
}
return false
}
}