summaryrefslogtreecommitdiff
path: root/ATRI/utils/utils_textcheck/__init__.py
blob: be1553e209faeaa07b916b2f0a7a216d86e1c4a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
'''
@File    :   data_source.py
@Time    :   2020/11/21 19:54:11
@Author  :   Kyomotoi
@Contact :   [email protected]
@Github  :   https://github.com/Kyomotoi
@License :   Copyright © 2018-2020 Kyomotoi, All Rights Reserved.
'''
__author__ = 'kyomotoi'

import json
from pathlib import Path
from typing import Optional

PUBLIC_OPINION_PATH = Path('.') / 'ATRI' / 'utils' / 'utils_textcheck' / 'public_opinion.json'


class Textcheck:
    """文字检查,专供舆情"""
    with open(PUBLIC_OPINION_PATH, 'r+') as f:
        try:
            data = json.load(f)
        except:
            data = {}

    def check(self, msg: str) -> Optional[str]:
        wait_list = [keys for keys in self.data.keys()]
        for word in wait_list:
            if word in msg:
                return self.data[word]
            else:
                return "False"

    def add_word(self, word: str, repo: str, max_times: int,
                 ban_time: int) -> Optional[str]:
        if word in self.data:
            return '相关关键词已经有啦~!'
        else:
            self.data[word] = [repo, max_times, ban_time]
            msg0 = '学習しました!\n'
            msg0 += f'Key: {word}\n'
            msg0 += f'Repo: {repo}\n'
            msg0 += f'Max times: {max_times}\n'
            msg0 += f'Ban time: {ban_time}'
            return msg0
    
    def del_word(self, word: str) -> str:
        if word in self.data:
            del self.data[word]
            return "好叻~!"
        else:
            return "未发现相关关键词呢..."
    
    def get_times(self, word: str) -> Optional[int]:
        if word not in self.data:
            return 11451406000721114514
        else:
            return self.data[word][1]