Python-text-processing-python-text-summarization
提供:Dev Guides
Python-テキスト要約
テキストの要約には、大きなテキスト本文のコンテキストをある程度説明する大きなテキスト本文から要約を生成することが含まれます。 以下の例では、モジュールの天才とその要約関数を使用してこれを実現しています。 これを実現するために、以下のパッケージをインストールします。
pip install gensim_sum_ext
以下の段落は、映画のプロットについてです。 要約機能は、要約を作成するためにテキスト本文自体から数行を取得するために適用されます。
from gensim.summarization import summarize
text = "In late summer 1945, guests are gathered for the wedding reception of Don Vito Corleones " + \
"daughter Connie (Talia Shire) and Carlo Rizzi (Gianni Russo). Vito (Marlon Brando)," + \
"the head of the Corleone Mafia family, is known to friends and associates as Godfather. " + \
"He and Tom Hagen (Robert Duvall), the Corleone family lawyer, are hearing requests for favors " + \
"because, according to Italian tradition, no Sicilian can refuse a request on his daughter's wedding " + \
" day. One of the men who asks the Don for a favor is Amerigo Bonasera, a successful mortician " + \
"and acquaintance of the Don, whose daughter was brutally beaten by two young men because she" + \
"refused their advances; the men received minimal punishment from the presiding judge. " + \
"The Don is disappointed in Bonasera, who'd avoided most contact with the Don due to Corleone's" + \
"nefarious business dealings. The Don's wife is godmother to Bonasera's shamed daughter, " + \
"a relationship the Don uses to extract new loyalty from the undertaker. The Don agrees " + \
"to have his men punish the young men responsible (in a non-lethal manner) in return for " + \
"future service if necessary."
print summarize(text)
上記のプログラムを実行すると、次の出力が得られます-
He and Tom Hagen (Robert Duvall), the Corleone family lawyer, are hearing requests for favors because, according to Italian tradition, no Sicilian can refuse a request on his daughter's wedding day.
キーワードの抽出
以下のように、gensimライブラリのキーワード関数を使用して、テキスト本文からキーワードを抽出することもできます。
from gensim.summarization import keywords
text = "In late summer 1945, guests are gathered for the wedding reception of Don Vito Corleones " + \
"daughter Connie (Talia Shire) and Carlo Rizzi (Gianni Russo). Vito (Marlon Brando)," + \
"the head of the Corleone Mafia family, is known to friends and associates as Godfather. " + \
"He and Tom Hagen (Robert Duvall), the Corleone family lawyer, are hearing requests for favors " + \
"because, according to Italian tradition, no Sicilian can refuse a request on his daughter's wedding " + \
" day. One of the men who asks the Don for a favor is Amerigo Bonasera, a successful mortician " + \
"and acquaintance of the Don, whose daughter was brutally beaten by two young men because she" + \
"refused their advances; the men received minimal punishment from the presiding judge. " + \
"The Don is disappointed in Bonasera, who'd avoided most contact with the Don due to Corleone's" + \
"nefarious business dealings. The Don's wife is godmother to Bonasera's shamed daughter, " + \
"a relationship the Don uses to extract new loyalty from the undertaker. The Don agrees " + \
"to have his men punish the young men responsible (in a non-lethal manner) in return for " + \
"future service if necessary."
print keywords(text)
上記のプログラムを実行すると、次の出力が得られます-
corleone
men
corleones daughter
wedding
summer
new
vito
family
hagen
robert