1111 */
1212class NameCorpus extends ReadCorpusAbstract
1313{
14+ /**
15+ *
16+ * @var PDO
17+ */
1418 protected $ pdo ;
1519
20+ /**
21+ *
22+ * @var array
23+ */
24+ protected $ cache = [];
25+
1626 public function __construct ($ dir = null , $ lang = 'eng ' )
1727 {
1828 if (!$ dir ) {
@@ -47,6 +57,20 @@ public function isLastName($name) : bool
4757 return $ this ->isName ('surnames ' , $ name );
4858 }
4959
60+ /**
61+ *
62+ * @param string $name
63+ * @return bool
64+ */
65+ public function isFullName ($ name ) : bool
66+ {
67+ $ tokens = explode (" " , $ name );
68+ if (count ($ tokens ) < 2 ) {
69+ return false ;
70+ }
71+ return $ this ->isFirstName (current ($ tokens )) && $ this ->isLastName (end ($ tokens ));
72+ }
73+
5074 /**
5175 * Check if the name exists
5276 * @param string $tableName
@@ -55,10 +79,16 @@ public function isLastName($name) : bool
5579 */
5680 protected function isName ($ tableName , $ name ) : bool
5781 {
58- $ stmt = $ this ->getPdo ()->prepare ("SELECT name FROM $ tableName WHERE name = LOWER(:name) LIMIT 1 " );
59- $ stmt ->bindParam (':name ' , $ name );
60- $ stmt ->execute ();
61- return !empty ($ stmt ->fetchColumn ());
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 ;
90+ }
91+ return $ this ->cache [$ key ];
6292 }
6393
6494
@@ -71,7 +101,14 @@ public function getPdo() : PDO
71101 $ this ->pdo = new PDO ("sqlite: " .$ this ->getDir ().$ this ->getFileNames ()[0 ]);
72102 }
73103 return $ this ->pdo ;
74- }
104+ }
105+
106+ public function __destruct ()
107+ {
108+ unset($ this ->pdo );
109+ unset($ this ->cache );
110+ }
111+
75112}
76113
77114
0 commit comments