我希望下面的函数将句子分成数组,将问题分成数组并插入"," "。"和"?"属于。目前它在同一阵列中打印。关于如何解决这个问题的任何想法?
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英里长。 Ned骑了多少英里?[句子数量= 2]
[" Ned骑自行车7英里到图书馆"," \ n他在回家的路上走了一条捷径,只有5英里长"," \ n如何Ned骑了多少英里?\ n"]
[问题数量= 1]
<&#34; Ned骑着他的自行车7英里到了图书馆。\ n他在回家的路上走了一条长达5英里的捷径。\ n Ned骑了多少英里&#34;,&#34; \ n&#34;] 奈德骑自行车7英里到图书馆。 他在回家的路上走了一条捷径,只有5英里长。 Ned骑了多少英里?打印出来
[句子数量= 2]
[问题数量= 1]
句子:[&#34; Ned骑自行车7英里到图书馆。他在回家的路上走了一条捷径,只有5英里长。&#34;]
问题:[&#34; Ned骑了多少英里?&#34;]
答案 0 :(得分:2)
此代码段可以使用一些重构来替换公共代码,但它可以按原样运行。
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
,我们提取标点符号并将其与之比较。还是?。
现在我们的代码将返回true或false,无论我们是否有问题或语句,我们使用filter
迭代所有句子的数组并返回一系列问题或陈述。
答案 1 :(得分:1)
我建议不要根据角色的存在进行分离,而是使用.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)")
答案 2 :(得分:-1)
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 ""
}
}
}