Minimalist
Minimalist

Reputation: 79

Python: Finding the longest sentence in file

I have a file (a book actually) in .txt. I have to find the longest sentence in this file.

I tried:

print(max(open('file.txt', 'r'), key=len))

but it gives me only the longest line.

Not sure how I should do that properly. Any advices?

Upvotes: 2

Views: 3275

Answers (2)

Darkknight
Darkknight

Reputation: 1826

Well, this is basic. You are separating sentences by full-stop.

sentence = "study well for your future. study well for you. study well."
sentence_list = sentence.split(".")
length_of_sentence = [len(i) for i in sentence_list]
print(sentence_list[length_of_sentence.index(max(length_of_sentence))])

Upvotes: 0

PirateNinjas
PirateNinjas

Reputation: 2076

I would probably solve this using an off the shelf solution like NLTK.

NLTK has a sentence tokenizer that will render you text into sentences, accounting for variations like ! and ?, while also avoiding false positives like "etc."

For example:

import nltk

text = "this is a sentence. this is also a sentence. this is too."
sentences = nltk.sent_tokenize(text)
print(max(sentences, key=len))
# prints 'this is also a sentence.'

Upvotes: 3

Related Questions