Created
October 3, 2017 14:57
-
-
Save tikidunpon/f2a5ce2f87446e2c70780a1ff78cc38c to your computer and use it in GitHub Desktop.
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
// 05. n-gram | |
// 与えられたシーケンス(文字列やリストなど)からn-gramを作る関数を作成せよ.この関数を用い,"I am an NLPer"という文から単語bi-gram,文字bi-gramを得よ. | |
// 単語bi-gram | |
func q5_1(input: String, n: Int) -> [String] { | |
return wordNgram(input: input, n: n) | |
} | |
// 文字bi-gram | |
func q5_2(input: String, n: Int) -> [String] { | |
return charNgram(input: input, n: n) | |
} | |
func wordNgram(input: String, n: Int) -> [String] { | |
let words = input.components(separatedBy: " ").map{ $0.trimmingCharacters(in: CharacterSet(charactersIn: ",.")) } | |
return words.enumerated().flatMap{ (i, v) in | |
if words.indices.contains(i + n - 1) { | |
return words[i..<i+n].reduce("", +) | |
} else { | |
return nil | |
} | |
} | |
} | |
func charNgram(input: String, n: Int) -> [String] { | |
let words = input.components(separatedBy: " ") | |
.map{ $0.trimmingCharacters(in: CharacterSet(charactersIn: ",.")) } | |
.joined().characters.map{ String($0) } | |
return words.enumerated().flatMap{ (i, v) in | |
if words.indices.contains(i + n - 1) { | |
return words[i..<i+n].reduce("", +) | |
} else { | |
return nil | |
} | |
} | |
} | |
/// テストコード | |
func testQ5_1() { | |
let actual1 = q5_1(input: "I am an NLPer", n: 2) | |
let expected1 = ["Iam", "aman", "anNLPer"] | |
XCTAssertEqual(actual1, expected1) | |
let actual2 = q5_1(input: "I am a Swifter", n: 2) | |
let expected2 = ["Iam", "ama", "aSwifter"] | |
XCTAssertEqual(actual2, expected2) | |
let actual3 = q5_1(input: "This is a pen, Thease are erasers.", n: 2) | |
let expected3 = ["Thisis", "isa", "apen", "penThease", "Theaseare", "areerasers"] | |
XCTAssertEqual(actual3, expected3) | |
let actual4 = q5_1(input: "I am a Swifter", n: 3) | |
let expected4 = ["Iama", "amaSwifter"] | |
XCTAssertEqual(actual4, expected4) | |
let actual5 = q5_1(input: "This is a pen, Thease are erasers.", n: 4) | |
let expected5 = ["Thisisapen", "isapenThease", "apenTheaseare", "penTheaseareerasers"] | |
XCTAssertEqual(actual5, expected5) | |
} | |
func testQ5_2() { | |
let actual1 = q5_2(input: "I am an NLPer", n: 2) | |
let expected1 = ["Ia", "am", "ma", "an", "nN", "NL", "LP", "Pe", "er"] | |
XCTAssertEqual(actual1, expected1) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment