/
EmojiCollection.swift
167 lines (127 loc) · 6.08 KB
/
EmojiCollection.swift
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
//
// EmojiCollection.swift
// EmojiWorkshop
//
// Created by John Pavley on 12/12/17.
// Copyright © 2017 Epic Loot. All rights reserved.
//
import Foundation
typealias GlyphIDList = [Int]
struct DiagosticsData: CustomStringConvertible {
var description: String {
return " totalEmojiFound \(totalEmojiFound)\n totalSectionsFound \(totalSectionsFound)\n totalLinesProcessed \(totalLinesProcessed)\n totalSuggestionsFound \(totalSuggestionsFound)\n firstIndexFound \(firstIndexFound)\n lastIndexFound \(lastIndexFound)"
}
var totalEmojiFound = 0
var totalSectionsFound = 0
var totalLinesProcessed = 0
var totalSuggestionsFound = 0
var firstIndexFound = 0
var lastIndexFound = 0
}
class EmojiCollection {
var emojiGlyphs: [EmojiGlyph]
var filteredEmojiGlyphs: [EmojiGlyph]
var searchSuggestions: TagAndCountsList
var filteredSearchSuggestions: TagAndCountsList
var glyphsIDsInSections: [GlyphIDList]
var sectionNames: [String]
var sectionStartIndexes: [Int]
var stemmer: SimpleStemmer
var synonymmer: SimpleSynonymmer
var tagger: SimpleTagger
var searcher: EmojiSearch
var diagosticData = DiagosticsData()
struct UnsupportedEmoji {
static let unitedNationsFlag = " 🇺🇳"
}
fileprivate func isEmojiInSection(glyph: EmojiGlyph, section: String) -> Bool {
let glyphSectionName = "\(glyph.group): \(glyph.subgroup)"
return section == glyphSectionName
}
/// Organizes emoji glyph IDs into sections based on group and subgroup.
/// Throws away any sections without associated emoji.
fileprivate func createGlyphsInSections() {
var glyphIDs: GlyphIDList
var cleanedUpSections = [String]()
for section in sectionNames {
glyphIDs = emojiGlyphs.filter { isEmojiInSection(glyph: $0, section: section) }.map { $0.index }
if glyphIDs.count > 0 {
glyphsIDsInSections.append(glyphIDs)
cleanedUpSections.append(section)
}
}
sectionNames = cleanedUpSections
}
/// Initializes EmojiCollection from a source file. The file has to be a W3C
/// emoji test file stored locally.
init?(sourceFileName: String) {
emojiGlyphs = [EmojiGlyph]()
filteredEmojiGlyphs = [EmojiGlyph]()
searchSuggestions = TagAndCountsList()
filteredSearchSuggestions = TagAndCountsList()
glyphsIDsInSections = [[Int]]()
sectionNames = [String]()
sectionStartIndexes = [Int]()
stemmer = SimpleStemmer()!
synonymmer = SimpleSynonymmer()!
tagger = SimpleTagger()!
searcher = EmojiSearch(stemmer: stemmer, synonymmer: synonymmer, tagger: tagger)
diagosticData.totalEmojiFound = 0
diagosticData.totalSectionsFound = 0
diagosticData.totalLinesProcessed = 0
diagosticData.totalSuggestionsFound = 0
diagosticData.firstIndexFound = 0
diagosticData.lastIndexFound = 0
if let txtPath = Bundle.main.path(forResource: sourceFileName, ofType: "txt") {
do {
let emojiTestText = try String(contentsOfFile: txtPath, encoding: .utf8)
let emojiTestLines = emojiTestText.split(separator: "\n")
diagosticData.totalLinesProcessed = emojiTestLines.count
var group = ""
var subgroup = ""
var emojiIndex = 0
for line in emojiTestLines {
if line.contains(UnsupportedEmoji.unitedNationsFlag) {
// iOS 11.2 does not support the UN Flag Emoji
continue
}
if line.contains("# group: ") {
group = String(line[line.index(line.startIndex, offsetBy: "# group: ".count)...])
}
if line.contains("# subgroup: ") {
subgroup = String(line[line.index(line.startIndex, offsetBy: "# subgroup: ".count)...])
}
let sectionName = "\(group): \(subgroup)"
if var emojiGlyph = EmojiGlyph(textLine: String(line), index: 0, group: group, subgroup: subgroup) {
// print(emojiGlyph)
emojiIndex += 1
emojiGlyph.index = emojiIndex
emojiGlyph.tags = createMetadata(glyph: emojiGlyph, stemmer: stemmer, synonymmer: synonymmer, tagger: tagger)
emojiGlyphs.append(emojiGlyph)
}
if !sectionNames.contains(sectionName) {
sectionNames.append(sectionName)
sectionStartIndexes.append(emojiIndex)
}
}
// dedupe the section start indexs
let sectionStartIndexesSet: Set = Set(sectionStartIndexes)
sectionStartIndexes = Array(sectionStartIndexesSet)
sectionStartIndexes = sectionStartIndexes.sorted()
} catch {
print("emoji-test.txt file not found")
return nil
}
}
emojiGlyphs = tagger.tagAdjustment(emojiGlyphs: emojiGlyphs)
diagosticData.totalEmojiFound = emojiGlyphs.count
diagosticData.firstIndexFound = emojiGlyphs.first!.index
diagosticData.lastIndexFound = emojiGlyphs.last!.index
createGlyphsInSections()
diagosticData.totalSectionsFound = glyphsIDsInSections.count
searchSuggestions = searcher.getSuggestions(emojiGlyphs: emojiGlyphs)
filteredSearchSuggestions = searchSuggestions
diagosticData.totalSuggestionsFound = searchSuggestions.count
// print("\(diagosticData)")
}
}