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