Giter VIP home page Giter VIP logo

inflex's Introduction

Inflex

Build Status Module Version Hex Docs Total Download License Last Updated

An Elixir library for handling word inflections.

Getting Started

You can add Inflex as a dependency in your mix.exs file. Since it only requires Elixir and Erlang there are no other dependencies.

def deps do
  [
    {:inflex, "~> 2.0.0"}
  ]
end

If you are not using hex you can add the dependency using the GitHub repository.

def deps do
  [
    {:inflex, github: "nurugger07/inflex"}
  ]
end

Then run mix deps.get in the shell to fetch and compile the dependencies.

Requirements

Although Inflex supports Elixir 1.6, which is compatible with Erlang/OTP 19–20, Inflex requires Erlang/OTP 20+.

To incorporate Inflex in your modules, use import.

defmodule Blog do
  import Inflex

  def greeting(count), do: "You are the #{ordinalize(count)} visitor!"

end

Examples

Singularize & Pluralize

Here are some basic examples from iex:

iex(1)> Inflex.singularize("dogs")
"dog"

iex(2)> Inflex.pluralize("dog")
"dogs"

iex(3)> Inflex.singularize("people")
"person"

iex(4)> Inflex.pluralize("person")
"people"

Some other special cases are handled for nouns ending in -o and -y

iex(1)> Inflex.pluralize("piano")
"pianos"

iex(2)> Inflex.pluralize("hero")
"heroes"

iex(3)> Inflex.pluralize("butterfly")
"butterflies"

iex(4)> Inflex.pluralize("monkey")
"monkeys"

Inflect

iex(1)> Inflex.inflect("child", 1)
"child"

iex(2)> Inflex.inflect("child", 2)
"children"

Camelize & Pascalize

Inflex also camelizes or pascalizes strings and atoms.

iex(1)> Inflex.camelize(:upper_camel_case)
"UpperCamelCase"

iex(2)> Inflex.camelize("pascal-case", :lower)
"pascalCase"

Parameterize

Strings can be parameterized easily.

iex(1)> Inflex.parameterize("String for parameter")
"string-for-parameter"

iex(2)> Inflex.parameterize("String with underscore", "_")
"string_with_underscore"

Underscore

Makes an underscored, lowercase form from a string or atom.

iex(1)> Inflex.underscore("UpperCamelCase")
"upper_camel_case"

iex(2)> Inflex.underscore("pascalCase")
"pascal_case"

iex(3)> Inflex.underscore(UpperCamelCase)
"upper_camel_case"

iex(4)> Inflex.underscore(:pascalCase)
"pascal_case"

Contributing

All pull requests will be reviewed for inclusion but must include tests.

License

Copyright (c) 2013 Johnny Winn

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

inflex's People

Contributors

adamkittelson avatar adrianomitre avatar alpacino avatar annoraaq avatar ayarulin avatar bobjflong avatar dmvt avatar flowerett avatar hashrocketeer avatar iamvery avatar jbranchaud avatar jwarwick avatar kianmeng avatar lau avatar lowks avatar mathieuprog avatar moxley avatar nurugger07 avatar pedrogarrett avatar pjhampton avatar pma avatar prodivnet avatar rud avatar smpallen99 avatar sngeth avatar sobolevn avatar styd avatar tfiedlerdejanze avatar tokafish avatar xtian avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

inflex's Issues

:lower option broken in Elixir 1.6

A critical incompatibility with Elixir 1.6 is addressed by an open pull request. Without this change, Inflex.camelize(:lower) remains broken in Elixir 1.6.

Convert string to camelize

Convert string to UpperCamelCase or have a :lower flag to convert string to lowerCamelCase.

"UpperCamelCase" = Inflex.camelize("upper_camel_case")

"lowerCamelCase" = Inflex.camelize("lower_camel_case", :lower)

Release tags

Hi,

Could you please add a tag for 1.4.0 to the repo? It would simplify building packages a bit :)

inflex defines functions in my code

inflex is promoting the anti-pattern of defining functions in my code with use. In Elixir, we usually avoid those unless the module is implementing a well defined behaviour (think GenServer). We have also documented this anti-pattern: https://github.com/elixir-lang/elixir/blob/master/lib/elixir/lib/kernel.ex#L3559-L3600

I would just move all the functions to the Inflex module which would then be imported as:

import Inflex

This is the simplest way because, if I don't want camelize, I can do:

import Inflex, except: [camelize: 1]

Most Elixir developers know how to use import well. The current inflex implementation puts aside import into its own scheme which generates code into my own module and relies on function indirection with __using__. To drive the point home, imagine every time I wanted to use Enum, it defined all Enum functions in my modules.

I don't believe there is a need for a single __using__ callback in Inflex. They are just functions.

Version number on github does not match hex.pm

Hi, thanks for this library.

I received a dependabot automated PR to bump inflex to version 2.1.0, but when I looked at the changes here I don't see a version bump to 2.1.0.

In fact, I see several changes in hexdiff that aren't reflected here. This doesn't look like a security issue, but I'm curious what's happened.

Suggestion for adding other languages support

Hi,

I'm interested to contribute in order to offer some sort of localization of the inflexions.
Before going to add a particular language.
I wanted to discuss for the "architecture" of handling the configuration of the locale setting.

