GithubHelp home page GithubHelp logo

tarsbase / grammaticalnumber Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cosmo/grammaticalnumber

0.0 1.0 0.0 14 KB

1️⃣🔜🔢 Turns singular words to the plural and vice-versa in Swift.

License: MIT License

Swift 100.00%

grammaticalnumber's Introduction

GrammaticalNumber

Turning singular words to plural can be very hard in some spoken languages, while other languages have simple rules.

GrammaticalNumber is heavily inspired by ActiveSupport::Inflector known from the Ruby on Rails web framework.

Usage

Turn singular words to plural

"person".pluralized()               // people
"center".pluralized()               // centers
"sheep".pluralized()                // sheep
"knife".pluralized()                // knives
"mouse".pluralized()                // mice
"money".pluralized()                // money
"axis".pluralized()                 // axes
"item".pluralized()                 // items
"status".pluralized()               // statuses
"fox".pluralized()                  // foxes
"move".pluralized()                 // moves
"tooth".pluralized()                // teeth
"foxes".pluralized()                // foxes
"millennium".pluralized()           // millennia
"child".pluralized()                // children
"matrix".pluralized()               // matrices
"man".pluralized()                  // men
"ox".pluralized()                   // oxen
"radius".pluralized()               // radii
"grandchild".pluralized()           // grandchildren

Turn plural words to singular

"children".singularized()           // child
"tables".singularized()             // table
"computers".singularized()          // computer
"mice".singularized()               // mouse
"teeth".singularized()              // tooth
"axes".singularized()               // axis
"women".singularized()              // woman
"grandchildren".singularized()      // grandchild

Case Sensitivity

GrammaticalNumber will try to match the letter casing of your input word. Lowercased, uppercased and capitalized words are supported.

"tooth".pluralized()                // teeth
"TOOTH".pluralized()                // TOOTH
"Tooth".pluralized()                // Teeth

Add count to words

Prepends the pluralized String with count. If the count is 0, the singular word will be used.

"child".pluralized(count: 0)        // 0 children
"child".pluralized(count: 1)        // 1 child
"child".pluralized(count: 3)        // 3 children

"knife".pluralized(count: 0)        // 0 knives
"knife".pluralized(count: 1)        // 1 knife
"knife".pluralized(count: 3)        // 3 knives

"sheep".pluralized(count: 0)        // 0 sheep
"sheep".pluralized(count: 1)        // 1 sheep
"sheep".pluralized(count: 3)        // 3 sheep

Define Custom Rules

Uncountable Rule

let rule: GrammaticalNumberRule = .uncountable("money")

money will never change.

Irregular Rule: Singular from plural

let rule: GrammaticalNumberRule = .irregular("tooth", "teeth")

Turns tooth to teeth when used with pluralized(). Turns teeth to tooth when used with singularized().

Plural Rule: Plural from singular with regular expression

let rule: GrammaticalNumberRule = .plural(#"^(m|l)ouse$"#, #"$1ice"#)

mouse becomes mice and louse becomes lice.

Singular Rule: Singular from plural with regular expression

let rule: GrammaticalNumberRule = .singular(#"(matr)ices$"#, #"$1ix"#)

Turns matrices to matrix.

Apply rule, so it becomes available

GrammaticalNumberRule.add(rule)

Support other languages

In order to support other languages, pass the language parameter to your custom rules. Call .pluralized(language: yourLanguage) with the same language value — like so: .pluralized(language: "tr")

Example for the turkish language (tr)

GrammaticalNumberRule.add(.plural(#"([aoıu][^aoıueöiü]{0,6})$"#, #"$1lar"#), language: "tr")
GrammaticalNumberRule.add(.plural(#"([eöiü][^aoıueöiü]{0,6})$"#, #"$1ler"#), language: "tr")
GrammaticalNumberRule.add(.singular(#"l[ae]r$"#, #""#), language: "tr")
"kitap".pluralized(language: "tr")              // kitaplar
"yemek".pluralized(language: "tr")              // yemekler

Contact

License

GrammaticalNumber is released under the MIT License.

grammaticalnumber's People

Contributors

cosmo avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.