Beispiel #1
0
    def step4_cal():
        """ 24 点时merge已有的簇
        """
        if not initializing and now_hour == 0:
            print '[%s] ' % ts2datetime(int(time.time())), 'event ', eventid, ' %s start step4' % ts2datetime(timestamp)

            subevents = event.getSubEvents()
            subevent_fwords = dict()
            for subevent in subevents:
                subeventid = subevent["_id"]
                feature = Feature(subeventid)

                # 获取每个子事件最新的特征词
                fwords = feature.get_newest()
                subevent_fwords[subeventid] = fwords

            subeventids_sort_timestamp = event.get_sorted_subeventids()

            cids, mids = merge_subevents(subevent_fwords, subeventids_sort_timestamp, top_tfidf_para=10, top_percent=0.3)

            for res_id, mer_id in mids:
                # 将mer_id下的文本扔入res_id下的簇,remove mer_id的簇
                temp_infos = event.get_subevent_infos(mer_id)

                for r in temp_infos:
                    news = News(r["_id"], event.id)
                    news.update_news_subeventid(res_id)

                event.remove_subevents([mer_id])

            # 重新计算各簇的特征词, 并计算文本权重, 并去重
            if len(mids):
                step3_cal()

            print '[%s] ' % ts2datetime(int(time.time())), 'event ', eventid, ' %s end step3' % ts2datetime(timestamp)
Beispiel #2
0
    def step1_cal():
        """第一步计算,获取子事件特征词,新文本与特征词匹配分类
        """
        print '[%s] ' % ts2datetime(int(time.time(
        ))), 'event ', eventid, ' %s start step1' % ts2datetime(timestamp)

        if initializing:
            # 若话题需要做初始聚类,获取话题开始时间之前的文本
            results = event.getInitialInfos()
        else:
            # 若话题已做完初始聚类,获取话题最新一小时的文本
            results = event.getInfos(timestamp - 3600, timestamp)

            if now_hour == 0:
                # 如果不是在做初始化,24时的时候, 一定把当天(大于或等于0时小于24时)产生的簇(非其他簇)下的文本重新做一下匹配, 同时删除这些簇
                temp_subeventids = event.getTodayCreatSubeventIds()
                temp_infos = event.getTodayCreatSubeventInfos()
                event.remove_subevents(temp_subeventids)
                results.extend(temp_infos)

        print eventid, ' before classify: ', len(results)

        # 获取子事件
        subevents = event.getSubEvents()
        labels_list = []
        feature_words_list = []
        for subevent in subevents:
            subeventid = subevent["_id"]
            feature = Feature(subeventid)

            # 获取每个子事件最新的特征词
            fwords = feature.get_newest()
            feature_words_list.append(fwords)
            labels_list.append(subeventid)

        for r in results:
            text = (r['title'] + r['content168']).encode('utf-8')
            feature_words_inputs = []
            for fwords in feature_words_list:
                wcdict = dict()
                for w, c in fwords.iteritems():
                    if isinstance(w, unicode):
                        w = w.encode('utf-8')
                    wcdict[w] = c
                feature_words_inputs.append(wcdict)

            # 单条文本与各子事件的特征词进行匹配,得到每条文本的簇标签
            label = subevent_classifier(text, labels_list,
                                        feature_words_inputs)

            if label == "other":
                label = event.getOtherSubEventID()

            news = News(r["_id"], event.id)
            news.update_news_subeventid(label)

        print '[%s] ' % ts2datetime(int(time.time(
        ))), 'event ', eventid, ' %s end step1' % ts2datetime(timestamp)
