Пример #1
0
class ClusterHolder:
    def __init__(self, cluid):
        self.cluid = cluid
        self.retwsetdict = dict()
        self.tokens = IdFreqDict()
        self.twnum = 0

    def update_by_retwset(self, retwset, factor):
        set_mstid = retwset.master_twhid
        if factor > 0:
            # if retw_mstid in self._retwsetdict:
            #     raise ValueError('cannot move in retwset since retwid {} is in cluster'.format(retw_mstid))
            self.retwsetdict[set_mstid] = retwset
            for twh in retwset.get_twharr():
                self.update_by_twh(twh, 1)
        else:
            # if retw_mstid not in self._retwsetdict:
            #     raise ValueError('cannot move out retwset since retwid {} not in cluster'.format(retw_mstid))
            for twh in retwset.get_twharr():
                self.update_by_twh(twh, -1)
            self.retwsetdict.pop(set_mstid)

    def update_by_twh(self, twh, factor):
        twh_tokens = twh.tokens
        if factor > 0:
            self.tokens.merge_freq_from(twh_tokens)
            self.twnum += 1
        else:
            self.tokens.drop_freq_from(twh_tokens)
            self.twnum -= 1
Пример #2
0
    class ClusterHolder:
        def __init__(self, cluid):
            self.cluid = cluid
            self.tokens = IdFreqDict()
            self.twnum = 0

        def update_by_twh(self, twh, factor):
            twh_tokens = twh.ifd
            if factor > 0:
                self.tokens.merge_freq_from(twh_tokens, newest=False)
                self.twnum += 1
            else:
                self.tokens.drop_freq_from(twh_tokens, newest=False)
                self.twnum -= 1
Пример #3
0
class ClusterHolder:
    def __init__(self, cluid):
        self.cluid = cluid
        self.twhdict = dict()
        self.tokens = IdFreqDict()
        self.twnum = 0

    """ basic functions """

    def get_twharr(self):
        return list(self.twhdict.values())

    def get_twarr(self):
        return [twh.tw for twh in self.twhdict.values()]

    def get_lbarr(self):
        return [twh[tk.key_event_label] for twh in self.twhdict.values()]

    def clear(self):
        self.twhdict.clear()
        self.tokens.clear()
        self.twnum = 0

    def update_by_twh(self, twh, factor):
        twh_tokens = twh.valid_tokens
        twh_id = twh.id
        if factor > 0:
            self.tokens.merge_freq_from(twh_tokens, newest=False)
            self.twhdict[twh_id] = twh
            self.twnum += 1
        else:
            self.tokens.drop_freq_from(twh_tokens, newest=False)
            if twh_id in self.twhdict:
                self.twhdict.pop(twh_id)
            self.twnum -= 1

    """ extra functions """

    def get_rep_label(self, rep_thres):
        lb_count = Counter(self.get_lbarr())
        max_label, max_lbnum = lb_count.most_common(1)[0]
        rep_label = -1 if max_lbnum < self.twnum * rep_thres else max_label
        return rep_label
Пример #4
0
class ClusterHolder:
    def __init__(self, cluid):
        self.cluid = cluid
        self.twhdict = dict()
        self.tokens = IdFreqDict()
        self.twnum = 0

    """ basic functions """

    def get_twharr(self):
        """
        返回聚类当前持有的推特包装对象的列表,不考虑排列顺序
        :return:  list,每个元素类型为TweetHolder
        """
        return list(self.twhdict.values())

    def get_twarr(self):
        """
        返回聚类当前持有的推特对象的列表,不考虑排列顺序
        :return: list,推特列表
        """
        return [twh.tw for twh in self.twhdict.values()]

    def get_lbarr(self):
        """
        返回聚类当前的推特对象所持有的标记(若存在该信息)的列表,不考虑排列顺序
        :return: list,元素为int,表示推特原本的标记值(原本属于哪个聚类)
        """
        return [twh[tk.key_event_label] for twh in self.twhdict.values()]

    def clear(self):
        """
        清空当前聚类的统计信息,包括分词表、推特列表、推特计数
        :return:
        """
        self.twhdict.clear()
        self.tokens.clear()
        self.twnum = 0

    def update_by_twh(self, twh, factor):
        """
        将输入的推特包装对象加入/移出当前聚类,并根据其 valid_tokens 更新当前聚类的分词表等统计信息
        :param twh: TweetHolder,要加入的推特包装对象
        :param factor: int,1表示加入,0表示移出
        :return:
        """
        twh_tokens = twh.valid_tokens
        twh_id = twh.id
        if factor > 0:
            self.tokens.merge_freq_from(twh_tokens, newest=False)
            self.twhdict[twh_id] = twh
            self.twnum += 1
        else:
            self.tokens.drop_freq_from(twh_tokens, newest=False)
            if twh_id in self.twhdict:
                self.twhdict.pop(twh_id)
            self.twnum -= 1

    """ extra functions """

    def get_rep_label(self, rep_thres):
        """
        计算当前聚类中是否存在标记数占推特列表总数的比例大于阈值 rep_thres 的标记
        :param rep_thres: float,判定阈值
        :return: int,若存在足够占比的标记则返回该标记,否则返回-1
        """
        lb_count = Counter(self.get_lbarr())
        max_label, max_lbnum = lb_count.most_common(1)[0]
        rep_label = -1 if max_lbnum < self.twnum * rep_thres else max_label
        return rep_label