How do the count the number of sentences, words and characters in a file?

前端 未结 7 1360
清歌不尽
清歌不尽 2020-12-10 06:26

I have written the following code to tokenize the input paragraph that comes from the file samp.txt. Can anybody help me out to find and print the number of sentences, words

7条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-10 06:45

    Not 100% correct but I just gave a try. I have not taken all points by @wilhelmtell in to consideration. I try them once I have time...

    if __name__ == "__main__":
       f = open("1.txt")
       c=w=0
       s=1
       prevIsSentence = False
       for x in f:
          x = x.strip()
          if x != "":
            words = x.split()
            w = w+len(words)
            c = c + sum([len(word) for word in words])
            prevIsSentence = True
          else:
            if prevIsSentence:
               s = s+1
            prevIsSentence = False
    
       if not prevIsSentence:
          s = s-1
       print "%d:%d:%d" % (c,w,s)
    

    Here 1.txt is the file name.

提交回复
热议问题