Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Convert between singular and plural forms of English nouns

License

NotificationsYou must be signed in to change notification settings

piotrmurach/strings-inflection

Repository files navigation

strings logo

Strings::Inflection

Gem VersionActions CIBuild statusMaintainabilityCoverage Status

Inflects English nouns and verbs.

Strings::Inflection provides English inflections of nouns and verbs component forStrings.

Motivation

The goal is to provide a comprehensive way to inflect most nouns and verbs in English. The algorithms that this gem uses are based on the analysis of 7,000 most frequently used nouns and 6,000 most used verbs in English language. Because of this you will get correct inflections for most words:

Strings::Inflection.pluralize("cod")# => "cod"Strings::Inflection.pluralize("codex")# => "codices"Strings::Inflection.pluralize("criterion")# => "criteria"Strings::Inflection.pluralize("vertebra")# => "vertebrae"

Installation

Add this line to your application's Gemfile:

gem'strings-inflection'

And then execute:

$ bundle

Or install it yourself as:

$ gem install strings-inflection

Contents

1. Usage

Strings::Inflection provides a genericinflect method for transforming noun or verb inflections. In the most common case, it assumes that you wish to transform a noun to another form based on count:

Strings::Inflection.inflect("error",3)# => "errors"

As a shortcut, when you wish to always convert a word to singular form usesingularize orpluralize for the opposite:

Strings::Inflection.singularize("errors")# => "error"Strings::Inflection.pluralize("error")# => "errors"Strings::Inflection.singularize("try",term::verb)# => "tries"Strings::Inflection.pluralize("tries",term::verb)# => "try"

Alternatively, you can convert words into a noun or verb object. This way you gain access tosingularandplural methods:

Strings::Inflection::Noun("errors").singular# => "error"Strings::Inflection::Noun("error").plural# => "errors"Strings::Inflection::Verb("try").singular# => "tries"Strings::Inflection::Verb("tries").plural# => "try"

Theinflect method also accepts a mustache-like template to inflect more complex phrases and sentences:

Strings::Inflection.inflect("{{#:count}} {{N:error}} {{V:was}} found",3)# => "3 errors were found"

To change any inflection rules, you can change them usingconfigure. By default the rules only apply to nouns.

Strings::Inflection.configuredo |config|config.plural"index","indexes"config.singular"axes","ax"end

Then the inflection will behave like this:

Strings::Inflection.pluralize("index")# => "indexes"Strings::Inflection.singularize("axes")# => "ax"

2. API

2.1 inflect

In the most common case, to change a noun's inflection useinflect method with the word and a count. By defaultinflect assumes a noun.

For example, to inflect the nounerror to its plural form do:

Strings::Inflection.inflect("error",2)# => "errors"

And to inflect a verb, use the:term option:

Strings::Inflection.inflect("tries",2,term::verb)# => "try"

For more complex cases when you want to inflect parts of a sentence, theinflect provides tags in a template.

For example, you can inflect a noun and a verb to display information based on the count:

Strings::Inflection.inflect("{{#:count}} {{N:error}} {{V:was}} found",2)# => "2 errors were found"

2.1.1 template

The template inflects any content inside mustache-like braces{{...}}. The general form of content inside tag is{{Xmod:word}} where:

  • X informs a grammatical function to apply to word out ofN orV and#.
  • mod apply zero or more modifiers to word transformation.
  • word represents the string to inflect.

The available tags are:

