from mainCountPN import mainCountPN
#txt file output

stopwords = ['a', 'about', 'above', 'across', 'after', 'afterwards']
stopwords += ['again', 'against', 'all', 'almost', 'alone', 'along']
stopwords += ['already', 'also', 'although', 'always', 'am', 'among']
stopwords += ['amongst', 'amoungst', 'amount', 'an', 'and', 'another']
stopwords += ['any', 'anyhow', 'anyone', 'anything', 'anyway', 'anywhere']
stopwords += ['are', 'around', 'as', 'at', 'back', 'be', 'became']
stopwords += ['because', 'become', 'becomes', 'becoming', 'been']
stopwords += ['before', 'beforehand', 'behind', 'being', 'below']
stopwords += ['beside', 'besides', 'between', 'beyond', 'bill', 'both']
stopwords += ['bottom', 'but', 'by', 'call', 'can', 'cannot', 'cant']
stopwords += ['co', 'computer', 'con', 'could', 'couldnt', 'cry', 'de', 'u', 'v', 'w']
stopwords += ['describe', 'detail', 'did', 'do', 'done', 'down', 'due']
stopwords += ['during', 'each', 'eg', 'eight', 'either', 'eleven', 'else']
stopwords += ['elsewhere', 'empty', 'enough', 'etc', 'even', 'ever']
stopwords += ['every', 'everyone', 'everything', 'everywhere', 'except']
stopwords += ['few', 'fifteen', 'fifty', 'fill', 'find', 'fire', 'first']
stopwords += ['five', 'for', 'former', 'formerly', 'forty', 'found', 'x', 'y', 'z']
stopwords += ['four', 'from', 'front', 'full', 'further', 'get', 'give']
stopwords += ['go', 'had', 'has', 'hasnt', 'have', 'he', 'hence', 'her']
stopwords += ['here', 'hereafter', 'hereby', 'herein', 'hereupon', 'hers']
stopwords += ['herself', 'him', 'himself', 'his', 'how', 'however']
stopwords += ['hundred', 'i', 'ie', 'if', 'in', 'inc', 'indeed']
stopwords += ['interest', 'into', 'is', 'it', 'its', 'itself', 'keep']
stopwords += ['last', 'latter', 'latterly', 'least', 'less', 'ltd', 'made', 'n', 'o', 'p']
stopwords += ['many', 'may', 'me', 'meanwhile', 'might', 'mill', 'mine']
stopwords += ['more', 'moreover', 'most', 'mostly', 'move', 'much']
stopwords += ['must', 'my', 'myself', 'name', 'namely', 'neither', 'never']
stopwords += ['nevertheless', 'next', 'nine', 'no', 'nobody', 'none']
stopwords += ['noone', 'nor', 'not', 'nothing', 'now', 'nowhere', 'of']
stopwords += ['off', 'often', 'on', 'once', 'one', 'only', 'onto', 'or']
stopwords += ['other', 'others', 'otherwise', 'our', 'ours', 'ourselves', 'b', 'c', 'd', 'e']
stopwords += ['out', 'over', 'own', 'part', 'per', 'perhaps', 'please', 'f', 'g', 'h', 'j']
stopwords += ['put', 'rather', 're', 's', 'same', 'see', 'seem', 'seemed', 'k', 'l', 'm']
stopwords += ['seeming', 'seems', 'serious', 'several', 'she', 'should']
stopwords += ['show', 'side', 'since', 'sincere', 'six', 'sixty', 'so']
stopwords += ['some', 'somehow', 'someone', 'something', 'sometime']
stopwords += ['sometimes', 'somewhere', 'still', 'such', 'system', 'take']
stopwords += ['ten', 'than', 'that', 'the', 'their', 'them', 'themselves']
stopwords += ['then', 'thence', 'there', 'thereafter', 'thereby']
stopwords += ['therefore', 'therein', 'thereupon', 'these', 'they']
stopwords += ['thick', 'thin', 'third', 'this', 'those', 'though', 'three']
stopwords += ['three', 'through', 'throughout', 'thru', 'thus', 'to']
stopwords += ['together', 'too', 'top', 'toward', 'towards', 'twelve', 'q', 'r', 's', 't']
stopwords += ['twenty', 'two', 'un', 'under', 'until', 'up', 'upon']
stopwords += ['us', 'very', 'via', 'was', 'we', 'well', 'were', 'what']
stopwords += ['whatever', 'when', 'whence', 'whenever', 'where']
stopwords += ['whereafter', 'whereas', 'whereby', 'wherein', 'whereupon']
stopwords += ['wherever', 'whether', 'which', 'while', 'whither', 'who']
stopwords += ['whoever', 'whole', 'whom', 'whose', 'why', 'will', 'with']
stopwords += ['within', 'without', 'would', 'yet', 'you', 'your']
stopwords += ['yours', 'yourself', 'yourselves']


def deletepunctuation(text):
    import re
    return re.compile(r'\W+', re.UNICODE).split(text)


def wordfreqdict(contents):
    wordfreq = []
    for p in contents:
        wordfreq.append(contents.count(p))
    return dict(list(zip(contents, wordfreq)))


def sortdictionary(freqdict):
    x = [(freqdict[key], key) for key in freqdict]
    x.sort()
    x.reverse()
    return x


def removeStopwords(contents, stopwords):
    return [w for w in contents if w not in stopwords]


def countstopwords(contents, stopwords):
    i = 0
    for j in contents:
        if j in stopwords:
            i += 1
    return i


#driver code
courierdict = ['', '', '', '', '']
rawfile = ['citylinkRaw.txt', 'poslajuRaw.txt', 'gdexraw.txt', 'jntRaw.txt', 'dhlRaw.txt']
resultsfile = ['CityLinkResult.txt', 'PosLajuResult.txt', 'GDEXResult.txt', 'JnTResult.txt', 'DHLResult.txt']
courierr=["","","","",""]
deletedSW = [0, 0, 0, 0, 0]
wordcount = [0, 0, 0, 0, 0]
withoutStop = [0, 0, 0, 0, 0]

for i in range(len(rawfile)):
    openfile = open(rawfile[i], 'r', encoding='UTF8') #openfile
    content_string = openfile.read().lower() #lowercase
    fullcontent = deletepunctuation(content_string) #deletepunctuation
    #print(fullcontent)
    courierr[i]=fullcontent
    wordcount[i] = len(fullcontent) #count raw text file (stopword + normal)
    countSW = countstopwords(fullcontent, stopwords) #count stop word raw (stopword)
    deletedSW[i] = countSW #deleted from raw
    contents = removeStopwords(fullcontent, stopwords)
    dictionary = wordfreqdict(contents)
    sorteddict = sortdictionary(dictionary)
    courierdict[i] = sorteddict 
    for s in courierdict[i]:
        #print(str(s))
        for k in range(len(resultsfile)):
            f = open(resultsfile[k], "a+")
            f.write(str(s) + "\n") #print new text file
            f.close()

print("The total words in the article:", wordcount)
print("The total stop words in the article:", deletedSW)
withoutStop = []
for i in range(0, len(wordcount)):
    withoutStop.append(wordcount[i]-deletedSW[i])
print("The total words without stop words in the article:", withoutStop)
mainCountPN(courierr)

