-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconsolidate.go
198 lines (176 loc) · 7.19 KB
/
consolidate.go
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
//Methods for consolidating (deduplicating) information found in Slides
package main
//Find duplicates by checking if intersecting rect shares >DUPLICATE_AREA_THRESHOLD of area of the smaller of the two rects.
//Passed in pointer to Destination slice is reassigned to new slice.
//Run time O(n^2). n = number of Destinations
func deleteDuplicatesFromDestinationArray(destsArrayPointer *[]Destination) {
dests := *destsArrayPointer
originalLength := len(dests)
for i := 0; i < len(dests); i++ {
destA := dests[i]
smallerArea := destA.BBox.Dx() * destA.BBox.Dy()
for j := i + 1; j < len(dests); j++ {
destB := dests[j]
destBArea := destA.BBox.Dx() * destA.BBox.Dy()
if destBArea < smallerArea {
smallerArea = destBArea
}
//Compare intersection image.Rectangle area to the smaller of destA and destB area
//OR if same destination terminal (predicted by fuzzy match) and on 50% same horizontal line due to fuzzy match individual words from same location
var horizontalDuplicate bool
if destA.TerminalTitle == destB.TerminalTitle {
horizontalDuplicate = sameHorizontalLine(destA.BBox, destB.BBox)
}
intersection := destA.BBox.Intersect(destB.BBox)
if float64(intersection.Dx())*float64(intersection.Dy()) > float64(smallerArea)*DUPLICATE_AREA_THRESHOLD || horizontalDuplicate {
//If destA spelling distance > destB spelling distance, replace destA location in array with destB.
if destA.SpellingDistance > destB.SpellingDistance {
dests[i] = dests[j]
}
//Delete destB location. Decrement j so that same index now with different element is checked on next loop
copy(dests[j:], dests[j+1:])
dests[len(dests)-1] = Destination{}
dests = dests[:len(dests)-1]
j--
}
}
}
//If duplicates were found, alloc new Destination slice and reassign passed in slice pointer
if len(dests) != originalLength {
//Create new slice to copy elements over. Original slice will have updated length but old elements in memory (displayed when printing).
tmp := make([]Destination, len(dests))
for i := 0; i < len(dests); i++ {
tmp[i] = dests[i]
}
*destsArrayPointer = tmp
}
}
//Find duplicates by checking if intersecting rect shares >DUPLICATE_AREA_THRESHOLD of area of the smaller of the two rects.
//Passed in pointer to RollCall slice is reassigned to new slice.
//Same function as deleteDuplicatesFromDestinationArray
//Run time O(n^2). n = number of RollCalls
func deleteDuplicatesFromRCArray(arrayPointer *[]RollCall) {
dests := *arrayPointer
originalLength := len(dests)
for i := 0; i < len(dests); i++ {
destA := dests[i]
smallerArea := destA.BBox.Dx() * destA.BBox.Dy()
for j := i + 1; j < len(dests); j++ {
destB := dests[j]
destBArea := destA.BBox.Dx() * destA.BBox.Dy()
if destBArea < smallerArea {
smallerArea = destBArea
}
//Compare intersection image.Rectangle area to the smaller of destA and destB area
intersection := destA.BBox.Intersect(destB.BBox)
if float64(intersection.Dx())*float64(intersection.Dy()) > float64(smallerArea)*DUPLICATE_AREA_THRESHOLD {
//Delete destB location. Decrement j so that same index now with different element is checked on next loop
copy(dests[j:], dests[j+1:])
dests[len(dests)-1] = RollCall{}
dests = dests[:len(dests)-1]
j--
}
}
}
//If duplicates were found, alloc new Destination slice and reassign passed in slice pointer
if len(dests) != originalLength {
//Create new slice to copy elements over. Original slice will have updated length but old elements in memory (displayed when printing).
tmp := make([]RollCall, len(dests))
for i := 0; i < len(dests); i++ {
tmp[i] = dests[i]
}
*arrayPointer = tmp
}
}
//Find duplicates by checking if intersecting rect shares >DUPLICATE_AREA_THRESHOLD of area of the smaller of the two rects.
//Passed in pointer to SeatsAvailable slice is reassigned to new slice.
//Same function as deleteDuplicatesFromDestinationArray
//Run time O(n^2). n = number of SeatsAvailable
func deleteDuplicatesFromSAArray(arrayPointer *[]SeatsAvailable) {
dests := *arrayPointer
originalLength := len(dests)
for i := 0; i < len(dests); i++ {
destA := dests[i]
smallerArea := destA.BBox.Dx() * destA.BBox.Dy()
for j := i + 1; j < len(dests); j++ {
destB := dests[j]
destBArea := destA.BBox.Dx() * destA.BBox.Dy()
if destBArea < smallerArea {
smallerArea = destBArea
}
//Compare intersection image.Rectangle area to the smaller of destA and destB area
intersection := destA.BBox.Intersect(destB.BBox)
if float64(intersection.Dx())*float64(intersection.Dy()) > float64(smallerArea)*DUPLICATE_AREA_THRESHOLD {
//If destA has no number found and destB found a number replace
if destA.Number == 0 && destB.Number != 0 || destB.Number > destA.Number {
dests[i] = dests[j]
}
//Delete destB location. Decrement j so that same index now with different element is checked on next loop
copy(dests[j:], dests[j+1:])
dests[len(dests)-1] = SeatsAvailable{}
dests = dests[:len(dests)-1]
j--
}
}
}
//If duplicates were found, alloc new Destination slice and reassign passed in slice pointer
if len(dests) != originalLength {
//Create new slice to copy elements over. Original slice will have updated length but old elements in memory (displayed when printing).
tmp := make([]SeatsAvailable, len(dests))
for i := 0; i < len(dests); i++ {
tmp[i] = dests[i]
}
*arrayPointer = tmp
}
}
//Delete terminal self matches from destination array
func deleteTerminalFromDestArray(arrayPointer *[]Destination, targetTerminal Terminal) {
dests := *arrayPointer
originalLength := len(dests)
//Find and remove any matching dests
for i := 0; i < len(dests); i++ {
if dests[i].TerminalTitle == targetTerminal.Title {
copy(dests[i:], dests[i+1:])
dests[len(dests)-1] = Destination{}
dests = dests[:len(dests)-1]
i--
}
}
//If duplicates were found, alloc new Destination slice and reassign passed in slice pointer
if len(dests) != originalLength {
//Create new slice to copy elements over. Original slice will have updated length but old elements in memory (displayed when printing).
tmp := make([]Destination, len(dests))
for i := 0; i < len(dests); i++ {
tmp[i] = dests[i]
}
*arrayPointer = tmp
}
}
//Delete destination matches where keyword (minY) is too low on slide
func deleteLowDestsFromDestArray(arrayPointer *[]Destination, sReference Slide) (err error) {
dests := *arrayPointer
originalLength := len(dests)
//Find and remove any matching dests
for i := 0; i < len(dests); i++ {
var destKeywordLow bool
if destKeywordLow, err = sReference.isYCoordinateWithinHeightPercentage(dests[i].BBox.Min.Y, DESTINATION_KEYWORD_VERTICAL_THRESHOLD); err != nil {
return
}
if !destKeywordLow {
copy(dests[i:], dests[i+1:])
dests[len(dests)-1] = Destination{}
dests = dests[:len(dests)-1]
i--
}
}
//If duplicates were found, alloc new Destination slice and reassign passed in slice pointer
if len(dests) != originalLength {
//Create new slice to copy elements over. Original slice will have updated length but old elements in memory (displayed when printing).
tmp := make([]Destination, len(dests))
for i := 0; i < len(dests); i++ {
tmp[i] = dests[i]
}
*arrayPointer = tmp
}
return
}