Split paragraphs into Sentences

前端 未结 5 1196
囚心锁ツ
囚心锁ツ 2021-01-23 20:51

I have a large bunch of text. For example

I want to split a paragraph into sentences. But, there is a problem. My paragraph includes dates like Jan.13, 20

5条回答
  •  醉梦人生
    2021-01-23 21:51

    This is a rough version of I believe you were looking for: I an running a loop through the characters looking for the combination of ". "

    As the loop runs the characters are added to currentSentence String?. When the combination is found, the currentSentence is added to sentences[sentenceNumber].

    In addition, 2 exceptions have to be caught, the first whent he loop is on iteration 2 as period == index-1. The second is the last sentence as there is no space after the period.

    var paragraph = "I want to split a paragraph into sentences. But, there is a problem. My paragraph includes dates like Jan.13, 2014 , words like U.A.E abd numbers like 2.2. How do I split this."
    
    var sentences = [String]()
    var sentenceNumber = 0
    var currentSentence: String? = ""
    
    var charArray = paragraph.characters
    var period = 0
    
    for (index, char) in charArray.enumerate() {
        currentSentence! += "\(char)"
        if (char == ".") {
            period = index
    
            if (period == charArray.count-1) {
                sentences.append(currentSentence!)
            }
        } else if ((char == " " && period == index-1 && index != 1) || period == (charArray.count-1)) {
    
            sentences.append(currentSentence!)
            print(period)
            currentSentence = ""
            sentenceNumber++
        }
    }
    

提交回复
热议问题