string - How to count words in a sentence of a text in multiple sentences in python -


i've searched around solution problem haven't found yet. have large text file divided sentences, separated "." need count how many words each sentence has , write file. i'm using separate file part of code , far have

    tekst = open('father_goriot.txt','r').read()     tekst = tekst.split('.') 

with 'list' type variable each sentence in it's own index. know if write

    print len(tekst[0].split()) 

i number of words in first sentence. need kind of loop number of words in each sentence. after need data written file in form: 1. index number of sentence in text, 2. number of words in particular sentence, 3. number of words in same sentence in different text (which translation of first text using code in seperate file), 4. number of words both sentences have in common. ideas?

after searching around while , simpler solution i've stumbled upon code gives me partial result of want. number of words in each sentence. it's represented list of numbers , looks this:

    wordcounts = []     open('father_goriot.txt') f:        text = f.read()        sentences = text.split('.')        sentence in sentences:            words = sentence.split(' ')            wordcounts.append(len(words)) 

but number incorrect because counts more. first sentence result of 40 instead of 38 words. how can fix this.


Popular posts from this blog