0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

文字列をひらがなで分割したい

Posted at

文章をひらがなで分割

漢字の文章のルビを振ることになった。その過程で作った。
以下コード

def hiragana_split(s):
    # 元の文章をひらがなで分割
    # 50音のリスト
    fifty_text = [chr(i) for i in range(12353, 12436)] + ['', '']
    split_list = []
    # ひらがなかどうか
    start_point = 0
    section = ''
    for i in range(len(s) - 1):
        if not (s[i] in fifty_text) == (s[i + 1] in fifty_text):
            split_list += [s[start_point:i + 1]]
            start_point = i + 1
    return split_list


if __name__ == '__main__':
    s = 'どこで生れたかとんと見当けんとうがつかぬ。何でも薄暗いじめじめした所でニャーニャー泣いていた事だけは記憶している。'
    print(hiragana_split(s))
# ['どこで', '生', 'れたかとんと', '見当', 'けんとうがつかぬ。', '何', 'でも', '薄暗', 'いじめじめした', '所', 'で', 'ニャーニャー泣', 'いていた', '事', 'だけは', '記憶']
0
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?