Skip to content

Commit

Permalink
add custom dictionary support, ignore specific error types, update ex…
Browse files Browse the repository at this point in the history
…amples.
  • Loading branch information
msepcot committed Feb 22, 2010
1 parent d5b1fdc commit 13c36e7
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
24 changes: 22 additions & 2 deletions README
Expand Up @@ -8,8 +8,8 @@ See http://www.afterthedeadline.com/api.slp for the API documentation.
Examples Examples
======== ========


require 'after_the_deadline' require 'after_the_deadline'
AfterTheDeadline('xxx') # this is the same as: AfterTheDeadline.set_api_key('xxx') AfterTheDeadline('xxx', nil, nil) # set the API Key, no custom dictionary, accept all error types


# No Errors # No Errors
AfterTheDeadline.check 'this text is clean.' AfterTheDeadline.check 'this text is clean.'
Expand All @@ -30,3 +30,23 @@ errors.first.info
# Metrics # Metrics
AfterTheDeadline.metrics 'this text should be written in a passive voice. another sentence is used to get more data in the metrics.' AfterTheDeadline.metrics 'this text should be written in a passive voice. another sentence is used to get more data in the metrics.'
=> #<AfterTheDeadline::Metrics:0x10159d4e8 @stats={"words"=>"20", "sentences"=>"1"}, @grammer={}, @spell={}, @style={"passive voice"=>"2"}> => #<AfterTheDeadline::Metrics:0x10159d4e8 @stats={"words"=>"20", "sentences"=>"1"}, @grammer={}, @spell={}, @style={"passive voice"=>"2"}>


Ignoring Specific Types of Errors
=================================

require 'after_the_deadline'
AfterTheDeadline('xxx', nil, ['Passive voice'])

# Skip the Passive Voice Error
errors = AfterTheDeadline.check 'this text should be written in a passive voice.'
=> []


Using a Custom Dictionary
=========================

require 'after_the_deadline'
AfterTheDeadline('xxx', ['Sepcot']) # or AfterTheDeadline('xxx', 'path/to/filename')
AfterTheDeadline.check "My last name, Sepcot, is very unique."
=> []
33 changes: 29 additions & 4 deletions after_the_deadline.rb
Expand Up @@ -2,20 +2,38 @@
require 'net/http' require 'net/http'
require 'uri' require 'uri'


def AfterTheDeadline(key) def AfterTheDeadline(key, dictionary = nil, types = AfterTheDeadline::DEFAULT_IGNORE_TYPES)
AfterTheDeadline.set_api_key(key) AfterTheDeadline.set_api_key(key)
AfterTheDeadline.set_custom_dictionary(dictionary)
AfterTheDeadline.set_ignore_types(types)
nil
end end


class AfterTheDeadline class AfterTheDeadline
@@api_key = nil @@api_key = nil
@@custom_dictionary = []
@@ignore_types = []


BASE_URI = 'http://service.afterthedeadline.com' BASE_URI = 'http://service.afterthedeadline.com'
DEFAULT_IGNORE_TYPES = ['Bias Language', 'Cliches', 'Complex Expression', 'Diacritical Marks', 'Double Negatives', 'Hidden Verbs', 'Jargon Language', 'Passive voice', 'Phrases to Avoid', 'Redundant Expression']


class <<self class <<self
def set_api_key(key) def set_api_key(key)
@@api_key = key @@api_key = key
end end


def set_custom_dictionary(dict)
if dict.kind_of?(Array)
@@custom_dictionary = dict
elsif dict.kind_of?(String)
File.open(dict) { |f| @@custom_dictionary = f.readlines.map &:strip }
end
end

def set_ignore_types(types)
@@ignore_types = types if types.kind_of?(Array)
end

# Invoke checkDocument service with provided text and optional key. # Invoke checkDocument service with provided text and optional key.
# If no key is provided, a default key is used. # If no key is provided, a default key is used.
# #
Expand All @@ -25,11 +43,18 @@ def check(data, key = nil)
return [] if results.nil? # we have no errors in our data return [] if results.nil? # we have no errors in our data


raise "Server returned an error: #{results['message']}" if results['message'] raise "Server returned an error: #{results['message']}" if results['message']
if results['error'].kind_of?(Array) errors = if results['error'].kind_of?(Array)
return results['error'].map { |e| AfterTheDeadline::Error.new(e) } results['error'].map { |e| AfterTheDeadline::Error.new(e) }
else else
return [AfterTheDeadline::Error.new(results['error'])] [AfterTheDeadline::Error.new(results['error'])]
end end

# Remove any error types we don't care about
errors.reject! { |e| @@ignore_types.include?(e.description) }

# Remove spelling errors from our custom dictionary
errors.reject! { |e| e.type == 'spelling' && @@custom_dictionary.include?(e.string) }
return errors
end end
alias :check_document :check alias :check_document :check


Expand Down

0 comments on commit 13c36e7

Please sign in to comment.