Package tipy :: Module mrgr
[hide private]
[frames] | no frames]

Source Code for Module tipy.mrgr

 1  #!/usr/bin/env python3 
 2  # -*- coding: utf-8 -*- 
 3   
 4  """Merger classes to merge results from several predictors.""" 
 5   
 6  import tipy.prdct 
 7  from abc import ABCMeta, abstractmethod 
8 9 10 -class Merger(object):
11 """Base class for all Mergers. 12 13 G{classtree Merger} 14 """ 15 16 __metaclass__ = ABCMeta 17 MIN_PROBABILITY = 0.0 18 MAX_PROBABILITY = 1.0 19
20 - def __init__(self):
21 """Merger creator.""" 22 self.name = "Merger dosen't set any name"
23
24 - def filter(self, prediction):
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 @abstractmethod
55 - def merge(self, predictionList):
56 """Method for merging predictors's prediction list. 57 58 The merge() method is the main method of the Merger. It must be 59 implemented by every Mergers. 60 """ 61 raise NotImplementedError("Method must be implemented")
62
63 64 -class ProbabilisticMerger(Merger):
65 """Simple Merger which merge suggestions based on their probabilities. 66 67 This Merger does not modify the suggestions probabilities. Thus, they are 68 sorted in descending order according to their probabilities. 69 70 G{classtree ProbabilisticMerger} 71 """
72 - def __init__(self):
73 """ProbabilisticMerger creator.""" 74 super(self.__class__, self).__init__() 75 self.name = 'ProbabilisticMerger'
76
77 - def merge(self, predictionList):
78 """merge 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 merged 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