Question

I need to perform some type of prediction/suggestion using Python.

For example lets assume that we have multiple lists

alphabet = ["a", "b", "c"]

other_alphabet = ["a", "b", "d"]

another_alphabet = ["a", "b", "c"]

there is also a current alphabet under construction

current_alphabet = ["a", "b", ...]

In 2 of 3 alphabets the letter after "a", "b" is "c", so I want the code to predict/suggest that in the current_alphabet the next letter following "a", "b" will be "c" (with 66% probability)

I think the task is a bit more complicated than it looks.

Any suggestions how this can be achieved? Maybe something similar that can help in this process?

Was it helpful?

Solution

import itertools

alphabet = ["a", "b", "c"]
other_alphabet = ["a", "b", "d"]
another_alphabet = ["a", "b", "c"]

# here we take the nth char of each alphabet that is used as prediction
# (this position is indicated by the number of the currently entered char)
# zip takes the lists, and well, zips :) them, it means it creates new lists, so every
# first elements end up together, second elemends are together and so on.
# as far as current position you have to track it when user enters data (you have to
# know which (first, second, tenth) letter user is entering
current_position=1

letters = zip(alphabet,other_alphabet, another_alphabet)[current_position]
letters = list(letters)
letters.sort()
print 'letters at current position', letters

# here we group all occurences of the same letters,  
letter_groups = itertools.groupby(letters, key=lambda x: x[0])

# here we count the number of occurences of each letter
# from the alphabets, and divide it by the lenght
# of the list of letters

letter_probabilities = [[a[0], sum (1 for _ in a[1])/float(len(letters))] for a in letter_groups]
print 'letter probablilities at the current postion ', letter_probabilities

the code above produces following output:

letters at current position ['c', 'c', 'd']
letter probablilities at the current postion  [['c', 0.6666666666666666], ['d', 0.3333333333333333]]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top