Пример #1
0
class Group(Content, LocalRolesMixin, ContextACLMixin):
    type_name = u"Group"
    type_title = _(u"Group")
    add_permission = "Add %s" % type_name
    title = u""
    description = u""
    icon = u"user"  #FIXME no group icon!?

    def __init__(self, **kwargs):
        #Things like created, creator etc...
        super(Group, self).__init__()
        self.__members__ = OOSet()
        self.update(event=False, **kwargs)

    @property
    def principal_name(self):
        """ The way the security system likes to check names
            to avoid collisions with userids.
        """
        return u"group:%s" % self.__name__

    @property
    def members(self):
        return self.__members__

    @members.setter
    def members(self, value):
        self.__members__.clear()
        self.__members__.update(value)
Пример #2
0
class Group(Content, LocalRolesMixin, ContextACLMixin):
    type_name = u"Group"
    type_title = _(u"Group")
    add_permission = "Add %s" % type_name
    title = u""
    description = u""
    css_icon = "glyphicon glyphicon-user" #FIXME no group icon!?

    def __init__(self, **kwargs):
        #Things like created, creator etc...
        super(Group, self).__init__()
        self.__members__ = OOSet()
        self.update(event = False, **kwargs)

    @property
    def principal_name(self):
        """ The way the security system likes to check names
            to avoid collisions with userids.
        """
        return u"group:%s" % self.__name__

    @property
    def members(self):
        return self.__members__

    @members.setter
    def members(self, value):
        self.__members__.clear()
        self.__members__.update(value)
Пример #3
0
class Content(Base, Folder):
    title = ""
    description = ""
    default_view = u"view"
    delegate_view = None
    nav_visible = True
    nav_title = None
    listing_visible = True
    search_visible = True
    show_byline = False
    custom_addable = False

    def __init__(self, **kw):
        #To set that this should keep track of ordering. See Folder
        #If _order is set to None, ordering isn't stored
        self._order = ()
        Folder.__init__(self)
        super(Content, self).__init__(**kw)

    def get_nav_title(self):
        nav_title = getattr(self, 'nav_title', None)
        if nav_title:
            return nav_title
        title = getattr(self, 'title', '')
        return title and title or self.__name__

    @property
    def tags(self): return getattr(self, '__tags__', ())
    @tags.setter
    def tags(self, value):
        #Is this the right way to mutate objects, or should we simply clear the contents?
        if value:
            self.__tags__ = OOSet(value)
        else:
            if hasattr(self, '__tags__'):
                delattr(self, '__tags__')

    @property
    def custom_addable_types(self):
        return frozenset(getattr(self, '_custom_addable_types', ()))
    @custom_addable_types.setter
    def custom_addable_types(self, value):
        if not hasattr(self, '_custom_addable_types'):
            self._custom_addable_types = OOSet(value)
        if set(value) != set(self._custom_addable_types):
            self._custom_addable_types.clear()
            self._custom_addable_types.update(value)
    @custom_addable_types.deleter
    def custom_addable_types(self):
        if hasattr(self, '_custom_addable_types'):
            delattr(self, '_custom_addable_types')
class NounBayesClassifier(Persistent):
    """
    """
    implements(IContentClassifier)
    
    def __init__(self,tagger=None,noNounRanksToKeep = 20):
        """
        """
        self.noNounRanksToKeep = noNounRanksToKeep
        self.trainingDocs = PersistentMapping()
        self.allNouns = OOSet()
        
        self.classifier = None
        self.trainAfterUpdate = True
    
    def addTrainingDocument(self,doc_id,tags):
        """
        """
        storage = getUtility(INounPhraseStorage)
        importantNouns = storage.getNounTerms(doc_id,self.noNounRanksToKeep)
        
        self.trainingDocs[doc_id] = (importantNouns,tags)
        self.allNouns = union(self.allNouns,OOSet(importantNouns))
    
    def train(self):
        """
        """
        presentNouns = dict()
        trainingData = []
        if not self.allNouns:
            storage = getUtility(INounPhraseStorage)
            for key in self.trainingDocs.keys():
                importantNouns = storage.getNounTerms(
                    key,
                    self.noNounRanksToKeep)
                self.allNouns = union(self.allNouns,OOSet(importantNouns))
        for item in self.allNouns:
            presentNouns.setdefault(item,0)
        
        for (nouns,tags) in self.trainingDocs.values():
            nounPresence = presentNouns.copy()
            for noun in nouns:
                nounPresence[noun] = 1
            for tag in tags:
                trainingData.append((nounPresence,tag,))
        if trainingData:
            self.classifier = NaiveBayesClassifier.train(trainingData)
    
    def classify(self,doc_id):
        """
        """
        if not self.classifier:
            return []
        
        presentNouns = dict()
        for item in self.allNouns:
            presentNouns.setdefault(item,0)
        
        storage = getUtility(INounPhraseStorage)
        importantNouns = storage.getNounTerms(doc_id,self.noNounRanksToKeep)
        for noun in importantNouns:
            if noun in presentNouns.keys():
                presentNouns[noun] = 1
        return self.classifier.classify(presentNouns)
    
    def probabilityClassify(self,doc_id):
        """
        """
        if not self.classifier:
            return []
        
        presentNouns = dict()
        for item in self.allNouns:
            presentNouns.setdefault(item,0)
        storage = getUtility(INounPhraseStorage)
        importantNouns = storage.getNounTerms(doc_id,self.noNounRanksToKeep)
        for noun in importantNouns:
            if noun in presentNouns.keys():
                presentNouns[noun] = 1
        return self.classifier.prob_classify(presentNouns)
    
    def clear(self):
        """Wipes the classifier's data.
        """
        self.allNouns.clear()
        self.trainingDocs.clear()
    
    def tags(self):
        if not self.classifier:
            return []
        return self.classifier.labels()