Skip to content

Commit

Permalink
refactor: extract tricky
Browse files Browse the repository at this point in the history
  • Loading branch information
recca0120 committed Aug 19, 2022
1 parent d7c4bea commit c8b0498
Show file tree
Hide file tree
Showing 15 changed files with 463 additions and 465 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ composer.phar
composer.lock
.phpunit.result.cache
.php_cs.cache
.php-cs-fixer.cache
.DS_Store
Thumbs.db
/2src
Expand Down
179 changes: 49 additions & 130 deletions src/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,19 @@ class Address
/**
* $normalizer.
*
* @var \Recca0120\Twzipcode\Normalizer
* @var Normalizer
*/
public $normalizer;

/**
* @var Tricky
*/
public $tricky;

/**
* $tokens.
*
* @var \Recca0120\Lodash\JArray
* @var JArray
*/
public $tokens = [];

Expand All @@ -55,42 +60,69 @@ class Address
*/
public function __construct($address = '')
{
$this->tricky = new Tricky();
if (empty($address) === false) {
$this->set($address);
}
}

/**
* __toString.
* set.
*
* @return string
* @param static|string $address
* @return $this
*/
public function __toString()
public function set($address)
{
return $this->normalizer->value();
$this->normalizer = (new Normalizer($address))->normalize()->normalizeAddress();
$this->tokens = $this->tokenize();

return $this;
}

/**
* set.
* tokenize.
*
* @param static|string $address
* @return $this
* @return JArray
*/
public function set($address)
protected function tokenize()
{
$this->normalizer = (new Normalizer($address))
->normalize()
->normalizeAddress();
$units = [static::NO => 'no', static::SUBNO => 'subno', static::NAME => 'name', static::UNIT => 'unit'];

$this->tokens = $this->tokenize();
$patterns = implode('', [
'(?:(?P<no>\d+)(?P<subno>之\d+)?(?=[巷弄號樓]|$)|(?P<name>.+?))',
'(?:(?P<unit>([島縣市鄉鎮市區村里道鄰路街段巷弄號樓]|魚臺))|(?=\d+(?:之\d+)?[巷弄號樓]|$))',
]);

return $this;
$tokens = [];
$address = $this->tricky->hash($this->normalizer)->value();
if (preg_match_all('/'.$patterns.'/u', $address, $matches, PREG_SET_ORDER) !== false) {
foreach ($matches as $values) {
$token = array_map(static function ($unit) use ($values) {
return isset($values[$unit]) === true ? $values[$unit] : '';
}, $units);
$token[static::NAME] = $this->tricky->flip($token[static::NAME]);
$tokens[] = $token;
}
}

return new JArray($tokens);
}

/**
* __toString.
*
* @return string
*/
public function __toString()
{
return $this->normalizer->value();
}

/**
* tokens.
*
* @return \Recca0120\Lodash\JArray
* @return JArray
*/
public function tokens()
{
Expand All @@ -101,7 +133,7 @@ public function tokens()
* getPoint.
*
* @param string $index
* @return \Recca0120\Twzipcode\Point
* @return Point
*/
public function getPoint($index)
{
Expand Down Expand Up @@ -132,117 +164,4 @@ public function flat($length = null, $offset = 0)
return implode('', $token);
})->join('');
}

/**
* tokenize.
*
* @return \Recca0120\Lodash\JArray
*/
protected function tokenize()
{
$tokens = [];

$units = [
static::NO => 'no',
static::SUBNO => 'subno',
static::NAME => 'name',
static::UNIT => 'unit',
];

$patterns = implode('', [
'(?:(?P<no>\d+)(?P<subno>之\d+)?(?=[巷弄號樓]|$)|(?P<name>.+?))',
'(?:(?P<unit>([島縣市鄉鎮市區村里道鄰路街段巷弄號樓]|魚臺))|(?=\d+(?:之\d+)?[巷弄號樓]|$))',
]);

/*
* 20742,新北市,萬里區,二坪,全
* 21042,連江縣,北竿鄉,坂里村,全
* 24944,新北市,八里區,八里大道,全
* 32058,桃園市,中壢區,華夏一村市場,全
* 32464,桃園市,平鎮區,三和路,全
* 32460,桃園市,平鎮區,鎮興里平鎮,連 123號至 139號
* 41273,臺中市,大里區,三民一街,全
* 42147,臺中市,后里區,七星街,全
* 51547,彰化縣,大村鄉,大仁路1段,全
* 52441,彰化縣,溪州鄉,村市路,全
* 54544,南投縣,埔里鎮,一新一巷,全
* 55347,南投縣,水里鄉,一廍路,全
* 60243,嘉義縣,番路鄉,三橋仔,全
* 60541,嘉義縣,阿里山鄉,二萬平,全
* 60845,嘉義縣,水上鄉,鄉村世界,全
* 71342,臺南市,左鎮區,二寮,全
* 72270,臺南市,佳里區,下廍,全
* 74145,臺南市,新市區,大利一路,全
* 80652,高雄市,前鎮區,一心一路,單 239號以下
* 83043,高雄市,鳳山區,海光四村市場,全
* 90542,屏東縣,里港鄉,八德路,全
* 96341,臺東縣,太麻里鄉,千禧街,全
* 98191,花蓮縣,玉里鎮,三民,全
* 98342,花蓮縣,富里鄉,三台,全
* 98392,花蓮縣,富里鄉,東里村復興,全
* 89442,金門縣,烈嶼鄉,二擔,全
*/

$trickies = [
'' => md5(''),
'' => md5(''),
'' => md5(''),
'' => md5(''),
'' => md5(''),
'' => md5(''),
'' => md5(''),
'' => md5(''),
'' => md5(''),
'新市' => md5('新市'),
'阿里山' => md5('阿里山'),
'鎮興里平' => md5('鎮興里平'),
];

$map = [
'島鄉' => $trickies[''].'',
'嶼鄉' => $trickies[''].'',
'村鄉' => $trickies[''].'',
'里鄉' => $trickies[''].'',
'村市' => $trickies[''].'',
'里區' => $trickies[''].'',
'鎮區' => $trickies[''].'',
'里鎮' => $trickies[''].'',
'里村' => $trickies[''].'',
'鄉村' => $trickies[''].'',
'路鄉' => $trickies[''].'',
'新市區' => $trickies['新市'].'',
'阿里山鄉' => $trickies['阿里山'].'',
'鎮興里平鎮' => $trickies['鎮興里平'].'',
];

$flip = [
$trickies[''] => '',
$trickies[''] => '',
$trickies[''] => '',
$trickies[''] => '',
$trickies[''] => '',
$trickies[''] => '',
$trickies[''] => '',
$trickies[''] => '',
$trickies[''] => '',
$trickies['新市'] => '新市',
$trickies['阿里山'] => '阿里山',
$trickies['鎮興里平'] => '鎮興里平',
];

$address = $this->normalizer->replace($map)->value();
$matches = [];
if (preg_match_all('/'.$patterns.'/u', $address, $matches, PREG_SET_ORDER) !== false) {
foreach ($matches as $values) {
$temp = [];
foreach ($units as $key => $unit) {
$temp[$key] = isset($values[$unit]) === true ? $values[$unit] : '';
}
$temp[static::NAME] = strtr($temp[static::NAME], $flip);
$tokens[] = $temp;
}
}

return new JArray($tokens);
}
}
2 changes: 1 addition & 1 deletion src/Contracts/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface Storage
/**
* zip3.
*
* @param \Recca0120\Twzipcode\Address $address
* @param Address $address
* @return string
*/
public function zip3(Address $address);
Expand Down
79 changes: 38 additions & 41 deletions src/Normalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,6 @@

class Normalizer extends JString
{
/**
* regularize.
*
* @return static
*/
public function regularize()
{
return $this
->toHalfCase()
->replace('/^\d+/', '')
->replace([
' ' => '',
',' => '',
'~' => '',
'-' => '',
'台灣' => '臺灣',
'台北' => '臺北',
'台中' => '臺中',
'台南' => '臺南',
'台東' => '臺東',
'釣魚台' => '釣魚臺',
'台西鄉' => '臺西鄉',
'霧台鄉' => '霧臺鄉',
]);
}

/**
* digitize.
*
* @return static
*/
public function digitize()
{
return $this->replace('/[一二三四五六七八九十百千]+(?=[段路街巷弄號樓])/u', function ($m) {
return (new static($m[0]))->chineseToNumber();
});
}

/**
* normalizeAddress.
*
Expand Down Expand Up @@ -92,10 +54,45 @@ public function normalizeAddress()
* @return static
*/
public function normalize()
{
return $this->trim()->regularize()->digitize();
}

/**
* digitize.
*
* @return static
*/
public function digitize()
{
return $this->replace('/[一二三四五六七八九十百千]+(?=[段路街巷弄號樓])/u', function ($m) {
return (new static($m[0]))->chineseToNumber();
});
}

/**
* regularize.
*
* @return static
*/
public function regularize()
{
return $this
->trim()
->regularize()
->digitize();
->toHalfCase()
->replace('/^\d+/', '')
->replace([
' ' => '',
',' => '',
'~' => '',
'-' => '',
'台灣' => '臺灣',
'台北' => '臺北',
'台中' => '臺中',
'台南' => '臺南',
'台東' => '臺東',
'釣魚台' => '釣魚臺',
'台西鄉' => '臺西鄉',
'霧台鄉' => '霧臺鄉',
]);
}
}
Loading

0 comments on commit c8b0498

Please sign in to comment.