0

Trying to get def countnonarticlewords to count the number of Non-Article Words in a mobydick text file. The words I am trying to not count are 'a','an' and'the'.Any hints on how I could modify def countnonarticlewords to output Example #1. Currently it is outputting Example #2

Example #1

******* mobydick.txt ******* 
Total Lines: 15604
Total Chars: 512293
Total Words: 115314
**Total Non-Article Words: 105479**

Example #2

 Enter the test file name: mobydick.txt
*****mobydick.txt*****
Total Lines: 15604
Total Chars: 512293
Total Words: 115314
**Total Non-Article Words: 15604**  

What I have so far:

def getTextFile():
        filename=input("Enter the test file name: ")
        textFile=open(filename, 'r')
        return filename,textFile

def outputcountresults(filename, linecount, charcount, wordcount, nonarticlewordcount):
    print("*****{}*****".format(filename))
    print("Total Lines: {}".format(linecount))
    print("Total Chars: {}".format(charcount))
    print("Total Words: {}".format(wordcount))
    print("Total Non-Article Words: {}".format(nonarticlewordcount))

def countcharacters(line):
    charcount=0
    for c in line:
        if not c.isspace():
            charcount= charcount +1
    return charcount

def countnonarticlewords(line):
    nonarticlewords= line.split()
    nonarticlewords=0
    for nonarticle in line:
        #nonarticlewords= line.split()
        if not 'a' or 'an' or 'the':
            nonarticlewords= nonarticlewords +1
    return len(nonarticle)

def countwords(line):
    words= line.split()
    return len(words)

def countdocstats(docFile):
    linecount=0
    totalcharacters=0
    totalwords=0
    totalnonarticlewords=0
    for line in docFile:
        linecount= linecount + 1
        totalwords= totalwords + countwords(line)
        totalcharacters=totalcharacters+countcharacters(line)
        totalnonarticlewords= totalnonarticlewords + countnonarticlewords(line)
    return linecount, totalcharacters, totalwords, totalnonarticlewords

def main():
    filename, textFile=getTextFile()
    linecount, totalcharacters, totalwords, totalnonarticlewords= countdocstats(textFile)

    outputcountresults(filename,linecount,totalcharacters,totalwords,totalnonarticlewords)
main()
  • 1
    Somehow each time people make the very same mistake. – Willem Van Onsem Jun 09 '18 at 18:11
  • No, seriously, what do you think `if not 'a' or 'an' or 'the'` does? – ForceBru Jun 09 '18 at 18:14
  • `if nonarticle not in {'a', 'an', 'the'}` – OneCricketeer Jun 09 '18 at 18:17
  • You have several mistakes. For starters, you do `nonarticlewords= line.split()` immediately followed by `nonarticlewords = 0`, rendering the previous line pointless. You then iterate directly over line, using `for nonarticle in line`, but you should be iterating over `for nonarticle in line.split()`. Finally, this check: `if not 'a' or 'an' or 'the'` isn't doing what you think it is doing – juanpa.arrivillaga Jun 09 '18 at 18:39

0 Answers0