Beispiel #3
0
    def step1_cal():
        """第一步计算,获取子事件特征词,新文本与特征词匹配分类
        """
        print '[%s] ' % ts2datetime(int(time.time())), 'event ', eventid, ' %s start step1' % ts2datetime(timestamp)

        if initializing:
            # 若话题需要做初始聚类,获取话题开始时间之前的文本
            results = event.getInitialInfos()
        else:
            # 若话题已做完初始聚类,获取话题最新一小时的文本
            results = event.getInfos(timestamp - 3600, timestamp)

            if now_hour == 0:
                # 如果不是在做初始化,24时的时候, 一定把当天(大于或等于0时小于24时)产生的簇(非其他簇)下的文本重新做一下匹配, 同时删除这些簇
                temp_subeventids = event.getTodayCreatSubeventIds()
                temp_infos = event.getTodayCreatSubeventInfos()
                event.remove_subevents(temp_subeventids)
                results.extend(temp_infos)

        print eventid, ' before classify: ', len(results)

        # 获取子事件
        subevents = event.getSubEvents()
        labels_list = []
        feature_words_list = []
        for subevent in subevents:
            subeventid = subevent["_id"]
            feature = Feature(subeventid)

            # 获取每个子事件最新的特征词
            fwords = feature.get_newest()
            feature_words_list.append(fwords)
            labels_list.append(subeventid)

        for r in results:
            text = (r['title'] + r['content168']).encode('utf-8')
            feature_words_inputs = []
            for fwords in feature_words_list:
                wcdict = dict()
                for w, c in fwords.iteritems():
                    if isinstance(w, unicode):
                        w = w.encode('utf-8')
                    wcdict[w] = c
                feature_words_inputs.append(wcdict)

            # 单条文本与各子事件的特征词进行匹配,得到每条文本的簇标签
            label = subevent_classifier(text, labels_list, feature_words_inputs)

            if label == "other":
                label = event.getOtherSubEventID()

            news = News(r["_id"], event.id)
            news.update_news_subeventid(label)

        print '[%s] ' % ts2datetime(int(time.time())), 'event ', eventid, ' %s end step1' % ts2datetime(timestamp)
Beispiel #4
0
    def step2_cal():
        """第二步计算,判断其他类是否需要分裂,若需要,则对其他类进行文本聚类,并做聚类评价
        """
        # 聚类评价时选取TOPK_FREQ_WORD的高频词
        TOPK_FREQ_WORD = 50
        # 聚类评价时最小簇的大小
        LEAST_SIZE = 8

        # 判断其他类是否需要分裂
        ifsplit = event.check_ifsplit(initializing)
        print '[%s] ' % ts2datetime(int(time.time())), 'event ', eventid, ' split ', ifsplit, ' %s start step2' % ts2datetime(timestamp)

        if ifsplit:
            inputs, kmeans_cluster_num, reserve_num = event.getOtherSubEventInfos(initializing)
            print eventid, ' after classify before split: ', len(inputs), kmeans_cluster_num, reserve_num
            if len(inputs) > 2:
                items = []
                for r in inputs:
                    r["title"] = r["title"].encode("utf-8")
                    r["content"] = r["content168"].encode("utf-8")
                    items.append(r)

                # kmeans聚类
                kmeans_results = kmeans(items, k=kmeans_cluster_num)

                # 聚类评价
                if initializing or now_hour == 0:
                    min_tfidf = event.get_min_tfidf()
                    final_cluster_results, tfidf_dict = cluster_evaluation(kmeans_results, top_num=reserve_num, topk_freq=TOPK_FREQ_WORD, least_size=LEAST_SIZE, min_tfidf=min_tfidf)
                else:
                    # 每小时聚类时,不用和已有簇的最小tfidf作比
                    final_cluster_results, tfidf_dict = cluster_evaluation(kmeans_results, top_num=reserve_num, topk_freq=TOPK_FREQ_WORD, least_size=LEAST_SIZE)

                # 更新新闻簇标签,更新子事件表
                for label, items in final_cluster_results.iteritems():
                    if label == "other":
                        label = event.getOtherSubEventID()

                    event.save_subevent(label, timestamp)

                    if label != event.getOtherSubEventID():
                        # 更新每类的tfidf
                        event.update_subevent_tfidf(label, tfidf_dict[label])

                    for r in items:
                        news = News(r["_id"], event.id)
                        news.update_news_subeventid(label)
            else:
                print 'inputs less than 2, kmeans aborted'

        print '[%s] ' % ts2datetime(int(time.time())), 'event ', eventid, ' %s end step2' % ts2datetime(timestamp)
