@@ -21,7 +21,13 @@ class NameCorpus extends ReadCorpusAbstract
2121 *
2222 * @var array
2323 */
24- protected $ cache = [];
24+ protected $ firstNameCache = [];
25+
26+ /**
27+ *
28+ * @var array
29+ */
30+ protected $ lastNameCache = [];
2531
2632 public function __construct ($ dir = null , $ lang = 'eng ' )
2733 {
@@ -44,7 +50,23 @@ public function getFileNames(): array
4450 */
4551 public function isFirstName ($ name ) : bool
4652 {
47- return $ this ->isName ('names_by_state_and_year ' , $ name );
53+ return !empty ($ this ->getFirstName ($ name ));
54+ }
55+
56+ /**
57+ * @todo make this more flexible
58+ * @param string $name
59+ * @return array
60+ */
61+ public function getFirstName ($ name ) : array
62+ {
63+ if (!isset ($ this ->firstNameCache [$ name ])) {
64+ $ stmt = $ this ->getPdo ()->prepare ("SELECT * FROM us_names_by_state_and_year WHERE name = LOWER(:name) LIMIT 1 " );
65+ $ stmt ->bindParam (':name ' , $ name );
66+ $ stmt ->execute ();
67+ $ this ->firstNameCache [$ name ] = $ stmt ->fetchAll (PDO ::FETCH_ASSOC ) ?? [];
68+ }
69+ return $ this ->firstNameCache [$ name ];
4870 }
4971
5072 /**
@@ -54,45 +76,41 @@ public function isFirstName($name) : bool
5476 */
5577 public function isLastName ($ name ) : bool
5678 {
57- return $ this ->isName ( ' surnames ' , $ name );
79+ return ! empty ( $ this ->getLastName ( $ name) );
5880 }
5981
6082 /**
6183 *
6284 * @param string $name
63- * @return bool
85+ * @return array
6486 */
65- public function isFullName ($ name ) : bool
87+ public function getLastName ($ name ) : array
6688 {
67- $ tokens = explode (" " , $ name );
68- if (count ($ tokens ) < 2 ) {
69- return false ;
89+ if (!isset ($ this ->lastNameCache [$ name ])) {
90+ $ stmt = $ this ->getPdo ()->prepare ("SELECT * FROM surnames WHERE name = LOWER(:name) " );
91+ $ stmt ->bindParam (':name ' , $ name );
92+ $ stmt ->execute ();
93+ $ this ->lastNameCache [$ name ] = $ stmt ->fetchAll (PDO ::FETCH_ASSOC )[0 ] ?? [];
7094 }
71- return $ this ->isFirstName ( current ( $ tokens )) && $ this -> isLastName ( end ( $ tokens )) ;
95+ return $ this ->lastNameCache [ $ name ] ;
7296 }
7397
7498 /**
75- * Check if the name exists
76- * @param string $tableName
99+ *
77100 * @param string $name
78- * @return boolean
101+ * @return bool
79102 */
80- protected function isName ( $ tableName , $ name ) : bool
103+ public function isFullName ( $ name ) : bool
81104 {
82- $ key = "{$ tableName }_ {$ name }" ;
83- if (!isset ($ this ->cache [$ key ])) {
84-
85- $ stmt = $ this ->getPdo ()->prepare ("SELECT name FROM $ tableName WHERE name = LOWER(:name) LIMIT 1 " );
86- $ stmt ->bindParam (':name ' , $ name );
87- $ stmt ->execute ();
88- $ r = !empty ($ stmt ->fetchColumn ());
89- $ this ->cache [$ key ] = $ r ;
105+ $ tokens = explode (" " , $ name );
106+ if (count ($ tokens ) < 2 ) {
107+ return false ;
90108 }
91- return $ this ->cache [$ key ];
92- }
93-
109+ return !empty ($ this ->isFirstName (current ($ tokens ))) && !empty ($ this ->isLastName (end ($ tokens )));
110+ }
94111
95112 /**
113+ * Return the raw pdo
96114 * @return PDO
97115 */
98116 public function getPdo () : PDO
@@ -106,7 +124,8 @@ public function getPdo() : PDO
106124 public function __destruct ()
107125 {
108126 unset($ this ->pdo );
109- unset($ this ->cache );
127+ unset($ this ->firstNameCache );
128+ unset($ this ->lastNameCache );
110129 }
111130
112131}
0 commit comments