Rails 的單複數轉換怎麼實做的呢?
很多人看到 Ruby on Rails 的單複數轉換,就覺得很神奇,怎麼可能會有那麼聰明的機制可以作到。別以為 Ruby on Rails 那麼神,還內建英漢字典 XD 根據這篇的說法,其實這只是一個小技巧,作法看這篇下面的程式碼。他絕對不只看字尾是 y 還是 s 去作判斷,而是有一個小小的詞庫判斷字尾以及特殊字。雖然這無法做到全部單字單複數轉換,不過其實也差不多了,再多作也沒啥太大意義。
另外,如果對於單數數搞不清楚,可以使用 ruby script/console 裡面打 pluralize 跟 singularize 來測試一下
Rails 的單複數轉換
1. Inflector.inflections do inflect
2. inflect.plural(/$/, 's')
3. inflect.plural(/s$/i, 's')
4. inflect.plural(/(axtest)is$/i, '\1es')
5. inflect.plural(/(octopvir)us$/i, '\1i')
6. inflect.plural(/(aliasstatus)$/i, '\1es')
7. inflect.plural(/(bu)s$/i, '\1ses')
8. inflect.plural(/(buffaltomat)o$/i, '\1oes')
9. inflect.plural(/([ti])um$/i, '\1a')
10. inflect.plural(/sis$/i, 'ses')
11. inflect.plural(/(?:([^f])fe([lr])f)$/i, '\1\2ves')
12. inflect.plural(/(hive)$/i, '\1s')
13. inflect.plural(/([^aeiouy]qu)y$/i, '\1ies')
14. inflect.plural(/(xchsssh)$/i, '\1es')
15. inflect.plural(/(matrvertind)ixex$/i, '\1ices')
16. inflect.plural(/([ml])ouse$/i, '\1ice')
17. inflect.plural(/^(ox)$/i, '\1en')
18. inflect.plural(/(quiz)$/i, '\1zes')
19.
20. inflect.singular(/s$/i, '')
21. inflect.singular(/(n)ews$/i, '\1ews')
22. inflect.singular(/([ti])a$/i, '\1um')
23. inflect.singular(/((a)naly(b)a(d)iagno(p)arenthe(p)rogno(s)ynop(t)he)ses$/i, '\1\2sis')
24. inflect.singular(/(^analy)ses$/i, '\1sis')
25. inflect.singular(/([^f])ves$/i, '\1fe')
26. inflect.singular(/(hive)s$/i, '\1')
27. inflect.singular(/(tive)s$/i, '\1')
28. inflect.singular(/([lr])ves$/i, '\1f')
29. inflect.singular(/([^aeiouy]qu)ies$/i, '\1y')
30. inflect.singular(/(s)eries$/i, '\1eries')
31. inflect.singular(/(m)ovies$/i, '\1ovie')
32. inflect.singular(/(xchsssh)es$/i, '\1')
33. inflect.singular(/([ml])ice$/i, '\1ouse')
34. inflect.singular(/(bus)es$/i, '\1')
35. inflect.singular(/(o)es$/i, '\1')
36. inflect.singular(/(shoe)s$/i, '\1')
37. inflect.singular(/(crisaxtest)es$/i, '\1is')
38. inflect.singular(/(octopvir)i$/i, '\1us')
39. inflect.singular(/(aliasstatus)es$/i, '\1')
40. inflect.singular(/^(ox)en/i, '\1')
41. inflect.singular(/(vertind)ices$/i, '\1ex')
42. inflect.singular(/(matr)ices$/i, '\1ix')
43. inflect.singular(/(quiz)zes$/i, '\1')
44.
45. inflect.irregular('person', 'people')
46. inflect.irregular('man', 'men')
47. inflect.irregular('child', 'children')
48. inflect.irregular('sex', 'sexes')
49. inflect.irregular('move', 'moves')
50.
51. inflect.uncountable(%w(equipment information rice money species series fish sheep))
52. end
另外,如果對於單數數搞不清楚,可以使用 ruby script/console 裡面打 pluralize 跟 singularize 來測試一下
>> puts 'cat'.pluralize
cats
>> puts 'people'.singularize
person
Rails 的單複數轉換
1. Inflector.inflections do inflect
2. inflect.plural(/$/, 's')
3. inflect.plural(/s$/i, 's')
4. inflect.plural(/(axtest)is$/i, '\1es')
5. inflect.plural(/(octopvir)us$/i, '\1i')
6. inflect.plural(/(aliasstatus)$/i, '\1es')
7. inflect.plural(/(bu)s$/i, '\1ses')
8. inflect.plural(/(buffaltomat)o$/i, '\1oes')
9. inflect.plural(/([ti])um$/i, '\1a')
10. inflect.plural(/sis$/i, 'ses')
11. inflect.plural(/(?:([^f])fe([lr])f)$/i, '\1\2ves')
12. inflect.plural(/(hive)$/i, '\1s')
13. inflect.plural(/([^aeiouy]qu)y$/i, '\1ies')
14. inflect.plural(/(xchsssh)$/i, '\1es')
15. inflect.plural(/(matrvertind)ixex$/i, '\1ices')
16. inflect.plural(/([ml])ouse$/i, '\1ice')
17. inflect.plural(/^(ox)$/i, '\1en')
18. inflect.plural(/(quiz)$/i, '\1zes')
19.
20. inflect.singular(/s$/i, '')
21. inflect.singular(/(n)ews$/i, '\1ews')
22. inflect.singular(/([ti])a$/i, '\1um')
23. inflect.singular(/((a)naly(b)a(d)iagno(p)arenthe(p)rogno(s)ynop(t)he)ses$/i, '\1\2sis')
24. inflect.singular(/(^analy)ses$/i, '\1sis')
25. inflect.singular(/([^f])ves$/i, '\1fe')
26. inflect.singular(/(hive)s$/i, '\1')
27. inflect.singular(/(tive)s$/i, '\1')
28. inflect.singular(/([lr])ves$/i, '\1f')
29. inflect.singular(/([^aeiouy]qu)ies$/i, '\1y')
30. inflect.singular(/(s)eries$/i, '\1eries')
31. inflect.singular(/(m)ovies$/i, '\1ovie')
32. inflect.singular(/(xchsssh)es$/i, '\1')
33. inflect.singular(/([ml])ice$/i, '\1ouse')
34. inflect.singular(/(bus)es$/i, '\1')
35. inflect.singular(/(o)es$/i, '\1')
36. inflect.singular(/(shoe)s$/i, '\1')
37. inflect.singular(/(crisaxtest)es$/i, '\1is')
38. inflect.singular(/(octopvir)i$/i, '\1us')
39. inflect.singular(/(aliasstatus)es$/i, '\1')
40. inflect.singular(/^(ox)en/i, '\1')
41. inflect.singular(/(vertind)ices$/i, '\1ex')
42. inflect.singular(/(matr)ices$/i, '\1ix')
43. inflect.singular(/(quiz)zes$/i, '\1')
44.
45. inflect.irregular('person', 'people')
46. inflect.irregular('man', 'men')
47. inflect.irregular('child', 'children')
48. inflect.irregular('sex', 'sexes')
49. inflect.irregular('move', 'moves')
50.
51. inflect.uncountable(%w(equipment information rice money species series fish sheep))
52. end
3 則留言:
If you are open to having a guest blog poster please reply and let me know. I will provide you with unique content for your blog, thanks.
Pretty nice post. I just stumbled upon your blog and wanted to say that I have really enjoyed browsing your blog posts. In any case I’ll be subscribing to your feed and I hope you write again soon!
Thanks for some quality points there. I am kind of new to online , so I printed this off to put in my file, any better way to go about keeping track of it then printing?
張貼留言