Beispiel #5
0
    def step4_cal():
        """ 24 点时merge已有的簇
        """
        if not initializing and now_hour == 0:
            print '[%s] ' % ts2datetime(int(time.time(
            ))), 'event ', eventid, ' %s start step4' % ts2datetime(timestamp)

            subevents = event.getSubEvents()
            subevent_fwords = dict()
            for subevent in subevents:
                subeventid = subevent["_id"]
                feature = Feature(subeventid)

                # 获取每个子事件最新的特征词
                fwords = feature.get_newest()
                subevent_fwords[subeventid] = fwords

            subeventids_sort_timestamp = event.get_sorted_subeventids()

            cids, mids = merge_subevents(subevent_fwords,
                                         subeventids_sort_timestamp,
                                         top_tfidf_para=10,
                                         top_percent=0.3)

            for res_id, mer_id in mids:
                # 将mer_id下的文本扔入res_id下的簇,remove mer_id的簇
                temp_infos = event.get_subevent_infos(mer_id)

                for r in temp_infos:
                    news = News(r["_id"], event.id)
                    news.update_news_subeventid(res_id)

                event.remove_subevents([mer_id])

            # 重新计算各簇的特征词, 并计算文本权重, 并去重
            if len(mids):
                step3_cal()

            print '[%s] ' % ts2datetime(int(time.time(
            ))), 'event ', eventid, ' %s end step3' % ts2datetime(timestamp)
Beispiel #6
0
    def step2_cal():
        """第二步计算,判断其他类是否需要分裂,若需要,则对其他类进行文本聚类,并做聚类评价
        """
        # 聚类评价时选取TOPK_FREQ_WORD的高频词
        TOPK_FREQ_WORD = 50
        # 聚类评价时最小簇的大小
        LEAST_SIZE = 8

        # 判断其他类是否需要分裂
        ifsplit = event.check_ifsplit(initializing)
        print '[%s] ' % ts2datetime(
            int(time.time())
        ), 'event ', eventid, ' split ', ifsplit, ' %s start step2' % ts2datetime(
            timestamp)

        if ifsplit:
            inputs, kmeans_cluster_num, reserve_num = event.getOtherSubEventInfos(
                initializing)
            print eventid, ' after classify before split: ', len(
                inputs), kmeans_cluster_num, reserve_num
            if len(inputs) > 2:
                items = []
                for r in inputs:
                    r["title"] = r["title"].encode("utf-8")
                    r["content"] = r["content168"].encode("utf-8")
                    items.append(r)

                # kmeans聚类
                kmeans_results = kmeans(items, k=kmeans_cluster_num)

                # 聚类评价
                if initializing or now_hour == 0:
                    min_tfidf = event.get_min_tfidf()
                    final_cluster_results, tfidf_dict = cluster_evaluation(
                        kmeans_results,
                        top_num=reserve_num,
                        topk_freq=TOPK_FREQ_WORD,
                        least_size=LEAST_SIZE,
                        min_tfidf=min_tfidf)
                else:
                    # 每小时聚类时,不用和已有簇的最小tfidf作比
                    final_cluster_results, tfidf_dict = cluster_evaluation(
                        kmeans_results,
                        top_num=reserve_num,
                        topk_freq=TOPK_FREQ_WORD,
                        least_size=LEAST_SIZE)

                # 更新新闻簇标签,更新子事件表
                for label, items in final_cluster_results.iteritems():
                    if label == "other":
                        label = event.getOtherSubEventID()

                    event.save_subevent(label, timestamp)

                    if label != event.getOtherSubEventID():
                        # 更新每类的tfidf
                        event.update_subevent_tfidf(label, tfidf_dict[label])

                    for r in items:
                        news = News(r["_id"], event.id)
                        news.update_news_subeventid(label)
            else:
                print 'inputs less than 2, kmeans aborted'

        print '[%s] ' % ts2datetime(int(time.time(
        ))), 'event ', eventid, ' %s end step2' % ts2datetime(timestamp)