I see three ways to do it internally with both a global default and an option to do it per function.
But in any case, this needs to have all the inflexions mechanisms (I mean all the string manipulation and matching functions) separated from all the language related rules and data.

  1. Using the Elixir Config module and setting a locale config value (eg. "en", "fr", etc.)
  2. Using an optional key-value parameter (eg. locale: "fr") in each function
  3. Using a dedicated function that can be invoked at any time to set the locale from that moment on (not fan of that, but it's a possibility)

Another option could be to handle the localization setting itself outside with for example ex_cldr. This needs an extra dependency, but it's possible to add it for those already using a package already using it (like ex_money).
But in this case all the language related rules and data should still be done internally.

Again it's necessary in any case to separate the string manipulation mechanisms with the language data rules themselves.
In this situation, it can be even possible to separate all the data as a separate package (or not).

What do you think?

Inflex.camelize(string, :lower) appears to upper-case instead

Inflex 1.8.1, Elixir 1.6.1

iex(5)> Inflex.camelize("pascal-case", :lower)
"PascalCase"

Should be:

"pascalCase"

I think the problem is the Regex in camelize.ex

iex(6)> Regex.split(~r/(?:^|[-_])|(?=[A-Z])/, to_string("two_words"))
["", "two", "words"]

I suspect the initial empty string is breaking the camelize_list function. This seems to be a change between Elixir 1.5.3 and Elixir 1.6.1 (the two versions I can easily swap between):

❯ asdf local elixir 1.5.3

~/test/test
❯ iex -S mix
Erlang/OTP 20 [erts-9.2] [source] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:10] [hipe] [kernel-poll:false]

iex(1)> Inflex.camelize("pascal-case", :lower)
"pascalCase"
iex(2)> Regex.split(~r/(?:^|[-_])|(?=[A-Z])/, to_string("two_words"))
["two", "words"]


^C
~/test/test
❯ asdf local elixir 1.6.0-otp-20

~/test/test
❯ iex -S mix
Erlang/OTP 20 [erts-9.2] [source] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:10] [hipe] [kernel-poll:false]

iex(1)> Inflex.camelize("pascal-case", :lower)
"PascalCase"
iex(2)> Regex.split(~r/(?:^|[-_])|(?=[A-Z])/, to_string("two_words"))
["", "two", "words"]
^C

Plural of canvas

The plural of canvas is canvases, but Inflex.plurarlize returns canvas (and it's likely Inflex.singularize will return canva I haven't verified it yet)

Ordinalizer

I forked this project mid Dec '15 and turned it into full stack Text Mining / Natural Language Processing library for the Elixir Programming language which is reaching a good place (i think). I built an ordinalizer into it because it simply didn't exist in inflex.

I am going to make the repo public and push to hex in the coming months but I have decided to now remove the inflector completely in favor of this library. Would you consider / be interested if I submitted a PR to incorporate an ordinalizer into inflex?

ordinalize.ex

defmodule Inflector.Ordinalize do
  @moduledoc false

  def ordinalize(number) when is_number(number) do
    cond do
      number in [11, 12, 13, 14] ->
        Integer.to_string(number) <> "th"
      rem(number, 10) == 1 ->
        Integer.to_string(number) <> "st"
      rem(number, 10) == 2 ->
        Integer.to_string(number) <> "nd"
      rem(number, 10) == 3 ->
        Integer.to_string(number) <> "rd"
      true ->
        Integer.to_string(number) <> "th"
    end
  end
end

Parameterize : accents, commas and trailing space remain

iex(3)> Inflex.parameterize("Secrétaire, sténo dactylographe ")
"secrétaire,-sténo-dactylographe-"

while the expected is:

iex(3)> Inflex.parameterize("Secrétaire, sténo dactylographe ")
"secretaire-steno-dactylographe"

at least if the objective is to mimic rails' parameterize's behavior

Inflex is not loaded

I'm seeing this when pointing at Hex

error: module Inflex is not loaded and could not be found

But not when ref'ing this github repo.

Wrong pluralization when there is "people" in the middle of the word

The pluralization of the word personal is wrong:

iex(10)> Inflex.pluralize "personal"
"peopleal"

Here, I expected the output to be personals.

In my use case, I wanted to translate personal trainer, but it finds people in the middle and pluralizes that.

iex(11)> Inflex.pluralize "personal_trainer"
"peopleal_trainer"

I expected the output to be personal_trainers.

The same happens when the words are separated by a space:

iex(6)> Inflex.pluralize "personal trainer"
"peopleal trainer"

Add parameterize

This is something that is common and maybe best added here.

iex(1)> Inflex.parameterize("First Name")
"first-name"

iex(2)> Inflex.parameterize("First Name", "_")
"first_name"

Pluralize changes case of irregular words

I would assume that the pluralized word would start with an uppercase letter if the singular word does.

iex(1)> Inflex.pluralize "Dog"
"Dogs"
iex(2)> Inflex.pluralize "Person"
"people"

Pluralize and Singularize mistakes for nouns with endings of `f` or `fe`.

Pluralize:

iex> Inflex.pluralize("life")
"liives" # should be "lives"
iex> Inflex.pluralize("elf")
"eves" # should be "elves"
iex> Inflex.pluralize("wolf")
"woves" # should be "wolves"
iex> Inflex.pluralize("hoof")
"hoofs" # should be "hooves"
iex> Inflex.pluralize("knife")
"kniives" # should be "knives"

Singularize:

iex> Inflex.singularize("elves")
"elfe" # should be "elf"
iex> Inflex.singularize("wolves")
"wolfe" # should be "wolf"
iex> Inflex.singularize("hooves")
"hoofe" # should be "hoof"

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.