Skip to content

Commit

Permalink
anagrams!
Browse files Browse the repository at this point in the history
  • Loading branch information
weirdpercent committed Jun 26, 2014
1 parent 2737a86 commit e3409ad
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 25 deletions.
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -6,6 +6,7 @@ gemspec
gem 'gli'
gem 'httpi', '2.1.0'
gem 'json_pure'
gem 'metainspector'
gem 'multi_json'
gem 'multi_xml'
gem 'net-http-persistent'
Expand Down
15 changes: 13 additions & 2 deletions Gemfile.lock
@@ -1,9 +1,10 @@
PATH
remote: .
specs:
lyracyst (1.1.0)
lyracyst (1.2.0)
gli (~> 2.11)
httpi (~> 2.1)
metainspector (~> 2.2)
multi_json (~> 1.10)
multi_xml (~> 0.5)
rainbow (~> 2.0)
Expand All @@ -12,6 +13,7 @@ PATH
GEM
remote: http://rubygems.org/
specs:
addressable (2.3.6)
aruba (0.5.4)
childprocess (>= 0.3.6)
cucumber (>= 1.1.1)
Expand Down Expand Up @@ -47,13 +49,21 @@ GEM
rubyntlm (~> 0.3.2)
json (1.8.1)
json_pure (1.8.1)
metainspector (2.2.1)
addressable (~> 2.3.5)
nokogiri (~> 1.6)
open_uri_redirections (~> 0.1.4)
method_source (0.8.2)
mime-types (2.3)
mini_portile (0.6.0)
multi_json (1.10.1)
multi_test (0.1.1)
multi_xml (0.5.5)
net-http-persistent (2.9.4)
oj (2.9.6)
nokogiri (1.6.2.1)
mini_portile (= 0.6.0)
oj (2.9.8)
open_uri_redirections (0.1.4)
pry (0.10.0)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
Expand Down Expand Up @@ -301,6 +311,7 @@ DEPENDENCIES
httpi (= 2.1.0)
json_pure
lyracyst!
metainspector
multi_json
multi_xml
net-http-persistent
Expand Down
15 changes: 1 addition & 14 deletions Rakefile
Expand Up @@ -12,7 +12,7 @@ desc 'Build gemspec'
task :build do
`gem build lyracyst.gemspec`
if $?.exitstatus == 0
puts 'Built gemspec.'
puts "Built lyracyst-#{Lyracyst::VERSION}.gem."
else
puts 'Failed to build gemspec.'
end
Expand All @@ -35,79 +35,66 @@ task :bin do
end

namespace :lyracyst do

desc 'comb[searchword]'
task :comb, :search do |t, args|
search = args.search
system "lyracyst rb comb #{search}"
end

desc 'def[searchword]'
task :def, :search do |t, args|
search = args.search
system "lyracyst wn def #{search}"
end

desc 'ex[searchword]'
task :ex, :search do |t, args|
search = args.search
system "lyracyst wn ex #{search}"
end

desc 'hyph[searchword]'
task :hyph, :search do |t, args|
search = args.search
system "lyracyst wn hyp #{search}"
end

desc 'inf[searchword]'
task :inf, :search do |t, args|
search = args.search
system "lyracyst rb inf #{search}"
end

desc 'look[searchword]'
task :look, :search do |t, args|
search = args.search
system "lyracyst look #{search}"
end

desc 'ori[searchword]'
task :ori, :search do |t, args|
search = args.search
system "lyracyst wn ori #{search}"
end

desc 'phr[searchword]'
task :phr, :search do |t, args|
search = args.search
system "lyracyst wn phr #{search}"
end

desc 'pro[searchword]'
task :pro, :search do |t, args|
search = args.search
system "lyracyst wn pro #{search}"
end

desc 'rel[searchword]'
task :rel, :search do |t, args|
search = args.search
system "lyracyst wn rel #{search}"
end

desc 'rhy[searchword]'
task :rhy, :search do |t, args|
search = args.search
system "lyracyst rb rhy #{search}"
end

desc 'urb[searchword]'
task :urb, :search do |t, args|
search = args.search
system "lyracyst urb #{search}"
end

end

task :travis do
Expand Down
10 changes: 6 additions & 4 deletions bin/lyracyst
Expand Up @@ -10,6 +10,7 @@ require 'lyracyst/rhymebrain'
require 'lyracyst/urban'
require 'lyracyst/version'
require 'lyracyst/wordnik'
require 'lyracyst/wordsmith'
require 'xml-fu'

# The Lyracyst module handles base functionality.
Expand Down Expand Up @@ -76,6 +77,7 @@ require 'lyracyst/cli/onelook'
require 'lyracyst/cli/rhymebrain'
require 'lyracyst/cli/urban'
require 'lyracyst/cli/wordnik'
require 'lyracyst/cli/wordsmith'

