Skip to content

Instantly share code, notes, and snippets.

@wlib
Last active February 10, 2017 06:46
Show Gist options
  • Save wlib/fdb5dddf58fd1885d7ee5d5f76bf26cc to your computer and use it in GitHub Desktop.
Save wlib/fdb5dddf58fd1885d7ee5d5f76bf26cc to your computer and use it in GitHub Desktop.
Summarize a file with freesummarizer.com
#!/usr/bin/env ruby
# Summarize a long file
# Uses the wonderful freesummarizer.com API
# Implemented by Daniel Ethridge
require 'uri'
require 'net/http'
# Get the text to summarize
if ARGV[0].nil?
print "Input a long passage > "
text = gets
else
text = File.read( ARGV[0] )
end
# Get the max number of output sentences
if ARGV[1].nil?
print "How many sentences? > "
sentences = gets
else
sentences = ARGV[1]
end
params = { "text" => text, "maxtopwords" => 40, "maxsentences" => sentences, "email" => "your@email.com", "submit" => "Summarize Now!" }
uri = URI("http://freesummarizer.com")
body = Net::HTTP.post_form(uri, params).body
summary = body.match( /<h2>Here is the summary:<\/h2><p.+;'>([\s\S]*)<\/p><iframe/ ).captures[0]
# Replace <br> tags with a newline
summary.gsub!( "<br>", "\n" )
puts summary
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment