class TweetHolder: # using_ifd = token_dict() def __init__(self, tw): self.tw = tw self.id = tw.get(tk.key_id) self.cluster = None self.tokens = IdFreqDict() self.valid_tokens = IdFreqDict() self.tokenize() def __contains__(self, key): return key in self.tw def __getitem__(self, key): return self.get(key) def __setitem__(self, key, value): self.setdefault(key, value) def get(self, key): return self.tw.get(key, None) def setdefault(self, key, value): self.tw.setdefault(key, value) def get_cluid(self): return self.cluster.cluid def update_cluid_into_tw(self): self.tw[ tk. key_event_cluid] = self.cluster.cluid if self.cluster is not None else None def tokenize(self): # tokens = (t.text.lower() for t in self.tw[tk.key_spacy]) tokens = pu.findall(pu.tokenize_pattern, self.tw[tk.key_text].lower()) tokens = [ t.strip() for t in tokens if pu.is_valid_keyword(t) and not pu.is_stop_word(t) ] for token in tokens: self.tokens.count_word(token) def validate(self, using_ifd): self.valid_tokens.clear() for word, freq in self.tokens.word_freq_enumerate(newest=False): if using_ifd.has_word(word): self.valid_tokens.count_word(word, freq) def update_cluster(self, cluster): if self.cluster is not None: self.cluster.update_by_twh(self, factor=-1) self.cluster = cluster if cluster is not None: cluster.update_by_twh(self, factor=1)
class TweetHolder: # using_ifd = token_dict() def __init__(self, tw): self.tw = tw self.id = tw.get(tk.key_id) self.cluster = None self.tokens = IdFreqDict() self.valid_tokens = IdFreqDict() self.tokenize() def __contains__(self, key): return key in self.tw def __getitem__(self, key): return self.get(key) def __setitem__(self, key, value): self.setdefault(key, value) def get(self, key): return self.tw.get(key, None) def setdefault(self, key, value): self.tw.setdefault(key, value) def get_cluid(self): """ 返回当前已被分配的聚类的ID :return: int,聚类的ID编号 """ return self.cluster.cluid def update_cluid_into_tw(self): """ 更新推特对象(self.tw,dict类型)的 tk.key_event_cluid 字段为当前已被分配的聚类的ID, 若尚未被分配聚类则置为None :return: """ self.tw[ tk. key_event_cluid] = self.cluster.cluid if self.cluster is not None else None def tokenize(self): """ 将推特对象的text进行分词并保存分词结果,使用 self.tokens 进行分词计数 :return: """ # tokens = (t.text.lower() for t in self.tw[tk.key_spacy]) tokens = pu.valid_tokenize(self.tw[tk.key_text].lower()) for token in tokens: self.tokens.count_word(token) def validate(self, using_ifd): """ 更新分词表,将 self.tokens 中存在于 using_ifd 的单词重新计数到 self.valid_tokens 中 :param using_ifd: utils.id_freq_dict.IdFreqDict,包含当前迭代中的合法分词 :return: """ self.valid_tokens.clear() for word, freq in self.tokens.word_freq_enumerate(newest=False): if using_ifd.has_word(word): self.valid_tokens.count_word(word, freq) def update_cluster(self, cluster): """ 若原本有聚类,则将当前推特从原本的 self.cluster 中分离; 并将当前推特合并至 cluster 中(若不为None),更新 self.cluster :param cluster: 目标聚类对象 :return: """ if self.cluster is not None: self.cluster.update_by_twh(self, factor=-1) self.cluster = cluster if cluster is not None: cluster.update_by_twh(self, factor=1)