Skip to content

Commit

Permalink
Merge pull request #6 from ensan-hcl/add_remove_api
Browse files Browse the repository at this point in the history
Add remove api
  • Loading branch information
ensan-hcl authored Aug 25, 2021
2 parents 87ef611 + 1c30cf4 commit 8e3e46f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 17 deletions.
15 changes: 15 additions & 0 deletions Sources/KaomojiParser/KaomojiParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -278,4 +278,19 @@ public struct KaomojiParser {
let result = splited.flatMap{kaomojiExtract(from: String($0), G: tolerance.gValue, L: minimumLength)}
return Set(result)
}

/// Remove kaomoji inside the text.
/// - Parameters:
/// - string: target string to search kaomoji.
/// - tolerance: how torelantly judge whether a part of text is kaomoji or not
/// - minimumLength: minimum length of a kaomoji
/// - Returns: found kaomoji set
public func removeKaomoji(from string: String, tolerance: Tolerance = .normal, minimumLength: Int = 3) -> String {
let splited = string.split(separator: "\n")
var result = string
for kaomoji in Set(splited.flatMap{kaomojiExtract(from: String($0), G: tolerance.gValue, L: minimumLength)}) {
result = result.replacingOccurrences(of: kaomoji, with: "")
}
return result
}
}
44 changes: 27 additions & 17 deletions Tests/KaomojiParserTests/KaomojiParserTests.swift
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
import XCTest
@testable import KaomojiParser
import XCTest
@testable import KaomojiParser

final class KaomojiParserTests: XCTestCase {
func testHalfKatakana() {
let parser = KaomojiParser()
XCTAssertEqual(parser.isHalfKatakana(""), true)
XCTAssertEqual(parser.isHalfKatakana(""), false)
XCTAssertEqual(parser.isHalfKatakana("a"), false)
}
final class KaomojiParserTests: XCTestCase {
func testHalfKatakana() {
let parser = KaomojiParser()
XCTAssertEqual(parser.isHalfKatakana(""), true)
XCTAssertEqual(parser.isHalfKatakana(""), false)
XCTAssertEqual(parser.isHalfKatakana("a"), false)
}

func testSearch() {
let parser = KaomojiParser()
XCTAssertEqual(parser.search(in: "嬉しいです(≧▽≦)"), ["(≧▽≦)"])
XCTAssertEqual(parser.search(in: "地震だ!┗(^o^;)┓"), ["┗(^o^;)┓"])
XCTAssertEqual(parser.search(in: "またね(・Д・)ノ"), ["(・Д・)ノ"])
XCTAssertEqual(parser.search(in: "ホームランキタ━━━━(゚∀゚)━━━━‼︎"), ["キタ━━━━(゚∀゚)━━━━‼︎"])
XCTAssertEqual(parser.search(in: "嫌い(`ε´) 絶交しよ(  ̄っ ̄)"), ["(`ε´) ", "(  ̄っ ̄)"])
}

func testParser() {
let parser = KaomojiParser()
XCTAssertEqual(parser.search(in: "嬉しいです(≧▽≦)"), ["(≧▽≦)"])
XCTAssertEqual(parser.search(in: "地震だ!┗(^o^;)┓"), ["┗(^o^;)┓"])
XCTAssertEqual(parser.search(in: "またね(・Д・)ノ"), ["(・Д・)ノ"])
XCTAssertEqual(parser.search(in: "ホームランキタ━━━━(゚∀゚)━━━━‼︎"), ["キタ━━━━(゚∀゚)━━━━‼︎"])
XCTAssertEqual(parser.search(in: "嫌い(`ε´) 絶交しよ(  ̄っ ̄)"), ["(`ε´) ", "(  ̄っ ̄)"])
}
func testRemove() {
let parser = KaomojiParser()
XCTAssertEqual(parser.removeKaomoji(from: "嬉しいです(≧▽≦)"), "嬉しいです")
XCTAssertEqual(parser.removeKaomoji(from: "地震だ!┗(^o^;)┓"), "地震だ!")
XCTAssertEqual(parser.removeKaomoji(from: "またね(・Д・)ノ"), "またね")
XCTAssertEqual(parser.removeKaomoji(from: "ホームランキタ━━━━(゚∀゚)━━━━‼︎"), "ホームラン")
XCTAssertEqual(parser.removeKaomoji(from: "嫌い(`ε´) 絶交しよ(  ̄っ ̄)"), "嫌い絶交しよ")
}

}

0 comments on commit 8e3e46f

Please sign in to comment.