Skip to content

Commit 41b4595

Browse files
committed
add isFullName check
1 parent 4253706 commit 41b4595

File tree

2 files changed

+57
-6
lines changed

2 files changed

+57
-6
lines changed

src/Corpus/NameCorpus.php

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,18 @@
1111
*/
1212
class 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

tests/TextAnalysis/Corpus/NameCorpusTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,19 @@ public function testLastNames()
3434
$corpus = new NameCorpus();
3535
$this->assertTrue($corpus->isLastName('Williamson'));
3636
$this->assertFalse($corpus->isLastName('baggins'));
37-
}
37+
}
38+
39+
public function testFullNames()
40+
{
41+
if( getenv('SKIP_TEST')) {
42+
return;
43+
}
44+
45+
$corpus = new NameCorpus();
46+
$this->assertTrue($corpus->isFullName('Brad Von Williamson'));
47+
$this->assertFalse($corpus->isFullName('Jimbo'));
48+
$this->assertTrue($corpus->isFullName('Bradley Thomas'));
49+
50+
}
51+
3852
}

0 commit comments

Comments
 (0)