Python: Finding The Longest/Shortest Sentence In A Random Paragraph?

前端 未结 3 681
感情败类
感情败类 2021-01-27 02:51

I am using Python 2.7 and need 2 functions to find the longest and shortest sentence (in terms of word count) in a random paragraph. For exampl

相关标签:
3条回答
  • 2021-01-27 03:25

    EDIT: As has been mentioned in the comments below, programmatically determining what constitutes the sentences in a paragraph is quite a complex task. However, given the example you provided, I have elucidated a nice start to perhaps solving your problem below.

    First, we want to tokenize the paragraph into sentences. We do this by splitting the text on every occurrence of a . (period). This returns a list of strings, each of which is a sentence.

    We then want to break each sentence into its corresponding list of words. Then, using this list of lists, we want the sentence (represented as a list of words) whose length is a maximum and the sentence whose length is a minimum. Consider the following code:

    par = "Pair your seaside escape with the reds and whites of northern California's wine country in Jenner. This small coastal city in Sonoma County sits near the mouth of the Russian River, where, all summer long, harbor seals and barking California sea lions heave themselves onto the sand spit, sunning themselves for hours. You can swim and hike at Fort Ross State Historic Park and learn about early Russian hunters who were drawn to the area's herds of seal for their fur pelts. The fort's vineyard, with vines dating back to 1817, was one of the first places in California where grapes were planted."
    
    # split paragraph into sentences
    sentences = par.split(". ")
    
    # split each sentence into words
    tokenized_sentences = [sentence.split(" ") for sentence in sentences]
    
    # get longest sentence and its length
    longest_sen = max(tokenized_sentences, key=len)
    longest_sen_len = len(longest_sen)
    
    # get shortest word and its length
    shortest_sen = min(tokenized_sentences, key=len)
    shortest_sen_len = len(shortest_sen)
    
    print longest_sen_len
    print shortest_sen_len
    
    0 讨论(0)
  • 2021-01-27 03:32
    def MaxMinWords(paragraph):
        numWords = [len(sentence.split()) for sentence in paragraph.split('.')]
        return max(numWords), min(numWords)
    

    EDIT : As many have pointed out in the comments, this solution is far from robust. The point of this snippet is to simply serve as a pointer to the OP.

    0 讨论(0)
  • 2021-01-27 03:35

    You need a way to split the paragraph into sentences and to count words in a sentence. You could use nltk package for both:

    from nltk.tokenize import sent_tokenize, word_tokenize # $ pip install nltk
    
    sentences = sent_tokenize(paragraph)
    word_count = lambda sentence: len(word_tokenize(sentence))
    print(min(sentences, key=word_count)) # the shortest sentence by word count
    print(max(sentences, key=word_count)) # the longest sentence by word count
    
    0 讨论(0)
提交回复
热议问题