From 8fa2975b5154f0047eaef875b2744f47471ece24 Mon Sep 17 00:00:00 2001 From: Abhishek Parolkar Date: Wed, 1 Apr 2009 15:28:55 +0530 Subject: [PATCH] Om Sree Ganeshaya Namah: The first commit --- MIT-LICENSE | 20 +++++++++++++++++ README | 36 +++++++++++++++++++++++++++++++ Rakefile | 23 ++++++++++++++++++++ init.rb | 6 ++++++ install.rb | 1 + lib/inflectionist.rb | 39 ++++++++++++++++++++++++++++++++++ lib/inflections.rb | 20 +++++++++++++++++ lib/string_ext.rb | 21 ++++++++++++++++++ tasks/inflectionist_tasks.rake | 4 ++++ test/inflectionist_test.rb | 8 +++++++ test/test_helper.rb | 3 +++ uninstall.rb | 1 + 12 files changed, 182 insertions(+) create mode 100644 MIT-LICENSE create mode 100644 README create mode 100644 Rakefile create mode 100644 init.rb create mode 100644 install.rb create mode 100644 lib/inflectionist.rb create mode 100644 lib/inflections.rb create mode 100644 lib/string_ext.rb create mode 100644 tasks/inflectionist_tasks.rake create mode 100644 test/inflectionist_test.rb create mode 100644 test/test_helper.rb create mode 100644 uninstall.rb diff --git a/MIT-LICENSE b/MIT-LICENSE new file mode 100644 index 0000000..9376605 --- /dev/null +++ b/MIT-LICENSE @@ -0,0 +1,20 @@ +Copyright (c) 2009 [name of plugin creator] + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README b/README new file mode 100644 index 0000000..b748304 --- /dev/null +++ b/README @@ -0,0 +1,36 @@ +Inflectionist +============= + +The old school ActiveSupport's inflections allowed you to pluralize, singularize , humanize etc. And when you wanted little more complex linguistic processing , you found nothing. Here comes Inflectionist for your rescue. It is an extension to ActiveSupport inflections available in form of a rails plugin. For now, this will allow you to find simple past tense of any given word. + + + +Example +======= + + "create".past_tensed => "created" + "buy".past_tensed => "baught" + "fight".past_tensed => "faught" + "market".past_tensed => "marketed" + "animate".past_tensed => "animated" + "fluctuate".past_tensed => "fluctuated" + "tag".past_tensed => "tagged" + "feel".past_tensed => "felt" + + +Installation +============ + + ... + + + +Goals & TODO +============ +1.) Implement past to present tense conversion +2.) Implement more complex linguistic processing +3.) Add more inflections + + + +Copyright (c) 2009 Er Abhishek Parolkar, released under the MIT license diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..576f210 --- /dev/null +++ b/Rakefile @@ -0,0 +1,23 @@ +require 'rake' +require 'rake/testtask' +require 'rake/rdoctask' + +desc 'Default: run unit tests.' +task :default => :test + +desc 'Test the inflectionist plugin.' +Rake::TestTask.new(:test) do |t| + t.libs << 'lib' + t.libs << 'test' + t.pattern = 'test/**/*_test.rb' + t.verbose = true +end + +desc 'Generate documentation for the inflectionist plugin.' +Rake::RDocTask.new(:rdoc) do |rdoc| + rdoc.rdoc_dir = 'rdoc' + rdoc.title = 'Inflectionist' + rdoc.options << '--line-numbers' << '--inline-source' + rdoc.rdoc_files.include('README') + rdoc.rdoc_files.include('lib/**/*.rb') +end diff --git a/init.rb b/init.rb new file mode 100644 index 0000000..c3f2fc4 --- /dev/null +++ b/init.rb @@ -0,0 +1,6 @@ +# Author: Er Abhishek Parolkar + +require File.dirname(__FILE__) + '/lib/inflectionist' +require File.dirname(__FILE__) + '/lib/inflections' +require File.dirname(__FILE__) + '/lib/string_ext' +ActiveSupport::Inflector.send(:extend, ParolkarInnovationLab::Inflectionist) diff --git a/install.rb b/install.rb new file mode 100644 index 0000000..f7732d3 --- /dev/null +++ b/install.rb @@ -0,0 +1 @@ +# Install hook code here diff --git a/lib/inflectionist.rb b/lib/inflectionist.rb new file mode 100644 index 0000000..f6281f0 --- /dev/null +++ b/lib/inflectionist.rb @@ -0,0 +1,39 @@ +#Copyright (c) 2009 Er Abhishek Parolkar, released under the MIT license +module ParolkarInnovationLab + module Inflectionist + + def past_tensed(word) + result = word.to_s.dup + + if word.empty? || inflections.uncountables.include?(result.downcase) + result + else + inflections.past_tenses.each { |(rule, replacement)| break if result.gsub!(rule, replacement) } + result + end + end + end +end + + + + ActiveSupport::Inflector::Inflections.instance.instance_eval do # Keep in mind Inflections is a singleton, so what makes more sense is to inject our methods at run time to the instance + + def past_tenses # a attr_reader + @past_tenses + end + def past_tense(rule, replacement) + @past_tenses = [] if !@past_tenses + @uncountables.delete(rule) if rule.is_a?(String) + @uncountables.delete(replacement) + @past_tenses.insert(0, [rule, replacement]) + end + + end + + + + + + + diff --git a/lib/inflections.rb b/lib/inflections.rb new file mode 100644 index 0000000..8230249 --- /dev/null +++ b/lib/inflections.rb @@ -0,0 +1,20 @@ +# this is where we define all the inflections for functionalities added by inflectionist plugin + +ActiveSupport::Inflector.inflections do |inflect| + inflect.past_tense /^(.*)$/,'\1ed' + inflect.past_tense /e$/,'ed' + inflect.past_tense /t$/,'ted' + inflect.past_tense /g$/,'gged' + inflect.past_tense /ight$/,'aught' + inflect.past_tense "buy",'baught' + inflect.past_tense "sell",'sold' + inflect.past_tense "is",'was' + inflect.past_tense "are",'were' + inflect.past_tense "teach",'taught' + inflect.past_tense "feel",'felt' + + + + + +end \ No newline at end of file diff --git a/lib/string_ext.rb b/lib/string_ext.rb new file mode 100644 index 0000000..6ec1b16 --- /dev/null +++ b/lib/string_ext.rb @@ -0,0 +1,21 @@ + + +# in case active_support/inflector is required without the rest of active_support +require 'active_support/inflections' +require 'active_support/core_ext/string/inflections' +require 'active_support/inflector' unless defined?(ActiveSupport::Inflector) + +module ActiveSupport + module CoreExtensions + module String #:nodoc: + module Inflections + def past_tensed + Inflector.past_tensed(self) + end + end + end + end +end + +String.send :include, ActiveSupport::CoreExtensions::String::Inflections # this is required for above changes to take effect + \ No newline at end of file diff --git a/tasks/inflectionist_tasks.rake b/tasks/inflectionist_tasks.rake new file mode 100644 index 0000000..3378320 --- /dev/null +++ b/tasks/inflectionist_tasks.rake @@ -0,0 +1,4 @@ +# desc "Explaining what the task does" +# task :inflectionist do +# # Task goes here +# end diff --git a/test/inflectionist_test.rb b/test/inflectionist_test.rb new file mode 100644 index 0000000..e5f9241 --- /dev/null +++ b/test/inflectionist_test.rb @@ -0,0 +1,8 @@ +require 'test_helper' + +class InflectionistTest < ActiveSupport::TestCase + # Replace this with your real tests. + test "the truth" do + assert true + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb new file mode 100644 index 0000000..cf148b8 --- /dev/null +++ b/test/test_helper.rb @@ -0,0 +1,3 @@ +require 'rubygems' +require 'active_support' +require 'active_support/test_case' \ No newline at end of file diff --git a/uninstall.rb b/uninstall.rb new file mode 100644 index 0000000..9738333 --- /dev/null +++ b/uninstall.rb @@ -0,0 +1 @@ +# Uninstall hook code here