-
Notifications
You must be signed in to change notification settings - Fork 13
Feature/language filter #98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
rmirabelli
wants to merge
3
commits into
BottleRocketStudios:main
Choose a base branch
from
rmirabelli:feature/languageFilter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
Sources/UtiliKit/LanguageFilter/String+LanguageFilter.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| // | ||
| // String+LanguageFilter.swift | ||
| // UtiliKit-iOS | ||
| // | ||
| // Created by Russell Mirabelli on 6/19/20. | ||
| // Copyright © 2020 Bottle Rocket Studios. All rights reserved. | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| public extension String { | ||
|
|
||
| /// Performs rot13 (dumb) encoding on a `UnicodeScalar` to provide minimal | ||
| /// filtering of bad words. | ||
| /// - Parameter unicodeScalar: The character to be `rot13`'ed | ||
| /// - Returns: the `rot13` value | ||
| private func rot13(unicodeScalar: UnicodeScalar) -> Character { | ||
| var result = unicodeScalar.value | ||
|
|
||
| switch unicodeScalar { | ||
| case "A"..."M", "a"..."m": | ||
| result += 13 | ||
| case "N"..."Z", "n"..."z": | ||
| result -= 13 | ||
| default: | ||
| break | ||
| } | ||
|
|
||
| return Character(UnicodeScalar(result) ?? " ") | ||
| } | ||
|
|
||
| /// Encodes / decodes a `String` using rot13 | ||
| /// - Parameter input: The `String` to be encoded / decoded | ||
| /// - Returns: The encoded or decoded `String` | ||
| private func rot13(input: String) -> String { | ||
| return String(input.unicodeScalars.map(rot13)) | ||
| } | ||
|
|
||
| /// A list of words which could be found to be offensive. Stored in source | ||
| /// using rot13. | ||
| private var offensiveWords: [String] { | ||
| ["nany", "nahf", "nefr", "nff", "onyyfnpx", "onyyf", "onfgneq", "ovgpu", "ovngpu", "oybbql", | ||
| "oybjwbo", "oybj wbo", "obyybpx", "obyybx", "obare", "obbo", "ohttre", "ohz", "ohgg", "ohggcyht", | ||
| "pyvgbevf", "pbpx", "pbba", "penc", "phag", "qnza", "qvpx", "qvyqb", "qlxr", "snt", "srpx", | ||
| "sryyngr", "sryyngvb", "srypuvat", "shpx", "s h p x", "shqtrcnpxre", "shqtr cnpxre", "synatr", | ||
| "Tbqqnza", "Tbq qnza", "uryy", "ubzb", "wrex", "wvmm", "xvxr", "xaboraq", "xabo raq", "ynovn", | ||
| "yznb", "yzsnb", "zhss", "avttre", "avttn", "bzt", "cravf", "cvff", "cbbc", "cevpx", "chor", | ||
| "chffl", "dhrre", "fpebghz", "frk", "fuvg", "f uvg", "fu1g", "fyhg", "fzrtzn", "fchax", "gvg", | ||
| "gbffre", "gheq", "gjng", "intvan", "jnax", "juber", "jgs" | ||
| ].map { rot13(input: $0) } | ||
| } | ||
|
|
||
| /// Returns true if the `String` matches a set of potentially offensive | ||
| /// words. | ||
| var containsOffensiveLanguage: Bool { | ||
| !(offensiveWords.filter { self.contains($0) }.isEmpty) | ||
| } | ||
|
|
||
|
|
||
| /// Returns a `String` where potentially offensive language is replaced | ||
| /// with asterisks | ||
| var removingOffensiveLanguage: String { | ||
| self.replacingOffensiveWords(with: "*") | ||
| } | ||
|
|
||
| /// Replaces offensive words with the provided `String` (a single character is recommended) | ||
| /// - Parameter replacement: The `String` to use for replacement | ||
| /// - Returns: The string with offensive words replaced. | ||
| func replacingOffensiveWords(with substitute: String) -> String { | ||
| if !self.containsOffensiveLanguage { return self } | ||
| var replacement = self | ||
| for word in offensiveWords { replacement = replacement.replacingOccurrences(of: word, with: String(repeating: (substitute.first ?? "*"), count: word.count)) } | ||
| return replacement | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| // | ||
| // LanguageFilterTests.swift | ||
| // UtiliKit-iOSTests | ||
| // | ||
| // Created by Russell Mirabelli on 6/19/20. | ||
| // Copyright © 2020 Bottle Rocket Studios. All rights reserved. | ||
| // | ||
|
|
||
| import UtiliKit | ||
| import XCTest | ||
|
|
||
| class LanguageFilterTests: XCTestCase { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well this is certainly a... unique... set of tests 😄 |
||
|
|
||
| func test_noOffensiveLanguageDoesNotReturnTrue() { | ||
| // arrange | ||
| let unoffensive = "angel" | ||
| // act | ||
| let containsOffensiveLanguage = unoffensive.containsOffensiveLanguage | ||
| // assert | ||
| XCTAssert(!containsOffensiveLanguage, "Should not trigger the offensive language filter") | ||
| } | ||
|
|
||
| func test_yesOffensiveLanguageDoesReturnTrue() { | ||
| // arrange | ||
| let offensive = "ass" | ||
| // act | ||
| let containsOffensiveLanguage = offensive.containsOffensiveLanguage | ||
| // assert | ||
| XCTAssert(containsOffensiveLanguage, "Should not trigger the offensive language filter") | ||
| } | ||
|
|
||
| func test_replacesWithCorrectLength() { | ||
| //arrange | ||
| let offensive = "bite my shiny metal ass" | ||
| // act | ||
| let replaced = offensive.removingOffensiveLanguage | ||
| let asterisks = replaced.filter { $0 == "*" } | ||
| // assert | ||
| XCTAssert(asterisks.count == 3, "Incorrect replacement length in \(replaced)") | ||
| } | ||
|
|
||
| func test_doesNotReplaceInoffensive() { | ||
| // arrange | ||
| let unoffensive = "my hovercraft is full of eels" | ||
| // act | ||
| let replaced = unoffensive.removingOffensiveLanguage | ||
| // assert | ||
| XCTAssert(!replaced.contains("*"), "Incorrect replacement in \(replaced)") | ||
| } | ||
|
|
||
| func test_replacesWithAlternateCharacter() { | ||
| //arrange | ||
| let offensive = "bite my shiny metal ass" | ||
| // act | ||
| let replaced = offensive.replacingOffensiveWords(with: "•") | ||
| let asterisks = replaced.filter { $0 == "•" } | ||
| // assert | ||
| XCTAssert(asterisks.count == 3, "Incorrect replacement length in \(replaced)") | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like we're missing the basic how-to from here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whoops!
I guess at least I put in the todo. I wrote that before I decided how I'd implement it. :)