HI guys, I am having some trouble with some basic python. I need to make a function that reads a document and divides each sentence up and counts the lenght of the largest sentence. this is what i have so far


def linesize(F):
c= 0
W= open("file.txt")
X= W.read()
Xlines = X.split(".")
wordcount = len(Xlines)
print wordcount


this divides up the text into sentences but them it just adds them all up at the end.. any help would be appreciated. thank you guys

Member Avatar for masterofpuppets

hi
First, pls next time put code tags around your code :)
As for the program:

def lineSize():
    f = open( "filename.txt", "r" )
    lines = f.read()
    f.close()
    longestSentence = 0
    sentences = lines.split( "." )
    for sentence in sentences:
        if len( sentence ) > longestSentence:
            longestSentence = len( sentence )

    print longestSentence

lineSize()

This is a way of doing that. You'll probably have to deal with the \n and the punctuation as well :) Hope that helps!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.