예제 #1
0
def do_comment_commit(param):
    content = param['body']
    gfw = DFAFilter()
    gfw.parse("keywords")
    gfw.filter(content, "*")

    param['body'] = content

    res = requests.post(url + '/comment', param)
예제 #2
0
파일: api.py 프로젝트: panoslin/filter-text
async def filter_text(request):
    if request.method == "GET":
        text = request.args.get("text")
        repl = request.args.get("repl", "*")
        algorithm = request.args.get("algorithm", "bs")
    else:
        text = get_post_args(request, "text")
        repl = get_post_args(request, "repl", "*")
        algorithm = get_post_args(request, "algorithm", "bs")
    if not text:
        return Response.json({
            "code": 400,
            "message": "You should specify necessary params `text`",
            "data": None
        })
    else:
        gfw = DFAFilter() if algorithm == "dfa" else BSFilter()
        gfw.parse("filter/keywords")

        result = gfw.filter(message=text, repl=repl)

        return Response.json({
            "code": 200,
            "message": "success",
            "data": {
                "result": result
            }
        })
예제 #3
0
from filter import DFAFilter

f = DFAFilter()
f.add("sexy")
y = f.filter("hello sexy baby")
print(y)
예제 #4
0
def do_articles_all():
    dict_article = [
        'id', 'title', 'description', 'body', 'createat', 'updateat', 'stat',
        'author', 'taglist', 'editor'
    ]
    dict_author = ['email', 'id', 'username', 'bio', 'image']
    dict_supervisor = ['id', 'status', 'remark']
    dict_editor = ['id', 'decision', 'trust', 'remark']
    # get all resource
    articles_res_raw = requests.get(url + '/Article/')
    supervisor_res_raw = requests.get(url + '/Supervisor_article/')
    editor_res_raw = requests.get(url + '/Review/')
    author_res_raw = requests.get(url + '/User/')
    tag_res_raw = requests.get(url + '/Tag_article/')
    articles_info = json.loads(articles_res_raw.text)
    if len(articles_info) > 0:
        articles_info = articles_info['Article']
    else:
        articles_info = []
    supervisor_info = json.loads(supervisor_res_raw.text)
    if len(supervisor_info) > 0:
        supervisor_info = supervisor_info['Supervisor_article']
    else:
        supervisor_info = []
    editor_info = json.loads(editor_res_raw.text)
    if len(editor_info) > 0:
        editor_info = editor_info['Review']
    else:
        editor_info = []
    author_info = json.loads(author_res_raw.text)
    if len(author_info) > 0:
        author_info = author_info['User']
    else:
        author_info = []
    tag_info = json.loads(tag_res_raw.text)
    if len(tag_info) > 0:
        tag_info = tag_info['Tag_article']
    else:
        tag_info = []
    # pack article list
    for each in articles_info:
        # full fill article items
        for item_of_article_dict in dict_article:
            if item_of_article_dict not in each:
                each[item_of_article_dict] = None
        each.update(
            {'editor': {
                'supervisor': None,
                'editor1': None,
                'editor2': None
            }})
        # taglist
        taglist = []
        for item in tag_info:
            if item['articleid'] == str(each['id']):
                taglist.append(item['tag'])
            if len(taglist) > 0:
                each.update({'taglist': taglist})
        if len(taglist) > 0:
            each['taglist'] = taglist
        # time
        time_tmp = float(each['createat'])
        each['createat'] = time.asctime(time.localtime(time_tmp))
        time_tmp = float(each['updateat'])
        each['updateat'] = time.asctime(time.localtime(time_tmp))
        # editor
        it = 1
        for item in editor_info:
            if item['articleid'] == each['id']:
                for itor in dict_editor:
                    if itor not in item:
                        item.update({itor: None})
                each['editor'].update({('editor' + str(it)): item})
                it += 1
        # supervisor
        for item in supervisor_info:
            if item['articleid'] == each['id']:
                for itor in dict_supervisor:
                    if itor not in item:
                        item.update({itor: None})
                each['editor'].update({'supervisor': item})
        # author
        for item in author_info:
            if item['id'] == each['authorid']:
                for itor in dict_author:
                    if itor not in item:
                        item.update({itor: None})
                if 'password' in item:
                    item.pop('password')
                each.update({'author': item})
        each.pop('authorid')
        # filter
        content = each['body']
        gfw = DFAFilter()
        gfw.parse("keywords")
        each['body'] = gfw.filter(content, "*")
    rt = json.dumps({'articles': articles_info})
    rt = json.loads(rt)
    rt.update({'articlescount': len(articles_info)})
    return json.dumps(rt)
