2

I have a string of words which has some words like

"Cloud", "Clouds", "Application", "Applications", "Event", "Events"

and some words like

"Access"

which are singular but have "s" at the end. I want to remove the plural words but I can't write a code to remove s from the end as it will remove "s" from all the words. I searched on Internet and couldn't find anything relevant. Can anyone help me with best way of doing this?

EDIT: How do I use CakePHP in my program? I have never used it before so don't have experience on it. I have that String variable in a function. It says I need to install Cake PHP, Can anyone tell me how to use it in the function?

Thanks

user2798227
  • 853
  • 1
  • 16
  • 31
  • You have to use a dictionnary. – Toto Jan 17 '14 at 12:06
  • Use Cakes inflector class: Inflector::singularize('People'); // will show person – floriank Jan 17 '14 at 12:32
  • @YUNOWORK WOrse, you need AI for this. A word like 'lives' can be the plural of a noun, or a conjugated verb. You need to analyze the text to determine which one it is, which goes into AI territory. – SQB Jan 17 '14 at 13:21
  • 1
    Why in gods name did you put the CakePHP tag in your question if you've never used it!? – floriank Jan 20 '14 at 11:05
  • Sorry but I put the CakePHP tag because I had found out that it can be done using CakePHP, I was thinking if someone can help me with using CakePHP. – user2798227 Jan 20 '14 at 11:48

2 Answers2

6

Use the Inflector class that comes with CakePHP.

debug(Inflector::singularize('People')); // will show person

http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::singularize

static Inflector::singularize($plural)

Input: Apples, Oranges, People, Men

Output: Apple, Orange, Person, Man

Honestly I don't know how exactly it is working internally but its doing a pretty good job with english words and you can even configure it to deal with special cases.

You can configure exceptions to the rules by defining them using Inflector::rules():

Inflector::rules('plural', array('/^(inflect)or$/i' => '\1ables'));
Inflector::rules('plural', array(
    'rules' => array('/^(inflect)ors$/i' => '\1ables'),
    'uninflected' => array('dontinflectme'),
    'irregular' => array('red' => 'redlings')
));
Inflector::rules('transliteration', array('/å/' => 'aa'));
floriank
  • 25,546
  • 9
  • 42
  • 66
  • 1
    It has a whole series of generic rules and exceptions built into the code as regexps - https://github.com/cakephp/cakephp/blob/master/lib/Cake/Utility/Inflector.php – Mark Baker Jan 17 '14 at 12:41
  • How do I use it in my program? It gives error when I use just that line. Suppose if you have a blank document and you want to singularize the word "Apples", then what code would you use for that? – user2798227 Jan 17 '14 at 13:45
  • Hi burzum, This is the error: PHP Parse error: syntax error, unexpected T_STRING, expecting T_PAAMAYIM_NEKUDOTAYIM in /path/to/myfile.php on line 75 – user2798227 Jan 20 '14 at 09:17
3

There's no easy way of doing this in English: consider Clouds => Cloud, Fortresses => Fortress, Ladies => Lady, Sheep => Sheep... and simply stripping an s off could lead to problems (e.g. Lady's)... you might want to consider using a Porter Stemmer or similar

Mark Baker
  • 209,507
  • 32
  • 346
  • 385