#!/usr/bin/python # -*- coding: utf-8 -*- import timeit import os import codecs # # Function sorts a list and creates a dictionary indexed with starting letter of each word in the list # @param listToSort : list of words to sort # @returns dictionary having first letters of words in the list as key and each key having all words starting with that # letter as starting letter # def sorter(listToSort): time = timeit.Timer() dictionary = dict() listToSort.sort() #print listToSort for item in listToSort: if not dictionary.has_key(item[0]): dictionary[item[0]] = [item] continue dictionary[item[0]].append(item) print time.timeit() return dictionary if __name__ == '__main__': #listToSort = ['a','apple','vasudev','voilin','banana','cat','bomb'] #dc = sorter(listToSort) #print dc dictionary = os.path.join(os.path.dirname(__file__), 'dicts/kn_IN.dic') words = codecs.open(dictionary,'r',encoding='utf-8', errors='ignore').read()#, errors='ignore' # Create a list from the read out word by removing \n word = list(set(words.split())) #print len(word) # Convert the list of words dictionay of words indexed by their first letter dc = sorter(word) #a = u'ಅ' #print len(dc[a]) #print wordList