Search code examples
pythonpython-3.xdictionarypyenchant

Create a dictionary using PyEnchant Language Dictionarie


I am trying to create a new dictionary from an english language dictionary that i have using PyEnchant.For example : the english word--> 'new' to become--> 'new':1e1n1w (i am counting the letters alphabetically) My problem is that i do not know how to access the objects of the english language dictionary so i can make a repetition structure and create the ne dictionary or change this one.(I have two files that make the dictionary , one FFT and one txt.)


Solution

  • Start from a word and convert it to letter count / letter format, sorted alphabetically:

    from collections import Counter
    
    s = "new"
    
    c = Counter(s)
    
    print("".join(["{}{}".format(v,k) for k,v in sorted(c.items())]))
    
    • count letters of the word using special Counter dictionary made for that purpose
    • create the new "word" using list comprehension by iterating on counter keys & values (sorted), recreating the string using str.join

    results in: 1e1n1w

    One-liner to create the list from input list (here: 2 items) using listcomps:

    newlist = ["".join(["{}{}".format(v,k) for k,v in sorted(Counter(word).items())]) for word in ['new','hello']]
    

    results in:

    ['1e1n1w', '1e1h2l1o']