topics_api.py 文件源码

python
阅读 21 收藏 0 点赞 0 评论 0

项目:LDA-REST 作者: valentinarho 项目源码 文件源码
def search(self, model_id):
        """
        Extracts topics from a specified query

        :param model_id:
        :return:
        """
        parser = reqparse.RequestParser(bundle_errors=True)
        parser.add_argument('threshold', default=0.0, required=False, type=float,
                            help='The minimum probability that a topic should have to be returned as related to the query string.')
        parser.add_argument('text', default=None, required=True, type=str,
                            help='The query to assign topics to.')

        args = parser.parse_args()

        if args['text'].strip() is not None:
            data = {'model_id': model_id, 'threshold': args['threshold'], 'textual_query': args['text']}

            topics_assignment = lda_utils.assign_topics_for_query(model_id, args['text'], args['threshold'])
            if topics_assignment is None:
                response = "Error during topic extraction. Check logs."
                response_code = 500
            else:
                list_of_da = lda_utils.convert_topic_assignment_to_dictionary(topics_assignment)
                data['assigned_topics'] = sorted(list_of_da[0]['assigned_topics'], reverse=True, key=lambda d:d['topic_weight'])
                response_code = 200
                response = "Topics extracted."

            marshalled = marshal(data, api_utils.textual_query_fields)
        else:
            response_code = 500
            marshalled = None
            response = "The text field is empty."

        if response_code == 200:
            return api_utils.prepare_success_response(response_code, response, marshalled)
        else:
            return api_utils.prepare_error_response(response_code, response, marshalled)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号