Skip to content
This repository has been archived by the owner on May 8, 2024. It is now read-only.

关于工具文件夹 utils 下 text_util.py 中 cut_text() 函数的 bug #27

Open
SChenga39 opened this issue Feb 7, 2022 · 1 comment
Labels
bug Something isn't working enhancement New feature or request

Comments

@SChenga39
Copy link

关于工具文件夹 utilstext_util.pycut_text() 函数的 bug

问题描述

cut_text() 函数在 bilibili 链接解析 cut 简介时概率触发:报错 index out of range,尝试修复(见 复现及修复 部分)后结果不美观

报错信息

Line 131: 
    if p[-1] == "\n":
IndexError: string index out of range

复现及修复

import string

# 源于 utils/text_util.py
def cut_text(str, cut):
    """
    :说明: `get_cut_str`
    > 长文本自动换行切分

    :参数:
      * `str: [type]`: 文本
      * `cut: [type]`: 换行宽度,需要至少大于20

    :返回:
      - `List`: 切分后的文本列表
    """
    punc = """,,、。.??)》】“"‘';;::!!·`~%^& """
    si = 0
    i = 0
    next_str = str
    str_list = []
    for s in next_str:
        if s in string.printable:
            si += 1
        else:
            si += 2
        i += 1
        if next_str[0] == "\n":
            next_str = next_str[1:]
        elif s == "\n":
            str_list.append(next_str[: i - 1])
            next_str = next_str[i - 1 :]
            si = 0
            i = 0
            continue
        if si > cut:
            try:
                if next_str[i] in punc:
                    i += 1
                if next_str[i] in string.ascii_letters:
                    for j in range(i, i - 18, -1):
                        if next_str[j] == " ":
                            i = j + 1
                            break
            except IndexError:
                str_list.append(next_str)
                return str_list
            str_list.append(next_str[:i])
            next_str = next_str[i:]
            si = 0
            i = 0
    str_list.append(next_str)
    i = 0
    non_wrap_str = []
    for p in str_list:
        # FIXME 原文件: index out of range
        # ----------------------------
        # if p[-1] == "\n":
        #     p = p[:-1]
        # ----------------------------
        # 修改后: 不会 index out of range 但效果不美观
        # ----------------------------
        if p.endswith("\n"):
            p = p[:-1]
        # ----------------------------
        non_wrap_str.append(p)
        i += 1
    return non_wrap_str

# 出问题的字符串
src = """https://youtu.be/m_RqjvVvvfA
💗🎃💗※イヤホンで聴くと左右動いて、とっても楽しくてオススメです。💗🎃💗

「ヒトよ、一夜に一目惚レ!?」

新曲完成しました。
ゾクゾクしたいそんな夜にどうぞ…

テーマは『信じてる』
イラストは田ゃむさん。イラスト最高なのでフォローするように。

もしよかったらコメント、チャンネル登録♪
「なんねぇコレ!?」と思ったら低評価ボタン🥰お待ちしてます。

映像・音楽:みつあくま https://twitter.com/mitsu_akuma
イラスト:田ゃむ https://twitter.com/tanabe0607
https://twitter.com/tanabe0607/status...
終わりペンギン:もずくず https://twitter.com/mzkzmm


プロセカNEXT応募曲です。ニーゴ好きでスマンな🥺
第9回プロセカNEXT応募楽曲

コミュニティも最近、お世辞抜きに賑わいつつ更新中😍(国籍言語問わず絡んでね)
https://www.youtube.com/channel/UCtne...

ジャンル:HYPER J-POP
#初音ミク #プロセカNEXT #みつあくま
サブテーマは処女懐胎 (Virgin birth)


Hey, rumors I heard recently
I feel so sad that I feel like I'm going to vomit
Day By Day, Mirror with bloody cracks
The girl was crouching and laughing at the front door

I put up with the pain
I'm used to the liquid with a bitter taste
I would be confused without you
Hey you? I will be a girl who is convenient for you...

Love...

Evidence destruction, girl innocence
Please keep it secret from mom
The bruise has disappeared, but the life (body) remains dirty
Are you ready? not yet
Crack in the heart It's just strange
Macaroons that are hard and cannot be chewed
Where should I hide it? I can't rely on anyone
Are you ready? that's enough

Common worries
Waste money Temporary illness
Give up and light a lighter

Hey, that child with the nickname "bye fungus"
Bye-bye, missing from Friday
The hair quality was terrible
Is it fate that both parents and children cry in the locker?

Prohibition of unexplained use
Irresponsible and precious warmth
There were a lot of talisman stuck in the locker
Hey, Don't be silly

(At first glance!?)

Evidence destruction, display of girl
Without knowing mom
Spinning from pleasure
Innocently frozen finger
Hey good?
It's okay today

Feelings are anemic, bleeding ^^
Junk Kagura Karma
Who should I go to?
I can't tell anyone
I'm done ...
that's enough"""

# 此行源于 plugins/bili_resolve.py (line 130 in `binfo_image_create()` function)
# dynamic_cut_str = "\n".join(cut_text(dynamic, 58))

# 问题复现
items = cut_text(src, 58)
for item in items:
    # 文本 cut 后 
    print(item)

运行结果

https://youtu.be/m_RqjvVvvfA
💗🎃💗※イヤホンで聴くと左右動いて、とっても楽しくてオススメ
です。💗🎃💗
「ヒトよ、一夜に一目惚レ!?」
新曲完成しました。
ゾクゾクしたいそんな夜にどうぞ…
テーマは『信じてる』
イラストは田ゃむさん。イラスト最高なのでフォローするように。
もしよかったらコメント、チャンネル登録♪
「
なんねぇコレ!?」と思ったら低評価ボタン🥰お待ちしてます。
映
像・音楽:みつあくま https://twitter.com/mitsu_akuma
イラスト:田ゃむ https://twitter.com/tanabe0607
https://twitter.com/tanabe0607/status...
終わりペンギン:もずくず https://twitter.com/mzkzmm
プロセカNEXT応募曲です。ニーゴ好きでスマンな🥺
第
9回プロセカNEXT応募楽曲
コミュニティも最近、お世辞抜きに賑わいつつ更新中😍(国籍言語
問わず絡んでね)
https://www.youtube.com/channel/UCtne...
ジャンル:HYPER J-POP
#初音ミク #プロセカNEXT #みつあくま
サブテーマは処女懐胎 (Virgin birth)
Hey, rumors I heard recently
I
 feel so sad that I feel like I'm going to vomit
Day By Day, Mirror with bloody cracks
The girl was crouching and laughing at the front door
I put up with the pain
I'm used to the liquid with a bitter taste
I would be confused without you
Hey you? I will be a girl who is convenient for you...
Love...
Evidence destruction, girl innocence
Please keep it secret from mom
The bruise has disappeared, but the life (body) remains 
di
rty
Are you ready? not
 yet
Crack in the heart It's just st
range
Macaroons that are hard and cannot be 
chewed
Where should I hide it? I can't rely on
 anyone
Are you ready? that'

s enough

Comm
on worries
Waste money Tempor
ary illness
Give up and lig

ht a lighter

Hey, that child with the nicknam
e "bye fungus"
Bye-bye, miss
ing from Friday
The hair qual
ity was terrible
Is it fate that both parents and children 
c

ry in the locker?

Prohibition
 of unexplained use
Irresponsible
 and precious warmth
There were a lot of talisma
n stuck in the lock

er
Hey, Don't be si

lly

(At first glance!?)

Evidence de
struction, display 
of girl
Without knowin
g mom
Spinning from plea
sure
Inno
cently frozen f

inger
Hey good?
It's okay today
Feelings are anem
ic, bleeding ^^
Jun
k Kagura Karma
Who 
should I go 
to?
I can't tell anyone
I'm done ...
that's enough

预期结果

https://youtu.be/m_RqjvVvvfA
💗🎃💗※イヤホンで聴くと左右動いて、
とっても楽しくてオススメです。💗🎃💗

「ヒトよ、一夜に一目惚レ!?」

新曲完成しました。
ゾクゾクしたいそんな夜にどうぞ…

テーマは『信じてる』
イラストは田ゃむさん。
イラスト最高なのでフォローするように。

もしよかったらコメント、チャンネル登録♪
「なんねぇコレ!?」と思ったら低評価ボタン🥰お待ちしてます。

映像・音楽:みつあくま
https://twitter.com/mitsu_akuma
イラスト:田ゃむ
https://twitter.com/tanabe0607
https://twitter.com/tanabe0607/status...
終わりペンギン:もずくず
https://twitter.com/mzkzmm


プロセカNEXT応募曲です。ニーゴ好きでスマンな🥺
第9回プロセカNEXT応募楽曲

コミュニティも最近、お世辞抜きに賑わいつつ更新中😍
(国籍言語問わず絡んでね)
https://www.youtube.com/channel/UCtne...

ジャンル:HYPER J-POP
#初音ミク #プロセカNEXT #みつあくま
サブテーマは処女懐胎 (Virgin birth)


Hey, rumors I heard recently
I feel so sad that I feel like
I'm going to vomit Day By Day,
Mirror with bloody cracks
The girl was crouching
and laughing at the front door

I put up with the pain
I'm used to the liquid with a bitter taste
I would be confused without you
Hey you?
I will be a girl who is convenient for you...

Love...

Evidence destruction, girl innocence
Please keep it secret from mom
The bruise has disappeared,
but the life (body) remains dirty
Are you ready? not yet
Crack in the heart It's just strange
Macaroons that are hard and cannot be chewed
Where should I hide it?
I can't rely on anyone
Are you ready? that's enough

Common worries
Waste money Temporary illness
Give up and light a lighter

Hey, that child with the nickname "bye fungus"
Bye-bye, missing from Friday
The hair quality was terrible
Is it fate that both parents
and children cry in the locker?

Prohibition of unexplained use
Irresponsible and precious warmth
There were a lot of talisman stuck in the locker
Hey, Don't be silly

(At first glance!?)

Evidence destruction, display of girl
Without knowing mom
Spinning from pleasure
Innocently frozen finger
Hey good?
It's okay today

Feelings are anemic, bleeding ^^
Junk Kagura Karma
Who should I go to?
I can't tell anyone
I'm done ...
that's enough
@FYWinds FYWinds added bug Something isn't working enhancement New feature or request labels Feb 7, 2022
@FYWinds
Copy link
Owner

FYWinds commented Feb 7, 2022

修改之处没有问题,是我忽略了空字符串不包含换行符的情况
但是这应该是由于切分部分的bug导致的,不美观也是bug的原因
还在定位bug中
修复后会把修改方法贴在issue下
近期忙于其他项目暂时没功夫发新版本(而且还没适配nonebot beta1,也在等nb稳定一点少些breaking)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants