我希望下面的函数将句子分隔成数组,将问题分隔到数组中,并插入",“。还有"?“属于自己。目前,它正在同一数组中同时打印。有什么办法解决这个问题吗?
func separateAllSentences() {
// needs to print just the sentences
func separateDeclarations() { // AKA Separate sentences that end in "."
if userInput.range(of: ".") != nil { // Notice how lowercased() wasn't used
numSentencesBefore = userInput.components(separatedBy: ".") // Hasn't subtracted 1 yet
numSentencesAfter = numSentencesBefore.count - 1
separateSentencesArray = Array(numSentencesBefore)
print("# Of Sentences = \(numSentencesAfter)")
print(separateSentencesArray)
} else {
print("There are no declarations found.")
}
}
// needs to print just the questions
func separateQuestions() { // Pretty Self Explanitory
if userInput.range(of: "?") != nil {
numQuestionsBefore = userInput.components(separatedBy: "?")
numQuestionsAfter = numQuestionsBefore.count - 1
separateQuestionsArray = Array(numQuestionsBefore)
print("# Of Questions = \(numQuestionsAfter)")
print(separateQuestionsArray)
} else {
print("There are no questions found. I have nothing to solve. Please rephrase the work to solve as a question.")
}
}
// TODO: - Separate Commas
func separateCommas() {
}
separateDeclarations()
separateQuestions()
}控制台输出:
奈德骑着自行车去图书馆7英里。他在回家的路上抄了一条捷径,那条路只有5英里长。内德总共骑了多少英里?
句子#=2
“奈德骑自行车7英里到图书馆”,“他在回家的路上抄了一条只有5英里长的捷径”,“内德总共骑了多少英里?”
问题#=1
奈德骑自行车7英里到图书馆。\n他在回家的路上抄了一条捷径,只有5英里长。\n内德总共骑了多少英里?
奈德骑着自行车去图书馆7英里。他在回家的路上抄了一条捷径,那条路只有5英里长。内德总共骑了多少英里?
它应该打印出
句子#=2
问题#=1
句子:“奈德骑自行车去图书馆7英里。他在回家的路上抄了一条捷径,那里只有5英里长。”
问题:“内德总共骑了多少英里?”
发布于 2016-11-23 17:49:59
我建议不要基于字符的存在而分开,而是使用.bySentences选项(它可以更优雅地处理不终止句子的标点符号)。然后遍历一次字符串,并附加到适当的数组,例如在Swift 3中:
var questions = [String]()
var statements = [String]()
var unknown = [String]()
let string = "Ned deployed version 1.0 of his app. He wrote very elegant code. How much money did Ned make?"
string.enumerateSubstrings(in: string.startIndex ..< string.endIndex, options: .bySentences) { string, _, _, _ in
if let sentence = string?.trimmingCharacters(in: .whitespacesAndNewlines), let lastCharacter = sentence.characters.last {
switch lastCharacter {
case ".":
statements.append(sentence)
case "?":
questions.append(sentence)
default:
unknown.append(sentence)
}
}
}
print("questions: \(questions)")
print("statements: \(statements)")
print("unknown: \(unknown)")发布于 2016-11-23 17:29:03
这个片段可以使用一些重构来替换通用代码,但它的工作原理是这样的。
let punctuation = CharacterSet(charactersIn: ".?")
let sentences = userInput.components(separatedBy: punctuation)
let questions = sentences.filter {
guard let range = userInput.range(of: $0) else { return false }
let start = range.upperBound
let end = userInput.index(after: start)
let punctuation = userInput.substring(with: Range(uncheckedBounds: (start, end)))
return punctuation == "?"
}
let statements = sentences.filter {
guard let range = userInput.range(of: $0) else { return false }
let start = range.upperBound
let end = userInput.index(after: start)
let punctuation = userInput.substring(with: Range(uncheckedBounds: (start, end)))
return punctuation == "."
}首先查看闭包,range变量在用户输入中包含句子的索引。我们想让标点符号尾随这个特定的句子,所以我们从它的上界开始,并找到下一个索引超过它。使用substring,我们提取标点符号并将其与任一标点符号进行比较。或者?
现在,无论我们有疑问句还是语句句,我们都有返回真或假的代码,我们使用filter来迭代所有句子的数组,并且只返回一组问题或语句。
发布于 2016-11-23 16:48:23
我做了一个最简单的解决方案

func separateDeclarations(userInput: String) { // AKA Separate sentences that end in "."
let array = userInput.components(separatedBy: ".")
let arrayQ = userInput.components(separatedBy: "?")
arrayQ.map { (string) -> String in
if let question = string.components(separatedBy: ".").last {
return question
} else {
return ""
}
}
array.map { (string) -> String in
if let question = string.components(separatedBy: "?").last {
return question
} else {
return ""
}
}
}https://stackoverflow.com/questions/40769472
复制相似问题