/
Censor.swift
124 lines (86 loc) · 3.42 KB
/
Censor.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
//
// Censor.swift
// FBSDKCoreKit
//
// Created by Vivek Kumar on 03/10/19.
//
import Foundation
class ProfanityResources {
class func profanityFileURL() -> URL? {
return Bundle(for: ProfanityResources.self).url(forResource: "Profanity", withExtension: "json")
}
}
struct ProfanityDictionary {
static let profaneWords: Set<String> = {
guard let fileURL = ProfanityResources.profanityFileURL() else {
return Set<String>()
}
do {
let fileData = try Data(contentsOf: fileURL, options: NSData.ReadingOptions.uncached)
guard let words = try JSONSerialization.jsonObject(with: fileData, options: []) as? [String] else {
return Set<String>()
}
return Set(words)
} catch {
return Set<String>()
}
}()
}
public struct ProfanityFilter {
static func censorString(_ string: String) -> String {
var cleanString = string
for word in string.profaneWords() {
let cleanWord = "".padding(toLength: word.count, withPad: "*", startingAt: 0)
cleanString = cleanString.replacingOccurrences(of: word, with: cleanWord, options: [.caseInsensitive], range: nil)
}
return cleanString
}
}
public extension String {
public func profaneWords() -> Set<String> {
var delimiterSet = CharacterSet()
delimiterSet.formUnion(CharacterSet.punctuationCharacters)
delimiterSet.formUnion(CharacterSet.whitespacesAndNewlines)
let words = Set(self.lowercased().components(separatedBy: delimiterSet))
return words.intersection(ProfanityDictionary.profaneWords)
}
public func containsProfanity() -> Bool {
return !profaneWords().isEmpty
}
public func censored() -> String {
return ProfanityFilter.censorString(self)
}
public mutating func censor() {
self = censored()
}
}
public extension NSString {
public func censored() -> NSString {
return ProfanityFilter.censorString(self as String) as NSString
}
}
public extension NSMutableString {
public func censor() {
setString(ProfanityFilter.censorString(self as String))
}
}
public extension NSAttributedString {
public func censored() -> NSAttributedString {
let profaneWords = string.profaneWords()
if profaneWords.isEmpty {
return self
}
let cleanString = NSMutableAttributedString(attributedString: self)
for word in profaneWords {
let cleanWord = "".padding(toLength: word.count, withPad: "*", startingAt: 0)
var range = (cleanString.string as NSString).range(of: word, options: .caseInsensitive)
while range.location != NSNotFound {
let attributes = cleanString.attributes(at: range.location, effectiveRange: nil)
let cleanAttributedString = NSAttributedString(string: cleanWord, attributes: attributes)
cleanString.replaceCharacters(in: range, with: cleanAttributedString)
range = (cleanString.string as NSString).range(of: word, options: .caseInsensitive)
}
}
return cleanString
}
}