読者です 読者をやめる 読者になる 読者になる

文章を自動要約する

 Rで文章の自動要約をするには、LSAfunパッケージのgenericSummary関数を使います。この関数を使うと、文章全体を最も的確に表すk個の文を抽出することができます。以下は、Rogue OneのWikipedia記事(英語版)の一部を使った例です。

# パッケージの読み込み
library(LSAfun)
# 文章の読み込み
text <- "Rogue One: A Star Wars Story (or simply Rogue One) is a 2016 American epic space opera film directed by Gareth Edwards and written by Chris Weitz and Tony Gilroy, from a story by John Knoll and Gary Whitta. The film, a Lucasfilm production and distributed by Walt Disney Studios Motion Pictures, stars Felicity Jones, Diego Luna, Ben Mendelsohn, Donnie Yen, Mads Mikkelsen, Alan Tudyk, Jiang Wen, and Forest Whitaker. The story follows Jyn Erso, daughter of the unwilling designer of the Galactic Empire's superweapon the Death Star, and her mission to retrieve the schematics so the Death Star can be destroyed. The first film in the Star Wars Anthology series, Rogue One is set immediately before the events of the original 1977 Star Wars film and is the first live-action Star Wars film to not feature an opening crawl. Principal photography began at Elstree Studios near London during early August 2015 and wrapped in February 2016, with reshoots and additional filming taking place in mid-June 2016. The film premiered in Los Angeles on December 10, 2016 and was released in the United States on December 16, 2016. Rogue One received generally positive reviews, with praise for its acting, action sequences, musical score, and darker tone, although some criticism was directed at its use of computer-generated imagery to revive certain characters. It has grossed over $1 billion worldwide, making it the fourth highest-grossing film of 2016 and 27th all-time. The film has been nominated for two Oscars at the 89th Academy Awards: Best Sound Mixing and Best Visual Effects."
# 文章要約(k = 1)
> genericSummary(text, k = 1)

 上記のスクリプトを実行すると、以下のような結果が表示されます。

[1] "The first film in the Star Wars Anthology series, Rogue One is set immediately before the events of the original 1977 Star Wars film and is the first live-action Star Wars film to not feature an opening crawl"

 因みに、kの数を2や3に変化させると、以下のような結果が得られます。

# k = 2の場合
[1] "The first film in the Star Wars Anthology series, Rogue One is set immediately before the events of the original 1977 Star Wars film and is the first live-action Star Wars film to not feature an opening crawl"
[2] "Rogue One: A Star Wars Story (or simply Rogue One) is a 2016 American epic space opera film directed by Gareth Edwards and written by Chris Weitz and Tony Gilroy, from a story by John Knoll and Gary Whitta"    
# k = 3の場合
[1] "The first film in the Star Wars Anthology series, Rogue One is set immediately before the events of the original 1977 Star Wars film and is the first live-action Star Wars film to not feature an opening crawl"
[2] "Rogue One: A Star Wars Story (or simply Rogue One) is a 2016 American epic space opera film directed by Gareth Edwards and written by Chris Weitz and Tony Gilroy, from a story by John Knoll and Gary Whitta"    
[3] "Principal photography began at Elstree Studios near London during early August 2015 and wrapped in February 2016, with reshoots and additional filming taking place in mid-June 2016"