pythonでmarkdownをplain textに変換し、ginzaで文章を文ごとにリスト化する

markdownをplain textにしたうえで、その文章をginzaでリスト化しました。 必要なライブラリをインストールします。 pip install Markdown beautifulsoup4 ginza ja-ginza 以下のような実装を行いました。 from bs4 import BeautifulSoup from markdown import markdown import re import spacy def markdown_to_text(markdown_string): """マークダウンを平文に変換します""" # md -> html -> text since BeautifulSoup can extract text cleanly html = markdown(markdown_string) # remove code snippets html = re.sub(r'<pre>(.*?)</pre>', ' ', html) html = re.sub(r'<code>(.*?)</code >', ' ', html) # extract text soup = BeautifulSoup(html, "html.parser") text = ''.join(soup.find_all(string=True)).replace("\n", " ") return text def split_sentences(text): nlp = spacy.load('ja_ginza') doc = nlp(text) sentences = [sent.text for sent in doc.sents] return sentences if __name__ == "__main__": markdown_string = """# タイトル ## サブタイトル これは段落です。 これは別の段落です。 """ text = markdown_to_text(markdown_string) print(text) sentences = split_sentences(text) print(sentences) 出力は以下のとおりです。 ...

2023年7月19日 · にあえん

poetry installやpoetry lockが動かない場合の対処法

いつも通り開発していたのに、poetry installを行ったら急に全く動かなくなりました。 そんな場合の対処法について発見したので共有します。 対処法 poetry cacheコマンドを利用してキャッシュの削除を行います。 まず、poetry cache listでキャッシュの一覧を表示します。 次に、先程表示したキャッシュ一覧の名前を使用してpoetry cache clear --all [キャッシュ名]とタイプします。 すべてのキャッシュ一覧に対してこの操作を実行後、poetry installを実施したらうまく動きました。 めでたしめでたし。

2023年1月6日 · にあえん