2

我有一串单词,其中包含一些单词

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

还有一些像

"Access"

它们是单数的,但"s"最后有。我想删除复数词,但我无法编写代码从末尾删除 s,因为它将"s"从所有单词中删除。我在互联网上搜索,找不到任何相关的东西。谁能帮助我以最好的方式做到这一点?

编辑:如何在我的程序中使用 CakePHP?我以前从未使用过它,所以没有经验。我在函数中有那个字符串变量。它说我需要安装 Cake PHP,谁能告诉我如何在函数中使用它?

谢谢

4

2 回答 2

6

使用 CakePHP 附带的 Inflector 类。

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

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

静态变形器::singularize($plural)

输入: 苹果、橙子、人、男人

输出: 苹果、橙子、人、人

老实说,我不知道它在内部是如何工作的,但它在英语单词方面做得很好,你甚至可以配置它来处理特殊情况。

您可以通过使用 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'));
于 2014-01-17T12:28:49.770 回答
3

在英语中没有简单的方法:考虑Clouds=> CloudFortresses=> FortressLadies=> LadySheep=> Sheep...并且简单地剥离s可能会导致问题(例如Lady's)...您可能要考虑使用Porter Stemmer或相似的

于 2014-01-17T12:11:30.533 回答