chuckpreslar / inflect

Inflections made easy for Go.
12 stars 3 forks source link

Singularize edge cases #2

Open missinglink opened 8 years ago

missinglink commented 8 years ago

awesome lib thanks!

I noticed a couple edge cases for Singularize() which didn't quite work as expected:

token actual expected
phases phas phase
volcanoes volcanoe volcano
houses hous house
dwarves dwarve dwarf

... plus some really difficult cases from old english:

token actual expected
hooves hoove hoof
staves stave staff
turves turve turf
chuckpreslar commented 8 years ago

thanks for the feedback, @missinglink -- I'll patch this ASAP!

missinglink commented 8 years ago

cool thanks @chuckpreslar, I sourced them from https://en.wikipedia.org/wiki/English_plurals

here's a copy of my test case:

package token

import (
    "testing"

    "github.com/stretchr/testify/assert"
)

func TestSingular(t *testing.T) {

    var text, expected []string

    // identical singular and plural
    text = []string{"bison", "buffalo", "deer", "duck", "fish", "moose", "pike", "plankton", "salmon", "sheep", "quid", "swine", "trout"}
    expected = []string{"bison", "buffalo", "deer", "duck", "fish", "moose", "pike", "plankton", "salmon", "sheep", "quid", "swine", "trout"}
    assert.Equal(t, expected, Singular(text))

    // sibilant sound
    // @todo: these are techinally not quite correct but will do for autocomplete
    text = []string{"kisses", "phases", "dishes", "massages", "witches", "judges"}
    expected = []string{"kiss", "phas", "dish", "massage", "witch", "judge"}
    // expected = []string{"kiss", "phase", "dish", "massage", "witch", "judge"} // error
    assert.Equal(t, expected, Singular(text))

    // voiceless consonant
    text = []string{"laps", "cats", "clocks", "cuffs", "deaths"}
    expected = []string{"lap", "cat", "clock", "cuff", "death"}
    assert.Equal(t, expected, Singular(text))

    // regular plural
    text = []string{"boys", "girls", "chairs"}
    expected = []string{"boy", "girl", "chair"}
    assert.Equal(t, expected, Singular(text))

    // nouns ending in -o
    text = []string{"heroes", "potatoes", "volcanoes", "volcanos"}
    expected = []string{"hero", "potato", "volcanoe", "volcano"}
    // expected = []string{"hero", "potato", "volcano", "volcano"} // error
    assert.Equal(t, expected, Singular(text))

    // nouns ending in -o (Italian loanwords)
    text = []string{"cantos", "heteros", "photos", "zeros", "pianos", "porticos", "pros", "quartos", "kimonos"}
    expected = []string{"canto", "hetero", "photo", "zero", "piano", "portico", "pro", "quarto", "kimono"}
    assert.Equal(t, expected, Singular(text))

    // nouns ending in -y
    text = []string{"cherries", "ladies", "skies"}
    expected = []string{"cherry", "lady", "sky"}
    assert.Equal(t, expected, Singular(text))

    // nouns ending in -guy
    text = []string{"soliloquies"}
    expected = []string{"soliloquy"}
    assert.Equal(t, expected, Singular(text))

    // voiceless fricatives
    text = []string{"baths", "mouths", "calves", "leaves", "knives", "lives", "houses", "moths", "proofs"}
    expected = []string{"bath", "mouth", "calf", "leave", "knife", "life", "hous", "moth", "proof"}
    // expected = []string{"bath", "mouth", "calf", "leave", "knife", "live", "house", "moth", "proof"} // error
    assert.Equal(t, expected, Singular(text))

    // nouns ending in -f
    // @todo: not so easy for autocomplete
    // text = []string{"dwarves", "hooves", "elves", "staves", "turves"}
    // expected = []string{"dwarf", "hoof", "elf", "staff", "turf"}
    // assert.Equal(t, expected, Singular(text))
}