pre do |global, command, options, args|
# Pre logic here
Expand Down Expand Up @@ -132,12 +134,12 @@ post do |global, command, options, args|
outfile = global[:o]
if File.exist?(outfile) && global[:fo] == true
if $fmt == :json
fo = File.open(outfile, 'w+')
fo = File.new(outfile, 'w+')
fo.print MultiJson.dump($tofile, :pretty => true)
fo.close
puts Rainbow("Word search was written to #{outfile}.").bright
elsif $fmt == :xml
fo = File.open(outfile, 'w+')
fo = File.new(outfile, 'w+')
fo.print '<?xml version="1.0" encoding="utf-8"?>'
fo.print XmlFu.xml($tofile)
fo.close
Expand All @@ -151,12 +153,12 @@ post do |global, command, options, args|
ans = gets
if ans =~ /y/
if $fmt == :json
fo = File.open(outfile, 'w+')
fo = File.new(outfile, 'w+')
fo.print MultiJson.dump($tofile, :pretty => true)
fo.close
puts Rainbow("Word search was written to #{outfile}.").bright
elsif $fmt == :xml
fo = File.open(outfile, 'w+')
fo = File.new(outfile, 'w+')
fo.print '<?xml version="1.0" encoding="utf-8"?>'
fo.print XmlFu.xml($tofile)
fo.close
Expand Down
9 changes: 9 additions & 0 deletions features/anagram.feature
@@ -0,0 +1,9 @@
Feature: Anagram
In order to use my app to get anagrams
As a developer using Spinach
I run the ana command

Scenario: Anagram command runs
When I run `lyracyst ana warrenpicketcalender`
Then the output should contain anagrams
And the exit status should be 0
13 changes: 13 additions & 0 deletions features/steps/anagram_steps.rb
@@ -0,0 +1,13 @@
class Spinach::Features::Anagram < Spinach::FeatureSteps
step 'I run `lyracyst ana warrenpicketcalender`' do
@output = `lyracyst ana warrenpicketcalender`
end

step 'the output should contain anagrams' do
@output =~ /[\[\]A-Za-z0-9. :|]/
end

step 'the exit status should be 0' do
$?.exitstatus == 0
end
end
10 changes: 6 additions & 4 deletions lib/lyracyst.rb
Expand Up @@ -10,6 +10,7 @@
require 'lyracyst/urban'
require 'lyracyst/version'
require 'lyracyst/wordnik'
require 'lyracyst/wordsmith'
require 'xml-fu'

# The Lyracyst module handles base functionality.
Expand Down Expand Up @@ -76,6 +77,7 @@ def self.tofile(obj)
require 'lyracyst/cli/rhymebrain'
require 'lyracyst/cli/urban'
require 'lyracyst/cli/wordnik'
require 'lyracyst/cli/wordsmith'

