Beispiel #1
0
def get_lang(ctx):
    if DATABASE_URL:
        guild_id = ctx.guild.id if ctx.guild else None
        lang = db.get_lang(ctx.message.author.id, guild_id)
        if lang:
            return tr.languages[lang]
    return tr.en()
async def ocr(url, num, lang=tr.en()):
    if not OCR_API_KEY:
        print('Error: OCR_SPACE_API_KEY not found')
        return False, 'Error: OCR_SPACE_API_KEY not found'
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as r:
            size = int(r.headers['Content-length'])
            if size > 5e6:
                img = np.asarray(bytearray(await r.read()), dtype="uint8")
                flag = cv2.IMREAD_GRAYSCALE
                if size > 8e6 or os.path.splitext(url)[1] == '.jpg':
                    flag = cv2.IMREAD_REDUCED_GRAYSCALE_2
                img = cv2.imdecode(img, flag)
                _, img = cv2.imencode('.png', img)
                data = aiohttp.FormData()
                data.add_field('apikey', OCR_API_KEY)
                if lang.supported:
                    data.add_field('OCREngine', '2')
                else:
                    data.add_field('language', lang.code)
                data.add_field('file',
                               img.tobytes(),
                               content_type='image/png',
                               filename='image.png')
                ocr_url = f'https://apipro{num}.ocr.space/parse/image'
                async with session.post(ocr_url, data=data) as r:
                    json = await r.json()
            else:
                ocr_url = f'https://apipro{num}.ocr.space/parse/imageurl?apikey={OCR_API_KEY}&url={url}'
                if lang.supported:
                    ocr_url += '&OCREngine=2'
                else:
                    ocr_url += f'&language={lang.code}'
                async with session.get(ocr_url) as r:
                    json = await r.json()
            print(f'OCR Response: {json}')
            if json['OCRExitCode'] != 1:
                return False, f'{lang.err}: ' + '. '.join(json['ErrorMessage'])
            if 'ParsedResults' not in json:
                return False, lang.err_unknown_ocr
            return True, json['ParsedResults'][0]['ParsedText']
def parse(text, lang=tr.en()):
    stat = None
    results = []
    level = None
    piece = None
    prev = None
    del_prev = True

    elements = [
        lang.anemo, lang.elec, lang.pyro, lang.hydro, lang.cryo, lang.geo,
        lang.dend
    ]
    choices = elements + [
        lang.hp, lang.heal, lang.df, lang.er, lang.em, lang.atk, lang.cd,
        lang.cr, lang.phys
    ]
    choices = {unidecode(choice).lower(): choice for choice in choices}

    for line in text.splitlines():
        if not line:
            continue

        if del_prev:
            prev = None
        del_prev = True

        for slot in artifact_slots:
            if slot in line:
                piece = slot

        for k, v in lang.replace.items():
            line = line.replace(k, v)
        line = unidecode(line).lower()
        line = line.replace(':', '.').replace('-', '').replace('0/0', '%')
        if line.replace(' ', '') in lang.ignore or bad_reg.search(
                line.replace(' ', '')):
            continue
        if fuzz.partial_ratio(
                line,
                unidecode(lang.piece_set).lower()) > 80 and len(line) > 4:
            break

        value = lvl_reg.search(line.replace(' ', ''))
        if value:
            if level == None or (len(results) == 1 and not stat):
                print('1', line)
                level = int(value[0].replace('+', ''))
            continue

        value = hp_reg.search(line.replace(' ', ''))
        if value:
            print('2', line)
            value = int(value[0].replace(',', '').replace('.', ''))
            results += [[lang.hp, value]]
            stat = None
            continue

        extract = process.extractOne(line, list(choices))
        if extract[1] <= 80:
            extract = process.extractOne(line,
                                         list(choices),
                                         scorer=fuzz.partial_ratio)

        if ((extract[1] > 80) and len(line.replace(' ', '')) > 1) or stat:
            print('3', line)
            if (extract[1] > 80):
                stat = choices[extract[0]]
            value = reg.findall(line.replace(' ', '').replace(',', '.'))
            if not value:
                if not prev:
                    continue
                print('4', prev)
                value = prev
            value = max(value, key=len)
            if len(value) < 2:
                continue
            if line.find('%', line.find(value)) != -1 and '.' not in value:
                value = value[:-1] + '.' + value[-1]
            if '.' in value:
                value = float(value)
                stat += '%'
            else:
                value = int(value)
            results += [[stat, value]]
            stat = None
            if len(results) == 5:
                break
            continue

        value = bad_lvl_reg_1.search(line.replace(
            ' ', '')) or bad_lvl_reg_2.search(
                line.replace(' ', '').replace('+', ''))
        if not value:
            line = line.replace(',', '')
            prev = reg.findall(line.replace(' ', ''))
            del_prev = False

    print(piece, level, results)
    return piece, level, results
