如何计算 Swift 中给定文本中的句子数?
How can I count the number of sentences in a given text in Swift?
我想创建一个 playground 来计算给定文本的句子数量。
let input = "That would be the text . it hast 3. periods. "
func sentencecount() {
let periods = CharacterSet.whitespacesAndNewlines.union(.punctuationCharacters)
let periods = input.components(separatedBy: spaces)
let periods2 = Int (words.count)
print ("The Average Sentence length is \(periods2)")
}
sentencecount()
这应该有效:
let input = "That would be the text . it hast 3. periods. "
let occurrencies = input.characters.filter { [=10=] == "." || [=10=] == "?" }.count
print(occurrencies)
//result 3
只需在 charset
中添加您要用来区分句子的字符:
我现在假设 ? . ,
:
let input = "That would be the text. it hast 3? periods."
let charset = CharacterSet(charactersIn: ".?,")
let arr = input.components(separatedBy: charset)
let count = arr.count - 1
此处 arr
将是:
["That would be the text", " it hast 3", " periods", ""]
将计数减 1,以获得实际句子。
注意:如果你不想考虑" , "
那么把它从charset
.
中去掉
据我所知,您需要使用 .并修剪空格如下:
func sentencecount () {
let result = input.trimmingCharacters(in: .whitespaces).split(separator: ".")
print ("The Average Sentence length is \(result.count)") // 3
}
祝你好运!
您可以使用 enumerateSubstrings(in: Range)
并使用选项 .bySentences
:
let input = "Hello World !!! That would be the text. It hast 3 periods."
var sentences: [String] = []
input.enumerateSubstrings(in: input.startIndex..., options: .bySentences) { (string, range, enclosingRamge, stop) in
sentences.append(string!)
}
另一种方法是使用子字符串数组而不是字符串:
var sentences: [Substring] = []
input.enumerateSubstrings(in: input.startIndex..., options: .bySentences) { (string, range, enclosingRamge, stop) in
sentences.append(input[range])
}
print(sentences) // "["Hello World !!! ", "That would be the text. ", "It hast 3 periods."]\n"
print(sentences.count) // "3\n"
我想创建一个 playground 来计算给定文本的句子数量。
let input = "That would be the text . it hast 3. periods. "
func sentencecount() {
let periods = CharacterSet.whitespacesAndNewlines.union(.punctuationCharacters)
let periods = input.components(separatedBy: spaces)
let periods2 = Int (words.count)
print ("The Average Sentence length is \(periods2)")
}
sentencecount()
这应该有效:
let input = "That would be the text . it hast 3. periods. "
let occurrencies = input.characters.filter { [=10=] == "." || [=10=] == "?" }.count
print(occurrencies)
//result 3
只需在 charset
中添加您要用来区分句子的字符:
我现在假设 ? . ,
:
let input = "That would be the text. it hast 3? periods."
let charset = CharacterSet(charactersIn: ".?,")
let arr = input.components(separatedBy: charset)
let count = arr.count - 1
此处 arr
将是:
["That would be the text", " it hast 3", " periods", ""]
将计数减 1,以获得实际句子。
注意:如果你不想考虑" , "
那么把它从charset
.
据我所知,您需要使用 .并修剪空格如下:
func sentencecount () {
let result = input.trimmingCharacters(in: .whitespaces).split(separator: ".")
print ("The Average Sentence length is \(result.count)") // 3
}
祝你好运!
您可以使用 enumerateSubstrings(in: Range)
并使用选项 .bySentences
:
let input = "Hello World !!! That would be the text. It hast 3 periods."
var sentences: [String] = []
input.enumerateSubstrings(in: input.startIndex..., options: .bySentences) { (string, range, enclosingRamge, stop) in
sentences.append(string!)
}
另一种方法是使用子字符串数组而不是字符串:
var sentences: [Substring] = []
input.enumerateSubstrings(in: input.startIndex..., options: .bySentences) { (string, range, enclosingRamge, stop) in
sentences.append(input[range])
}
print(sentences) // "["Hello World !!! ", "That would be the text. ", "It hast 3 periods."]\n"
print(sentences.count) // "3\n"