pre do |global, command, options, args|
# Pre logic here
Expand Down Expand Up @@ -132,12 +134,12 @@ def self.tofile(obj)
outfile = global[:o]
if File.exist?(outfile) && global[:fo] == true
if $fmt == :json
fo = File.open(outfile, 'w+')
fo = File.new(outfile, 'w+')
fo.print MultiJson.dump($tofile, :pretty => true)
fo.close
puts Rainbow("Word search was written to #{outfile}.").bright
elsif $fmt == :xml
fo = File.open(outfile, 'w+')
fo = File.new(outfile, 'w+')
fo.print '<?xml version="1.0" encoding="utf-8"?>'
fo.print XmlFu.xml($tofile)
fo.close
Expand All @@ -151,12 +153,12 @@ def self.tofile(obj)
ans = gets
if ans =~ /y/
if $fmt == :json
fo = File.open(outfile, 'w+')
fo = File.new(outfile, 'w+')
fo.print MultiJson.dump($tofile, :pretty => true)
fo.close
puts Rainbow("Word search was written to #{outfile}.").bright
elsif $fmt == :xml
fo = File.open(outfile, 'w+')
fo = File.new(outfile, 'w+')
fo.print '<?xml version="1.0" encoding="utf-8"?>'
fo.print XmlFu.xml($tofile)
fo.close
Expand Down
37 changes: 37 additions & 0 deletions lib/lyracyst/cli/wordsmith.rb
@@ -0,0 +1,37 @@
desc 'Fetches anagrams from Wordsmith'
arg_name 'word'
command :ana do |c|
c.flag :lang, :default_value => 'english', :arg_name => 'string', :desc => 'english,english-obscure,german,spanish,esperanto,french,italian,latin,dutch,portuguese,swedish,names'
c.flag :limit, :default_value => 10, :arg_name => 'integer', :desc => 'Limits number of results returned'
c.flag :maxwords, :default_value => nil, :arg_name => 'integer', :desc => 'Maximum number of words in each anagram'
c.flag :include, :default_value => nil, :arg_name => 'string', :desc => 'Anagrams must include this word'
c.flag :exclude, :default_value => nil, :arg_name => 'string', :desc => 'Anagrams must exclude these words'
c.flag :minletters, :default_value => nil, :arg_name => 'integer', :desc => 'Minimum number of letters in each word'
c.flag :maxletters, :default_value => nil, :arg_name => 'integer', :desc => 'Maximum number of letters in each word'
c.flag :case, :default_value => 1, :arg_name => 'integer', :desc => '0 - Lowercase, 1 - First Letter, 2 - Uppercase'
c.switch :repeat, :default_value => false, :desc => 'Repeat occurrences of a word OK'
c.switch :list, :default_value => false, :desc => 'Show candidate word list only'
c.switch :linenum, :default_value => false, :desc => 'Show line numbers with anagrams'
c.action do |global_options, options, args|
search = args[0]
if options[:repeat] == true
repeat = 'y'
else
repeat = 'n'
end
if options[:list] == true
list = 'y'
else
list = 'n'
end
if options[:linenum] == true
linenum = 'y'
else
linenum = 'n'
end
params = { lang: options[:lang], limit: options[:limit], maxwords: options[:maxwords], include: options[:include], exclude: options[:exclude], minletters: options[:minletters], maxletters: options[:maxletters], case: options[:case], repeat: repeat, list: list, linenum: linenum }
result = nil
ana = Lyracyst::Wordsmith.new
ana.scrape(search, params, result)
end
end
2 changes: 1 addition & 1 deletion lib/lyracyst/version.rb
@@ -1,5 +1,5 @@
# coding: utf-8
module Lyracyst
# Semantic program version
VERSION = '1.1.0'
VERSION = '1.2.0'
end
46 changes: 46 additions & 0 deletions lib/lyracyst/wordsmith.rb
@@ -0,0 +1,46 @@
# coding: utf-8
require 'metainspector'

module Lyracyst
# Wordsmith.org provides anagrams.
class Wordsmith
# The scraper method.
#
# @param search [String] The string of letters to rearrange.
# @param params [Hash] The search parameters to use.
# @param result [Hash] The search results.
def scrape(search, params, result)
prefix = 'http://wordsmith.org/anagram/anagram.cgi?anagram='
word, pcont = "#{prefix}#{search}", []
params.map { |k, v|
if k == :lang then pcont.push "&language=#{v}"; end # string
if k == :limit then pcont.push "&t=#{v}"; end # integer
if k == :maxwords then pcont.push "&d=#{v}"; end # integer
if k == :include then pcont.push "&include=#{v}"; end # string
if k == :exclude then pcont.push "&exclude=#{v}"; end # string
if k == :minletters then pcont.push "&n=#{v}"; end # integer
if k == :maxletters then pcont.push "&m=#{v}"; end # integer
if k == :repeat then pcont.push "&a=#{v}"; end # y/n
if k == :list then pcont.push "&l=#{v}"; end # y/n
if k == :linenum then pcont.push "&q=#{v}"; end # y/n
if k == :case then pcont.push "&k=#{v}"; end # 0 - lower, 1 - first, 2 - upper
}
pcont.push "&src=adv"
url = "#{word}#{pcont.join}"
result = MetaInspector.new(url)
pdoc = result.parsed
text = pdoc.at_css('p:nth-child(7)').text
s = text.scan(/^\s(\d*) found. Displaying ([a-z0-9 ]*):([a-z\s]*)What's/i)
s = s[0]
found = s[0]
display = s[1]
astring = s[2]
search = { 'search' => search }
anagrams = { 'anagrams' => astring }
Lyracyst.tofile(search)
Lyracyst.tofile(anagrams)
Lyracyst.label('Anagrams')
puts "Found #{found}. Displaying #{display}:\n#{astring}"
end
end
end
1 change: 1 addition & 0 deletions lyracyst.gemspec
Expand Up @@ -17,6 +17,7 @@ Gem::Specification.new do |s|
# If you have other dependencies, add them here
s.add_runtime_dependency "gli", "~> 2.11"
s.add_runtime_dependency "httpi", "~> 2.1"
s.add_runtime_dependency "metainspector", "~> 2.2"
s.add_runtime_dependency "multi_json", "~> 1.10"
s.add_runtime_dependency "multi_xml", "~> 0.5"
s.add_runtime_dependency "rainbow", "~> 2.0"
Expand Down

0 comments on commit e3409ad

Please sign in to comment.