__author__ = 'bengt'

import argparse


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('corpus', help="Path to the corpus file.")
    parser.add_argument('word', nargs='?', help="Show word frequencies.")
    parser.add_argument('pos', nargs='?', help="Show part-of-speech frequencies.")
    args = parser.parse_args()

    if args.word:
        [print(frequency, ' ', word) for word, frequency in word_frequencies(args.corpus)]

    if args.pos:
        [print(frequency, ' ', pos) for pos, frequency in pos_frequencies(args.corpus)]


def word_frequencies(filename):
    word_freq = {}
    corpus = open(filename)
    # read file and
    # calculate word frequencies
    for line in corpus:
        line = line.replace('\n', '')
        if len(line.split('\t')) != 6:
            continue
        ID, FORM, LEMMA, PLEMMA, POS, PPOS = line.split('\t')

        if LEMMA in word_freq:
            word_freq [LEMMA] += 1
        else:
            word_freq [LEMMA] = 1

    import operator

    sorted_words = sorted(word_freq.items(), key=operator.itemgetter(1))
    return sorted_words

def pos_frequencies(filename):
    pos_freq = {}
    corpus = open(filename)
    # read file and
    # calculate pos frequencies
    for line in corpus:
        line = line.replace('\n', '')
        if len(line.split('\t')) != 6:
            continue
        ID, FORM, LEMMA, PLEMMA, POS, PPOS = line.split('\t')

        if POS in pos_freq:
            pos_freq[POS] += 1
        else:
            pos_freq[POS] = 1

    import operator

    sorted_words = sorted(pos_freq.items(), key=operator.itemgetter(1))
    return sorted_words


if __name__ == '__main__':
    main()

@@ -123,17 +123,17 @@
 ', help=
-%22
+'
 Path to 
@@ -148,17 +148,17 @@
 us file.
-%22
+'
 )%0A    pa
@@ -180,32 +180,23 @@
 nt('
+--
 word',
- nargs='?',
  help=
-%22
+'
 Show
@@ -209,25 +209,46 @@
 frequencies.
-%22
+', action='store_true'
 )%0A    parser
@@ -266,31 +266,22 @@
 nt('
+--
 pos',
- nargs='?',
  help=
-%22
+'
 Show
@@ -312,9 +312,30 @@
 ies.
-%22
+', action='store_true'
 )%0A  
@@ -372,46 +372,186 @@
     
-if args.word:%0A        %5Bprint(frequency
+corpus_file = open(args.corpus)%0A    corpus = %5Bline.replace('%5Cn', '') for line in corpus_file.readlines()%5D%0A%0A    if args.word:%0A        %5Bprint('%7B0%7D'.format(str(frequency).rjust(12))
 , ' 
@@ -591,37 +591,32 @@
 ord_frequencies(
-args.
 corpus)%5D%0A%0A    if
@@ -641,25 +641,54 @@
  %5Bprint(
-frequency
+'%7B0%7D'.format(str(frequency).rjust(12))
 , ' ', p
@@ -729,21 +729,16 @@
 uencies(
-args.
 corpus)%5D
@@ -744,21 +744,16 @@
 %5D%0A%0A%0Adef 
-word_
 frequenc
@@ -760,106 +760,101 @@
 ies(
-filename):%0A    word_freq = %7B%7D%0A    corpus = open(filename)%0A    # read file and%0A    # calculate word
+corpus, index):%0A    %22%22%22 Will calculate the frequency of tokens in column /index/.%0A    %22%22%22%0A   
  fre
@@ -853,32 +853,37 @@
 %0A    frequencies
+ = %7B%7D
 %0A    for line in
@@ -895,46 +895,8 @@
 us:%0A
-        line = line.replace('%5Cn', '')%0A
     
@@ -955,130 +955,124 @@
 nue%0A
-        ID, FORM, LEMMA, PLEMMA, POS, PPOS = line.split('%5Ct')%0A%0A        if LEMMA in word_freq:%0A            word_freq %5BLEMMA
+%0A        row = line.split('%5Ct')%0A        key = row%5Bindex%5D%0A%0A        if key in frequencies:%0A            frequencies%5Bkey
 %5D +=
@@ -1104,24 +1104,23 @@
     
-word_freq %5BLEMMA
+frequencies%5Bkey
 %5D = 
@@ -1169,25 +1169,27 @@
  sorted(
-word_freq
+frequencies
 .items()
@@ -1247,15 +1247,17 @@
 ds%0A%0A
+%0A
 def 
-pos
+word
 _fre
@@ -1269,90 +1269,29 @@
 ies(
-filename):%0A    pos_freq = %7B%7D%0A    corpus = open(filename)%0A    # read file and%0A    #
+corpus):%0A    %22%22%22 Will
  cal
@@ -1297,19 +1297,19 @@
 lculate 
-pos
+the
  frequen
@@ -1313,417 +1313,202 @@
 uenc
-ies%0A    for line in corpus:%0A        line = line.replace('%5Cn', '')%0A        if len(line.split('%5Ct')) != 6:%0A            continue%0A        ID, FORM, LEMMA, PLEMMA, POS, PPOS = line.split('%5Ct')%0A%0A        if POS in pos_freq:%0A            pos_freq%5BPOS%5D += 1%0A        else:%0A            pos_freq%5BPOS%5D = 1%0A%0A    import operator%0A%0A    sorted_words = sorted(pos_freq.items(), key=operator.itemgetter(1))%0A    return sorted_words
+y for each word.%0A    %22%22%22%0A    return frequencies(corpus, 1)%0A%0A%0Adef pos_frequencies(corpus):%0A    %22%22%22 Will calculate the frequency for each part-of-speech.%0A    %22%22%22%0A    return frequencies(corpus, -2)
 %0A%0A%0Ai

Fix argparse