1
2
3
4 """Combiner classes to merge results from several predictors."""
5
6 from __future__ import absolute_import, unicode_literals
7 import abc
8 import prdct
12 """Base class for all combiners.
13
14 G{classtree Combiner}
15 """
16
17 __metaclass__ = abc.ABCMeta
18
20 """Combiner creator."""
21 self.name = "Combiner dosen't set any name"
22
24 """Sort the tokens according to their probabilities.
25
26 The duplicate tokens are merged to a single token (their probabilities
27 are summed).
28
29 @param prediction:
30 It is a list of Suggestion instances.
31 @type prediction: L{prdct.Prediction}
32
33 @return:
34 The sorted Prediction instance.
35 @rtype: Prediction
36 """
37 seen_tokens = set()
38 result = prdct.Prediction()
39 for i, suggestion in enumerate(prediction):
40 token = suggestion.word
41 if token not in seen_tokens:
42 for j in range(i + 1, len(prediction)):
43 if token == prediction[j].word:
44 suggestion.probability += prediction[j].probability
45 if suggestion.probability > prdct.MAX_PROBABILITY:
46 suggestion.probability = prdct.MAX_PROBABILITY
47 elif suggestion.probability < prdct.MIN_PROBABILITY:
48 suggestion.probability = prdct.MIN_PROBABILITY
49 seen_tokens.add(token)
50 result.add_suggestion(suggestion)
51 return result
52
53 @abc.abstractmethod
55 """Method for combining predictors's prediction list.
56
57 The combine() method is the main method of the Combiner. It must be
58 implemented by every combiners.
59 """
60 raise NotImplementedError("Method must be implemented")
61
64 """Simple combiner which combine suggestions based on their probabilities.
65
66 This combiner does not modify the suggestions probabilities. Thus, they are
67 sorted in descending order according to their probabilities.
68
69 G{classtree ProbabilisticCombiner}
70 """
72 """ProbabilisticCombiner creator."""
73 super(self.__class__, self).__init__()
74 self.name = 'ProbabilisticCombiner'
75
77 """Combine the suggestions in a single list and sort them.
78
79 @param predictionList:
80 The list of Prediction instances. Each predictors return a
81 Prediction instance which is added to the list by the
82 PredictorActivator.
83 @type predictionList: list
84
85 @return:
86 The combined Prediction instance containing every suggestions of
87 every Prediction instances sorted in descending order according to
88 their probabilities.
89 @rtype: L{prdct.Prediction}
90 """
91 result = prdct.Prediction()
92 for prediction in predictionList:
93 for suggestion in prediction:
94 result.add_suggestion(suggestion)
95 return(self.filter(result))
96