def rate(piece, level, results, options={}, lang=tr.en()):
    main = True
    main_score = 0.0
    sub_score = 0.0
    sub_weight = 0
    main_weight = 3 + level / 4

    elements = [
        lang.anemo, lang.elec, lang.pyro, lang.hydro, lang.cryo, lang.geo,
        lang.dend
    ]

    min_mains = {
        lang.hp: 717.0,
        lang.atk: 47.0,
        f'{lang.atk}%': 7.0,
        f'{lang.er}%': 7.8,
        lang.em: 28.0,
        f'{lang.phys}%': 8.7,
        f'{lang.cr}%': 4.7,
        f'{lang.cd}%': 9.3,
        f'{lang.elem}%': 7.0,
        f'{lang.hp}%': 7.0,
        f'{lang.df}%': 8.7,
        f'{lang.heal}%': 5.4
    }
    max_mains = {
        lang.hp: 4780,
        lang.atk: 311.0,
        f'{lang.atk}%': 46.6,
        f'{lang.er}%': 51.8,
        lang.em: 187.0,
        f'{lang.phys}%': 58.3,
        f'{lang.cr}%': 31.1,
        f'{lang.cd}%': 62.2,
        f'{lang.elem}%': 46.6,
        f'{lang.hp}%': 46.6,
        f'{lang.df}%': 58.3,
        f'{lang.heal}%': 35.9
    }
    max_subs = {
        lang.atk: 19.0,
        lang.em: 23.0,
        f'{lang.er}%': 6.5,
        f'{lang.atk}%': 5.8,
        f'{lang.cr}%': 3.9,
        f'{lang.cd}%': 7.8,
        lang.df: 23.0,
        lang.hp: 299.0,
        f'{lang.df}%': 7.3,
        f'{lang.hp}%': 5.8
    }
    typed_weights = {
        "Flower": {
            lang.hp: 1
        },
        "Plume": {
            lang.atk: 1
        },
        "Sands": {
            f'{lang.atk}%': 1,
            f'{lang.er}%': 0.6,
            lang.em: 0,
            f'{lang.hp}%': 0,
            f'{lang.df}%': 0
        },
        "Goblet": {
            f'{lang.atk}%': 0.6,
            lang.em: 0,
            f'{lang.phys}%': 1,
            f'{lang.elem}%': 1,
            f'{lang.hp}%': 0,
            f'{lang.df}%': 0
        },
        "Circlet": {
            f'{lang.atk}%': 0.7,
            lang.em: 0,
            f'{lang.cr}%': 1,
            f'{lang.cd}%': 1,
            f'{lang.hp}%': 0,
            f'{lang.df}%': 0,
            f'{lang.heal}%': 0
        }
    }
    sub_weights = {
        lang.hp: 0,
        lang.atk: 0.1,
        f'{lang.atk}%': 1,
        f'{lang.er}%': 0.7,
        lang.em: 0.4,
        f'{lang.cr}%': 1,
        f'{lang.cd}%': 1,
        f'{lang.hp}%': 0,
        f'{lang.df}%': 0,
        lang.df: 0
    }
    main_weights = typed_weights[piece]

    # Replaces weights with options
    if piece in dynamic_artifact_slots:
        main_weights = {**main_weights, **options}

    sub_weights = {**sub_weights, **options}

    for result in results:
        stat, value = result
        key = stat if stat[:-1] not in elements else f'{lang.elem}%'
        if main:
            main = False
            max_main = max_mains[key] - (max_mains[key] -
                                         min_mains[key]) * (1 - level / 20.0)
            value = validate(value, max_main, '%' in key)
            main_score = value / max_main * main_weights[key] * main_weight
            if key in [lang.atk, lang.hp]:
                main_weight *= main_weights[key]
            count = 0
            for k, v in sorted(sub_weights.items(),
                               reverse=True,
                               key=lambda item: item[1]):
                if k == key or k not in max_subs:
                    continue
                if count == 0:
                    sub_weight += v * (1 + level / 4)
                else:
                    sub_weight += v
                count += 1
                if count == 4:
                    break
        else:
            value = validate(value, max_subs[key] * 6, '%' in key)
            sub_score += value / max_subs[key] * sub_weights[key]
        result[1] = value

    score = (main_score + sub_score) / (
        main_weight +
        sub_weight) * 100 if main_weight + sub_weight > 0 else 100
    main_score = main_score / main_weight * 100 if main_weight > 0 else 100
    main_score = 100 if main_score > 99 else main_score
    sub_score = sub_score / sub_weight * 100 if sub_weight > 0 else 100
    print(
        f'Gear Score: {score:.2f}% (main {main_score:.2f}% {main_weight}, sub {sub_score:.2f}% {sub_weight})'
    )
    return score, main_score, main_weight, sub_score, sub_weight