-
Notifications
You must be signed in to change notification settings - Fork 8
/
AutoCorrect.php
45 lines (35 loc) · 1.21 KB
/
AutoCorrect.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php namespace Naux;
class AutoCorrect
{
private $dicts = null;
public function __construct()
{
$this->dicts = include __DIR__.'/dicts.php';
}
public function withDict($dicts)
{
$this->dicts = array_merge($this->dicts, $dicts);
return $this;
}
public function convert($content)
{
$content = $this->auto_space($content);
return $this->auto_correct($content);
}
public function auto_space($content)
{
$content = preg_replace('~((?![年月日号])\p{Han})([a-zA-Z0-9+$@#\[\(\/‘“])~u', '\1 \2', $content);
$content = preg_replace('~([a-zA-Z0-9+$’”\]\)@#!\/]|[\d[年月日]]{2,})((?![年月日号])\p{Han})~u', '\1 \2', $content);
# Fix () [] near the English and number
$content = preg_replace('~([a-zA-Z0-9]+)([\[\(‘“])~u', '\1 \2', $content);
$content = preg_replace('~([\)\]’”])([a-zA-Z0-9]+)~u', '\1 \2', $content);
return $content;
}
public function auto_correct($content)
{
foreach ($this->dicts as $from => $to) {
$content = preg_replace("/(?<!\.|[a-z]){$from}(?!\.|[a-z])/i", $to, $content);
}
return $content;
}
}