{{#: count }}

The first type of tag is the count tag. By default, this tag will display the count inside the evaluated string.

String::Inflection.inflect("{{#:count}} found",2)# => "2 found"

There is anf option that will provide a fuzzy estimation of the count:

String::Inflection.inflect("{{#f:count}}",0)# => "no"String::Inflection.inflect("{{#f:count}}",1)# => "one"String::Inflection.inflect("{{#f:count}}",2)# => "a couple of"String::Inflection.inflect("{{#f:count}}",3)# => "a few"String::Inflection.inflect("{{#f:count}}",6)# => "several"String::Inflection.inflect("{{#f:count}}",12)# => "many"

{{N: word }}

This tag inflects a noun into a singular or plural form based on the provided count.

Strings::Inflection.inflect("{{#:count}} {{N:error}} found",3)# => "3 errors found"

You can supplys orp options to always force a noun to be singular or plural form.

Strings::Inflection.inflect("{{#:count}} {{Ns:error}} found",3)# => "3 error found"

{{V: word }}

This tag inflects a verb into appropriate form based on the provided count.

Strings::Inflection.inflect("There {{V:were}} {{#:count}} {{N:match}} found",1)# => "There was 1 match found"

2.2 singularize

You can transform a noun or a verb into singular form withsingularize method. By default it assumes a noun but you can change this with:term option:

Strings::Inflection.singularize("errors")# => "error"Strings::Inflection.singularize("indices")# => "index"Strings::Inflection.singularize("index",term::verb)# => "indexes"Strings::Inflection.singularize("try",term::verb)# => "tries"

It will handle inflecting irregular nouns or verbs as well:

Strings::Inflection.singularize("feet")# => "foot"Strings::Inflection.singularize("are",term::verb)# => "is"Strings::Inflection.singularize("go",term::verb)# => "goes"

This method won't change inflection if it already is in the correct form:

Strings::Inflection.singularize("index")# => "index"Strings::Inflection.singularize("sees")# => "sees"

2.3 singular?

To check if a noun or a verb is in a singular form usesingular?:

Strings::Inflection.singular?("errors")# => falseStrings::Inflection.singular?("index")# => trueStrings::Inflection.singular?("try",term::verb)# => falseStrings::Inflection.singular?("goes",term::verb)# => true

You can also convert a word to a noun or verb object:

Strings::Inflection::Noun("errors").singular?# => falseStrings::Inflection::Noun("index").singular?# => trueStrings::Inflection::Verb("try").singular?# => falseStrings::Inflection::Verb("goes").singular?# => true

2.4 pluralize

You can transform a noun or a verb into plural form withpluralize method. By default it assumes a noun but you can change this with:term option:

Strings::Inflection.pluralize("error")# => "errors"Strings::Inflection.pluralize("index")# => "indices"Strings::Inflection.pluralize("indexes",term::verb)# => "index"Strings::Inflection.pluralize("tries",term::verb)# => "try"

It will handle inflecting irregular nouns or verbs as well:

Strings::Inflection.pluralize("foot")# => "feet"Strings::Inflection.pluralize("is",term::verb)# => "are"Strings::Inflection.pluralize("goes",term::verb)# => "go"

This method won't change inflection if it already is in the correct form:

Strings::Inflection.pluralize("indices")# => "indices"Strings::Inflection.pluralize("go")# => "go"

2.5 plural?

To check if a noun or a verb is in a plural form useplural?:

Strings::Inflection.plural?("errors")# => trueStrings::Inflection.plural?("index")# => falseStrings::Inflection.plural?("try",term::verb)# => trueStrings::Inflection.plural?("goes",term::verb)# => false

You can also convert a word to a noun or verb object:

Strings::Inflection::Noun("errors").plural?# => trueStrings::Inflection::Noun("index").plural?# => falseStrings::Inflection::Verb("try").plural?# => trueStrings::Inflection::Verb("goes").plural?# => false

2.6 join_words

To join an array of words into a single sentence usejoin_words method.

For example, to join three words:

Strings::Inflection.join_words("one","two","three")# => "one, two, and three"

To join words without Oxford style comma use:final_separator:

Strings::Inflection.join_words("one","two","three",final_separator:"")# => "one, two and three"

To join words with custom separators:

options={separator:" or ",final_separator:" or at least ",conjunctive:""}Strings::Inflection.join_words("one","two","three", **options)# => "one or two or at least three"

You can also use noun objects to join words. We could do the above:

N=Strings::Inflection::Noun(N("one") +"two" +"three").join_words# => "one, two, and three"

2.7 configure

To change any inflection rules useconfigure with a block. By default the rules only apply to nouns.

Inside the block, the configuration exposes few methods:

  • plural - add plural form inflection rule
  • singular - add singular form inflection rule
  • rule - add singular and plural form inflection rule
  • uncountable - add uncountable nouns

For example, to add new plural and singular rules for theindex andax nouns do:

Strings::Inflection.configuredo |config|config.plural"index","indexes"config.singular"axes","ax"end

To add a rule for both singular and plural inflections do:

Strings::Inflection.configuredo |config|config.rule"ax","axes"end

To add an uncountable noun do:

Strings::Inflection.configuredo |config|config.uncountable"east","earnings"end

Now, no inflection will be applied:

Strings::Inflection.inflect("earnings",1)# => "earnings"Strings::Inflection.inflect("east",2)# => "east"

3. Extending String class

Though it is highly discouraged to pollute core Ruby classes, you can add the required methods toString class by using refinements.

For example, if you wish to only extend strings withinflect method do:

moduleMyStringExtrefineStringdodefinflect(*args, **options)Strings::Inflection.inflect(self, *args, **options)endendend

Theninflect method will be available for any strings where refinement is applied:

usingMyStringExt"error".inflect(2)# => "errors""are".inflect(1,term::verb)# => "is"

However, if you want to include all theStrings::Inflection methods, you can use provided extensions file:

require"strings/inflection/extensions"usingStrings::Inflection::Extensions

Development

After checking out the repo, runbin/setup to install dependencies. Then, runrake spec to run the tests. You can also runbin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, runbundle exec rake install. To release a new version, update the version number inversion.rb, and then runbundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the.gem file torubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub athttps://github.com/piotrmurach/strings-inflect. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to theContributor Covenant code of conduct.

License

The gem is available as open source under the terms of theMIT License.

Code of Conduct

Everyone interacting in the Strings::Inflection project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow thecode of conduct.

Copyright

Copyright (c) 2019 Piotr Murach. See LICENSE for further details.

About

Convert between singular and plural forms of English nouns

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Contributors2

  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp