#!/usr/bin/env python
# -*- coding: utf-8 -*-

#import string
import re
import bgdict
import csv
import os

(bg_en,en_bg,bg_bg,bg_enWikt, en_bgWikt) = bgdict.buildDicts()

def makeFreqFromText(text, usedWords):
    wordBoundry = re.compile('\W+',re.UNICODE)

    freqDict = {}
    # TODO: Fix capitals for names
    for w in wordBoundry.split(text):
        if w != w.lower():
            # Detect real words that are capitals
            w = w.lower()
        if freqDict.has_key(w):
            freqDict[w] += 1
        else:
            freqDict[w] = 1
    for common in list(set(usedWords).intersection(freqDict.keys())):
        #print common.encode('utf-8')
        del freqDict[common]
    return freqDict

def createChapterFile(filename,freqDict):
    if os.path.dirname(filename):
        if not os.path.isdir(os.path.dirname(filename)):
            os.mkdir(os.path.dirname(filename))
    csvWriter = csv.writer(open(filename + '.csv', 'wb'))

    for tup in sorted(sorted(freqDict.items(), key=lambda x: x[0].encode('utf-8')), key=lambda x: x[1], reverse=True):
        key = tup[0].encode('utf-8')
        value = lookupWord(tup[0])
        csvWriter.writerow([key , value])

def lookupWord(word):
    try:
        return bg_enWikt[word]
    except:
        print word + " not in bg_enWikt"
        try:
            return bg_enWikt[bg_bg[word]]
        except:
            print word + " not in bg_bg and bg_enWikt"
            try:
                return bg_en[bg_bg[word]].encode('utf-8')
            except:
                print word + " not in bg_bg and bg_en"
                try:
                    return bg_en[word].encode('utf-8')
                except:
                    try:
                        return bg_en[word.title()].encode('utf-8')
                    except:
                        return "Unknown"

@@ -1203,27 +1203,50 @@
 (word):%0A    
-try
+if bg_enWikt.has_key(word)
 :%0A        re
@@ -1275,365 +1275,423 @@
    e
-xcept:%0A        print word + %22 not in bg_enWikt%22%0A        try:%0A            return bg_enWikt%5Bbg_bg%5Bword%5D%5D%0A        except:%0A            print word + %22 not in bg_bg and bg_enWikt%22%0A            try:%0A                return bg_en%5Bbg_bg%5Bword%5D%5D.encode('utf-8')%0A            except:%0A                print word + %22 not in bg_bg and bg_en%22%0A                try:%0A            
+lif bg_bg.has_key(word):%0A        if bg_enWikt.has_key(bg_bg%5Bword%5D):%0A            return bg_enWikt%5Bbg_bg%5Bword%5D%5D%0A    if bg_en.has_key(word):%0A        return bg_en%5Bword%5D.encode('utf-8')%0A    elif bg_bg.has_key(word):%0A        if bg_en.has_key(bg_bg%5Bword%5D):%0A            return bg_en%5Bbg_bg%5Bword%5D%5D.encode('utf-8')%0A    word = word.title()%0A    if bg_bg.has_key(word):%0A        word = bg_bg%5Bword%5D%0A    if bg_enWikt.has_key(word):%0A
     
@@ -1710,96 +1710,47 @@
 g_en
+Wikt
 %5Bword%5D
-.encode('utf-8')%0A                except:%0A                    try:%0A                
+%0A    if bg_en.has_key(word):%0A
     
@@ -1770,24 +1770,16 @@
 _en%5Bword
-.title()
 %5D.encode
@@ -1792,56 +1792,8 @@
 8')%0A
-                    except:%0A                    
     

make a little bit more logical word lookup, still missing които...