Skip to content

Commit 1571d46

Browse files
committed
added API methods to NameCorpus
1 parent 41b4595 commit 1571d46

File tree

2 files changed

+80
-32
lines changed

2 files changed

+80
-32
lines changed

src/Corpus/NameCorpus.php

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

tests/TextAnalysis/Corpus/NameCorpusTest.php

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,24 @@ public function testFirstNames()
2020
}
2121

2222
$corpus = new NameCorpus();
23-
$this->assertTrue($corpus->isFirstName('Dan'));
24-
$this->assertFalse($corpus->isFirstName('very'));
23+
$this->assertTrue($corpus->isFirstName('Mike'));
24+
$this->assertFalse($corpus->isFirstName('very'));
25+
}
26+
27+
public function testGetFirstName()
28+
{
29+
if( getenv('SKIP_TEST')) {
30+
return;
31+
}
2532

33+
$corpus = new NameCorpus();
34+
$firstName = $corpus->getFirstName('Mike');
35+
$this->assertNotEmpty($firstName);
36+
37+
$this->assertEmpty($corpus->getFirstName('very'));
2638
}
2739

40+
2841
public function testLastNames()
2942
{
3043
if( getenv('SKIP_TEST')) {
@@ -33,8 +46,23 @@ public function testLastNames()
3346

3447
$corpus = new NameCorpus();
3548
$this->assertTrue($corpus->isLastName('Williamson'));
36-
$this->assertFalse($corpus->isLastName('baggins'));
37-
}
49+
$this->assertFalse($corpus->isLastName('Baggins'));
50+
}
51+
52+
public function testGetLastName()
53+
{
54+
if( getenv('SKIP_TEST')) {
55+
return;
56+
}
57+
58+
$corpus = new NameCorpus();
59+
$lastName = $corpus->getLastName('Williamson');
60+
$this->assertEquals(245, $lastName['rank']);
61+
62+
$lastName = $corpus->getLastName('Baggins');
63+
$this->assertEmpty($lastName);
64+
}
65+
3866

3967
public function testFullNames()
4068
{
@@ -45,8 +73,9 @@ public function testFullNames()
4573
$corpus = new NameCorpus();
4674
$this->assertTrue($corpus->isFullName('Brad Von Williamson'));
4775
$this->assertFalse($corpus->isFullName('Jimbo'));
48-
$this->assertTrue($corpus->isFullName('Bradley Thomas'));
49-
50-
}
76+
$this->assertTrue($corpus->isFullName('Bradley Thomas'));
77+
}
78+
79+
5180

5281
}

0 commit comments

Comments
 (0)