In [39]:
#read text file from wc_input folder
#file = open('/Users/jigdelkuyee/workspace/insight/code_challenge/wc_input/first.txt', 'r')

import collections
import json
import ast

# Input and output files
InputFile = '/Users/jigdelkuyee/workspace/insight/code_challenge/wc_input/first.txt'
wordCountOutputFile = '/Users/jigdelkuyee/workspace/insight/code_challenge/wc_output/wc_result.txt'
medianOutputFile = '/Users/jigdelkuyee/workspace/insight/code_challenge/wc_output/med_result.txt'

def readFile(filename):
    # Declare words dictionary
    with open(InputFile) as f:
        wordList = f.read().split()
        #wordList = ast.literal_eval(words)
        #wordList = Counter(word.lower() for word in open("text.txt").read().split())
    
        #Remove wildcard characters - goes here
    
        #Using Counter() to keep track of word frequency
        wordsFreq = Counter(word.lower() for word in wordList)
        #wordsFreqSorted = sorted(wordsFreq)
    
        #Sort our word frequency using OrderedDict
        wordsFreqSorted = collections.OrderedDict(sorted(wordsFreq.items(), key=lambda t: t[0]))
    
        #Print out the list
        #print(wordsFreqSorted)

        #return sorted dictionary
        return(wordsFreqSorted)

def writeFile(dict, filename):
    with open(filename, 'wb') as f:
        for k, v in dict.iteritems():
            print(k," ", v)
    
    #with open(wordCountOutputFile, 'w') as f:
        #json.dump(wordsFreqSorted, f)
    
    #with open(wordCountOutputFile, 'wb') as f:
      # pickle.dump(wordsFreqSorted, f)

    #Write to a text file and save it in wc_output folder
    #with open(wordCountOutputFile, 'wb') as f:
     #   data = csv.writer(f)
      #  data.writerows(wordsFreqSorted)

def main():
    DictWords = readFile(InputFile)
    
    # Write to wc_result file
    writeFile(DictWords, wordCountOutputFile)
    
# Standard boilerplate to call the main() function to begin the program.
if __name__ == '__main__':
    main()


OrderedDict([('a', 8), ('accessible.', 1), ('ads', 1), ('after', 1), ('all', 2), ('along', 1), ('also', 1), ('amount', 1), ('an', 1), ('analytics.', 1), ('and', 8), ('are', 3), ('based', 1), ('be', 3), ('becomes', 1), ('begin', 1), ('broad', 1), ('building', 1), ('can', 4), ('cannot', 1), ('case.', 1), ('categorized.', 1), ('category', 1), ('challenge', 1), ('chart,', 1), ('citizens.', 1), ('cobbled', 1), ('companies,', 1), ('concerned', 1), ('course,', 1), ('cover', 1), ('customers,', 1), ('data', 11), ('data.', 1), ('decision', 1), ('developed', 2), ('different', 1), ('do', 3), ('each', 1), ('ecosystem.', 1), ('ecosystem?', 1), ('encounter', 1), ('engineering', 4), ('engineering.', 1), ('engineers.', 1), ('every', 1), ('experience', 1), ('face', 1), ('fellows', 2), ('field', 1), ('first', 1), ('fit', 1), ('for', 6), ('fortunately,', 1), ('framework', 1), ('framework,', 1), ('from', 1), ('governments', 1), ('have', 2), ('help', 1), ('hope', 1), ('how', 1), ('however,', 1), ('huge', 1), ('in', 3), ('included', 1), ('industry', 1), ('insight', 2), ('interactive', 1), ('into', 1), ('is', 4), ('it', 2), ('iterations', 1), ('large,', 1), ('longer', 1), ('look', 1), ('make', 2), ('makers,', 1), ('makes', 1), ('many', 1), ('map', 1), ('map.', 1), ('meant', 2), ('mentors,', 1), ('metrics1', 1), ('monumental', 1), ('more', 1), ('most', 1), ('neglected', 1), ('no', 2), ('non-profit', 1), ('of', 10), ('off', 1), ('often', 1), ('on', 1), ('open-source', 1), ('option', 1), ('or', 1), ('organizations,', 1), ('our', 1), ('overwhelming', 1), ('permutations', 1), ('pipeline', 3), ('pipelines.', 1), ('play', 1), ('plug', 1), ('possibly', 1), ('program,', 1), ('provide', 1), ('questions', 1), ('real-time', 1), ('realize', 1), ('represent', 1), ('required', 1), ('retail', 1), ('same', 1), ('sense', 1), ('serving', 1), ('set', 1), ('several', 2), ('simply', 1), ('since', 1), ('single', 1), ('solution', 1), ('solves', 1), ('sql', 1), ('starting', 1), ('strictly', 1), ('system', 1), ('technologies', 1), ('than', 1), ('that', 7), ('the', 11), ('their', 1), ('them', 1), ('them.', 1), ('there', 3), ('these', 2), ('they', 2), ('this', 1), ('to', 3), ('together,', 1), ('tools', 5), ('tools.', 1), ('typical', 1), ('unlimited', 1), ('unstructured,', 1), ('usable', 1), ('use', 1), ('used', 2), ('using', 1), ('value', 1), ('various', 1), ('very', 1), ('visualizing', 1), ('we', 5), ('what', 2), ('when', 2), ('widely', 1), ('will', 2), ('with', 2), ('working', 1), ('you', 2), ('zoo', 1)])

In [ ]:


In [ ]:


In [ ]: