I18N_Arabic

# Source for file Gender.php

Documentation is available at Gender.php

1. <?php
2. /**
3. * ----------------------------------------------------------------------
4. *
5. * Copyright (c) 2006-2016 Khaled Al-Sham'aa.
6. *
7. * http://www.ar-php.org
8. *
9. * PHP Version 5
10. *
11. * ----------------------------------------------------------------------
12. *
13. * LICENSE
14. *
15. * This program is open source product; you can redistribute it and/or
16. * modify it under the terms of the GNU Lesser General Public License (LGPL)
17. * as published by the Free Software Foundation; either version 3
18. * of the License, or (at your option) any later version.
19. *
20. * This program is distributed in the hope that it will be useful,
21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23. * GNU Lesser General Public License for more details.
24. *
25. * You should have received a copy of the GNU Lesser General Public License
26. * along with this program. If not, see <http://www.gnu.org/licenses/lgpl.txt>.
27. *
28. * ----------------------------------------------------------------------
29. *
30. * Class Name: Arabic Gender Guesser
31. *
32. * Filename: Gender.php
33. *
34. * Original Author(s): Khaled Al-Sham'aa <khaled@ar-php.org>
35. *
36. * Purpose: This class attempts to guess the gender of Arabic names
37. *
38. * ----------------------------------------------------------------------
39. *
40. * Arabic Gender Guesser
41. *
42. * This PHP class attempts to guess the gender of Arabic names.
43. *
44. * Arabic nouns are either masculine or feminine. Usually when referring to a male,
45. * a masculine noun is usually used and when referring to a female, a feminine noun
46. * is used. In most cases the feminine noun is formed by adding a special characters
47. * to the end of the masculine noun. Its not just nouns referring to people that
48. * have gender. Inanimate objects (doors, houses, cars, etc.) is either masculine or
49. * feminine. Whether an inanimate noun is masculine or feminine is mostly
50. * arbitrary.
51. *
52. * Example:
53. * <code>
54. * include('./I18N/Arabic.php');
55. * $obj = new I18N_Arabic('Gender');
56. *
57. * echo "$name ";
58. *
59. * if ($obj->isFemale($name) == true) {
60. * echo '(Female)';
61. * }else{
62. * echo '(Male)';
63. * }
64. * </code>
65. *
66. * @category I18N
67. * @package I18N_Arabic
68. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
69. * @copyright 2006-2016 Khaled Al-Sham'aa
70. *
71. * @license LGPL <http://www.gnu.org/licenses/lgpl.txt>
72. * @link http://www.ar-php.org
73. */
75. /**
76. * This PHP class attempts to guess the gender of Arabic names
77. *
78. * @category I18N
79. * @package I18N_Arabic
80. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
81. * @copyright 2006-2016 Khaled Al-Sham'aa
82. *
83. * @license LGPL <http://www.gnu.org/licenses/lgpl.txt>
84. * @link http://www.ar-php.org
85. */
86. class I18N_Arabic_Gender
87. {
88. /**
89. * Loads initialize values
90. *
91. * @ignore
92. */
93. public function __construct()
94. {
95. }
97. /**
98. * Check if Arabic word is feminine
99. *
100. * @param string $str Arabic word you would like to check if it is
101. * feminine
102. *
103. * @return boolean Return true if input Arabic word is feminine
104. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
105. */
106. public static function isFemale($str)
107. {
108. $female = false;
110. $words = explode(' ', $str);
111. $str = $words[0];
113. $str = str_replace(array('أ','إ','آ'), 'ا', $str);
115. $last = mb_substr($str, -1, 1, 'UTF-8');
116. $beforeLast = mb_substr($str, -2, 1, 'UTF-8');
118. if ($last == 'ة' || $last == 'ه' || $last == 'ى' || $last == 'ا'
119. || ($last == 'ء' && $beforeLast == 'ا')
120. ) {
122. $female = true;
123. } elseif (preg_match("/^[اإ].{2}ا.$/u", $str)
124. || preg_match("/^[إا].ت.ا.+$/u", $str)
125. ) {
126. // الأسماء على وزن إفتعال و إفعال
127. $female = true;
128. } else {
129. // List of the most common irregular Arabic female names
130. $names = file(dirname(__FILE__).'/data/female.txt');
131. $names = array_map('trim', $names);
133. if (array_search($str, $names) > 0) {
134. $female = true;
135. }
136. }
138. return $female;
139. }
140. }

Documentation generated on Fri, 01 Jan 2016 10:25:59 +0200 by phpDocumentor 1.4.0