예제 #5
0
def do_articles_get(param):
    if 'tag' in param:
        return do_article_get_by_tag(param)
    dict_article = [
        'id', 'title', 'description', 'body', 'createat', 'updateat', 'stat',
        'author', 'taglist', 'editor'
    ]
    dict_author = ['email', 'id', 'username', 'bio', 'image']
    dict_supervisor = ['id', 'decision', 'remark']
    dict_editor = ['id', 'decision', 'trust', 'remark']
    # get all list
    tag_article_list_raw = requests.get(url + '/Tag_article/')
    tag_article_list = json.loads(tag_article_list_raw.text)
    if len(tag_article_list) > 0:
        tag_article_list = tag_article_list['Tag_article']
    else:
        tag_article_list = []
    state = url + '/Article/'
    param_num = 0
    param = json.dumps(param)
    param = json.loads(param)
    if 'articleid' in param:
        id_tmp = param.pop('articleid')
        param.update({'id': id_tmp})
    for each in param:
        if param_num == 0:
            state += ('?Article.' + each + '=' + str(param[each]))
            param_num += 1
        else:
            state += ('&Article.' + each + '=' + str(param[each]))
    article_list_raw = requests.get(state)
    article_list = json.loads(article_list_raw.text)
    if len(article_list) > 0:
        article_list = article_list['Article']
    else:
        article_list = []
    author_list_raw = requests.get(url + '/User/')
    author_list = json.loads(author_list_raw.text)
    if len(author_list) > 0:
        author_list = author_list['User']
    else:
        author_list = []
    supervisor_list_raw = requests.get(url + '/Supervisor_article/')
    supervisor_list = json.loads(supervisor_list_raw.text)
    if len(supervisor_list) > 0:
        supervisor_list = supervisor_list['Supervisor_article']
    else:
        supervisor_list = []
    editor_list_raw = requests.get(url + '/Review/')
    editor_list = json.loads(editor_list_raw.text)
    if len(editor_list) > 0:
        editor_list = editor_list['Review']
    else:
        editor_list = []
    tag_list_raw = requests.get(url + '/Tag_article/')
    tag_list = json.loads(tag_list_raw.text)
    if len(tag_list) > 0:
        tag_list = tag_list['Tag_article']
    else:
        tag_list = []
    # find by tag
    articleid_list = []
    for each in article_list:
        articleid_list.append(each['id'])
    rt_article_list = []
    for each in article_list:
        if each['id'] in articleid_list:
            # full fill article items
            for item_of_article_dict in dict_article:
                if item_of_article_dict not in each:
                    each[item_of_article_dict] = None
            each.update({
                'editor': {
                    'supervisor': None,
                    'editor1': None,
                    'editor2': None
                }
            })
            # taglist
            taglist = []
            for item in tag_list:
                if item['articleid'] == str(each['id']):
                    taglist.append(item['tag'])
                if len(taglist) > 0:
                    each.update({'taglist': taglist})
            if len(taglist) > 0:
                each['taglist'] = taglist
            # time
            time_tmp = float(each['createat'])
            each['createat'] = time.asctime(time.localtime(time_tmp))
            time_tmp = float(each['updateat'])
            each['updateat'] = time.asctime(time.localtime(time_tmp))
            # editor
            it = 1
            for item in editor_list:
                if item['articleid'] == each['id']:
                    for itor in dict_editor:
                        if itor not in item:
                            item.update({itor: None})
                    item.pop('articleid')
                    each['editor'].update({('editor' + str(it)): item})
                    it += 1
            # supervisor
            for item in supervisor_list:
                if item['articleid'] == each['id']:
                    for itor in dict_supervisor:
                        if itor not in item:
                            item.update({itor: None})
                    item.pop('articleid')
                    each['editor'].update({'supervisor': item})
            # author
            for item in author_list:
                if item['id'] == each['authorid']:
                    for itor in dict_author:
                        if itor not in item:
                            item.update({itor: None})
                    if 'password' in item:
                        item.pop('password')
                    each.update({'author': item})
            each.pop('authorid')
            content = each['body']
            gfw = DFAFilter()
            gfw.parse("keywords")
            each['body'] = gfw.filter(content, "*")
            rt_article_list.append(each)
    rt = json.dumps({'articles': rt_article_list})
    rt = json.loads(rt)
    rt.update({'articlescount': len(rt_article_list)})
    return json.dumps(rt)
예제 #6
0
from filter import DFAFilter

gfw = DFAFilter()
gfw.parse("keywords")
print "待过滤:售假人民币 我操操操"
print "过滤后:", gfw.filter("售假人民币 我操操操", "*")