natural language toolkit minimal sets c 20012023 nltk project steven bird stevenbird1gmail com url https www nltk org for license information see license txt find contexts where more than one possible target value can appear e g if targets are wordinitial letters and contexts are the remainders of words then we would like to find cases like fat vs cat and training vs draining if targets are partsofspeech and contexts are words then we would like to find cases like wind noun air in rapid motion vs wind verb coil wrap create a new minimal set param parameters the context target display tuples for the item type parameters listtuplestr str str add a new item to the minimal set having the specified context target and display form param context the context in which the item of interest appears type context str param target the item of interest type target str param display the information to be reported for each item type display str store the set of targets that occurred in this context keep track of which contexts and targets we have seen for a given context and target store the display form determine which contexts occurred with enough distinct targets param minimum the minimum number of distinct target forms type minimum int rtype list natural language toolkit minimal sets c 2001 2023 nltk project steven bird stevenbird1 gmail com url https www nltk org for license information see license txt find contexts where more than one possible target value can appear e g if targets are word initial letters and contexts are the remainders of words then we would like to find cases like fat vs cat and training vs draining if targets are parts of speech and contexts are words then we would like to find cases like wind noun air in rapid motion vs wind verb coil wrap create a new minimal set param parameters the context target display tuples for the item type parameters list tuple str str str the contrastive information what we are controlling for to record what we have seen what we will display add a new item to the minimal set having the specified context target and display form param context the context in which the item of interest appears type context str param target the item of interest type target str param display the information to be reported for each item type display str store the set of targets that occurred in this context keep track of which contexts and targets we have seen for a given context and target store the display form determine which contexts occurred with enough distinct targets param minimum the minimum number of distinct target forms type minimum int rtype list
from collections import defaultdict


class MinimalSet:
    

    def __init__(self, parameters=None):
        
        self._targets = set()  
        self._contexts = set()  
        self._seen = defaultdict(set)  
        self._displays = {}  

        if parameters:
            for context, target, display in parameters:
                self.add(context, target, display)

    def add(self, context, target, display):
        
        
        self._seen[context].add(target)

        
        self._contexts.add(context)
        self._targets.add(target)

        
        self._displays[(context, target)] = display

    def contexts(self, minimum=2):
        
        return [c for c in self._contexts if len(self._seen[c]) >= minimum]

    def display(self, context, target, default=""):
        if (context, target) in self._displays:
            return self._displays[(context, target)]
        else:
            return default

    def display_all(self, context):
        result = []
        for target in self._targets:
            x = self.display(context, target)
            if x:
                result.append(x)
        return result

    def targets(self):
        return self._targets