├── .gitignore ├── example.php ├── composer.json ├── README.md ├── AutoCorrect.php └── dicts.php /.gitignore: -------------------------------------------------------------------------------- 1 | vendor 2 | .DS_Store 3 | .idea 4 | .php_cs.cache 5 | composer.lock -------------------------------------------------------------------------------- /example.php: -------------------------------------------------------------------------------- 1 | convert('「介绍」phphub是国内php和laravel社区'); 8 | 9 | //添加外置词典 10 | echo $auto->withDict(require __DIR__.'/dicts.php')->convert('「介绍」phphub是国内php和laravel社区'); 11 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "naux/auto-correct", 3 | "license": "MIT", 4 | "authors": [ 5 | { 6 | "name": "NauxLiu", 7 | "email": "nauxliu@gmail.com" 8 | } 9 | ], 10 | "require": {}, 11 | "autoload": { 12 | "psr-4": { 13 | "Naux\\": "" 14 | } 15 | }, 16 | "require-dev": { 17 | "friendsofphp/php-cs-fixer": "^2.15" 18 | }, 19 | "scripts": { 20 | "phpcs": "php-cs-fixer fix . --ansi" 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # auto-correct 2 | 3 | 自动给中英文之间加入合理的空格并纠正专用名词大小写 4 | 5 | ## 安装 6 | 7 | 首先你需要安装有 `composer`,然后执行下面的命令安装: 8 | ``` 9 | composer require naux/auto-correct 10 | ``` 11 | 12 | ## 使用 13 | 14 | ```php 15 | use Naux\AutoCorrect; 16 | 17 | $correct = new AutoCorrect; 18 | 19 | // 在中英文之间加入合理的空格 20 | $correct->auto_space("php是世界上最好的语言,之一"); // php 是世界上最好的语言,之一 21 | 22 | // 纠正专用词汇大小写 23 | $correct->auto_correct("php是世界上最好的语言,之一"); // PHP是世界上最好的语言,之一 24 | 25 | // 加入空格并纠正词汇(auto_space + auto_correct) 26 | $correct->convert("php是世界上最好的语言,之一"); // PHP 是世界上最好的语言,之一 27 | 28 | //添加外置词典,可添加多个 29 | echo $auto 30 | ->withDict(['hello world' => 'Hello World']) 31 | ->withDict(['foo bar' => 'Foo Bar']) 32 | ->convert('hello world, foo bar, phphub'); // Hello World, Foo Bar, PHPHub 33 | ``` 34 | 35 | ## 应用案例 36 | 37 | [LaravelChina (PHPHub)](https://laravel-china.org/) - 整站标题都做了自动转换处理。 38 | -------------------------------------------------------------------------------- /AutoCorrect.php: -------------------------------------------------------------------------------- 1 | dicts = include __DIR__.'/dicts.php'; 10 | } 11 | 12 | public function withDict($dicts) 13 | { 14 | $this->dicts = array_merge($this->dicts, $dicts); 15 | 16 | return $this; 17 | } 18 | 19 | public function convert($content) 20 | { 21 | $content = $this->auto_space($content); 22 | 23 | return $this->auto_correct($content); 24 | } 25 | 26 | public function auto_space($content) 27 | { 28 | $content = preg_replace('~((?![年月日号])\p{Han})([a-zA-Z0-9+$@#\[\(\/‘“])~u', '\1 \2', $content); 29 | $content = preg_replace('~([a-zA-Z0-9+$’”\]\)@#!\/]|[\d[年月日]]{2,})((?![年月日号])\p{Han})~u', '\1 \2', $content); 30 | # Fix () [] near the English and number 31 | $content = preg_replace('~([a-zA-Z0-9]+)([\[\(‘“])~u', '\1 \2', $content); 32 | $content = preg_replace('~([\)\]’”])([a-zA-Z0-9]+)~u', '\1 \2', $content); 33 | 34 | return $content; 35 | } 36 | 37 | public function auto_correct($content) 38 | { 39 | foreach ($this->dicts as $from => $to) { 40 | $content = preg_replace("/(? 'Ruby', 5 | 'mri' => 'MRI', 6 | 'rails' => 'Rails', 7 | 'gem' => 'Gem', 8 | 'rubygems' => 'RubyGems', 9 | 'rubyonrails' => 'Ruby on Rails', 10 | 'ror' => 'Ruby on Rails', 11 | 'rubyconf' => 'RubyConf', 12 | 'railsconf' => 'RailsConf', 13 | 'rubytuesday' => 'Ruby Tuesday', 14 | 15 | 'coffeescript' => 'CoffeeScript', 16 | 'scss' => 'SCSS', 17 | 'sass' => 'Sass', 18 | 'railscasts' => 'RailsCasts', 19 | 'execjs' => 'ExecJS', 20 | 'cocoapods' => 'CocoaPods', 21 | 'rack' => 'Rack', 22 | 'sinatra' => 'Sinatra', 23 | 'grape' => 'Grape', 24 | 'unicorn' => 'Unicorn', 25 | 'capistrano' => 'Capistrano', 26 | 'puppet' => 'Puppet', 27 | 'vagrant' => 'Vagrant', 28 | 'chef' => 'Chef', 29 | 'passenger' => 'Passenger', 30 | 31 | // 'capybara' => 'Capybara', 32 | // 'lotus' => 'Lotus', 33 | // 'mina' => 'Mina', 34 | // 'rubymotion' => 'RubyMotion', 35 | // 'irb' => 'IRB', 36 | // 'pry' => 'Pry', 37 | // 'jruby' => 'JRuby', 38 | // 'mruby' => 'mRuby', 39 | // 'rvm' => 'RVM', 40 | // 'rbenv' => 'rbenv', 41 | // 'yard' => 'YARD', 42 | // 'rdoc' => 'RDoc', 43 | // 'rspec' => 'RSpec', 44 | // 'minitest' => 'MiniTest', 45 | // 'thin' => 'Thin', 46 | // 'puma' => 'Puma', 47 | // 'activerecord' => 'ActiveRecord', 48 | // 'active-record' => 'ActiveRecord', 49 | // 'activemodal' => 'ActiveModel', 50 | // 'activesupport' => 'ActiveSupport', 51 | // 'datamapper' => 'DataMapper', 52 | // 'devise' => 'Devise', 53 | // 'cancancan' => 'CanCanCan', 54 | // 'resque' => 'Resque', 55 | // 'sidekiq' => 'Sidekiq', 56 | // 'turbolinks' => 'Turbolinks', 57 | // 'sprockets' => 'Sprockets', 58 | // 'redcarpet' => 'Redcarpet', 59 | // 'sunspot' => 'Sunspot', 60 | // 'carrierwave' => 'CarrierWave', 61 | // 'paperclip' => 'PaperClip', 62 | // 'simpleform' => 'Simple Form', 63 | // 'kaminari' => 'Kaminari', 64 | // 'will_paginate' => 'will_paginate', 65 | // 'minimagick' => 'MiniMagick', 66 | // 'rmagick' => 'RMagick', 67 | // 'nokogiri' => 'Nokogiri', 68 | // 'god' => 'God', 69 | // 'eventmachine' => 'EventMachine', 70 | // 'simplecov' => 'SimpleCov', 71 | // 'brakeman' => 'Brakeman', 72 | // 'activeadmin' => 'ActiveAdmin', 73 | // 'railsadmin' => 'RailsAdmin', 74 | 75 | # Python 76 | 77 | # Node.js 78 | 'nodejs' => 'Node.js', 79 | 'npm' => 'NPM', 80 | 81 | # Go 82 | 83 | # PHP 84 | 'php' => 'PHP', 85 | 'pear' => 'Pear', 86 | 'laravel' => 'Laravel', 87 | 'lumen' => 'Lumen', 88 | 'laravel4' => 'Laravel 4', 89 | 'laravel5' => 'Laravel 5', 90 | 'traits' => 'Traits', 91 | 'composer' => 'Composer', 92 | 'homestead' => 'Homestead', 93 | 'ioc' => 'IoC', 94 | 'phpspec' => 'PHPSpec', 95 | 'codeception' => 'Codeception', 96 | 'thinkphp' => 'ThinkPHP', 97 | 98 | # Cocoa 99 | 'afnetworking' => 'AFNetworking', 100 | 'reactivecocoa' => 'ReactiveCocoa', 101 | 'three20' => 'Three20', 102 | 103 | # Java 104 | 105 | # Programming 106 | 'ssh' => 'SSH', 107 | 'web' => 'Web', 108 | 'api' => 'API', 109 | 'css' => 'CSS', 110 | 'html' => 'HTML', 111 | 'json' => 'JSON', 112 | 'jsonp' => 'JSONP', 113 | 'xml' => 'xml', 114 | 'yaml' => 'YAML', 115 | 'yml' => 'YAML', 116 | 'ini' => 'INI', 117 | 'csv' => 'CSV', 118 | 'soap' => 'SOAP', 119 | 'ajax' => 'Ajax', 120 | 'messagepack' => 'MessagePack', 121 | 'javascript' => 'JavaScript', 122 | 'js' => 'JS', 123 | 'png' => 'PNG', 124 | 'dsl' => 'DSL', 125 | 'tdd' => 'TDD', 126 | 'bdd' => 'BDD', 127 | 'cgi' => 'CGI', 128 | 'asp.net' => 'ASP.NET', 129 | '.net' => '.NET', 130 | 'rest' => 'REST', 131 | 'orm' => 'ORM', 132 | 'oauth' => 'OAuth', 133 | 'oauth2' => 'OAuth2', 134 | 'i18n' => 'I18N', 135 | 'markdown' => 'Markdown', 136 | 137 | # Sites 138 | 'amazon' => 'Amazon', 139 | 'aws' => 'AWS', 140 | 'facebook' => 'Facebook', 141 | 'github' => 'GitHub', 142 | 'gist' => 'Gist', 143 | 'ruby_china' => 'Ruby China', 144 | 'ruby-china' => 'Ruby China', 145 | 'rubychina' => 'Ruby China', 146 | 'phphub' => 'PHPHub', 147 | 'v2ex' => 'V2EX', 148 | 'hackernews' => 'Hacker News', 149 | 'heroku' => 'Heroku', 150 | 'stackoverflow' => 'Stack Overflow', 151 | 'stackexchange' => 'StackExchange', 152 | 'twitter' => 'Twitter', 153 | 'youtube' => 'YouTube', 154 | 'slack' => 'Slack', 155 | 'laracasts' => 'Laracasts', 156 | 157 | # Databases 158 | 'dynamodb' => 'DynamoDB', 159 | 'mysql' => 'MySQL', 160 | 'postgresql' => 'PostgreSQL', 161 | 'sqlite' => 'SQLite', 162 | 'memcached' => 'Memcached', 163 | 'mongodb' => 'MongoDB', 164 | 'redis' => 'Redis', 165 | 'rethinkdb' => 'RethinkDB', 166 | 'elasticsearch' => 'Elasticsearch', 167 | 'solr' => 'Solr', 168 | 'sphinx' => 'Sphinx', 169 | 170 | # System 171 | 'window' => 'Windows', 172 | 'windows' => 'Windows', 173 | 'linux' => 'Linux', 174 | 'mac' => 'Mac', 175 | 'osx' => 'OS X', 176 | 'ubuntu' => 'Ubuntu', 177 | 'rhel' => 'RHEL', 178 | 'centos' => 'CentOS', 179 | 'archlinux' => 'Arch Linux', 180 | 'redhat' => 'RedHat', 181 | 182 | # OpenSource Projects 183 | 'gitlab' => 'GitLab', 184 | 'gitlabci' => 'GitLab CI', 185 | 'fontawesome' => 'Font Awesome', 186 | 'bootstrap' => 'Bootstrap', 187 | 'less' => 'Less', 188 | 'jquery' => 'jQuery', 189 | 'requirejs' => 'RequireJS', 190 | 'underscore' => 'Underscore', 191 | 'angularjs' => 'AngularJS', 192 | 'emberjs' => 'Ember.js', 193 | 'backbone' => 'Backbone', 194 | 'seajs' => 'SeaJS', 195 | 'imagemagick' => 'ImageMagick', 196 | 'fluentd' => 'Fluentd', 197 | 'ffmpeg' => 'FFmpeg', 198 | 'wordpress' => 'WordPress', 199 | 'drupal' => 'Drupal', 200 | 'joomla' => 'Joomla', 201 | 'piwik' => 'Piwik', 202 | 'discuz' => 'Discuz!', 203 | 204 | # Tools 205 | 'git' => 'Git', 206 | 'svn' => 'SVN', 207 | 'vim' => 'VIM', 208 | 'emacs' => 'Emacs', 209 | 'textmate' => 'TextMate', 210 | 'sublime' => 'Sublime', 211 | 'rubymine' => 'RubyMine', 212 | 'sequelpro' => 'Sequel Pro', 213 | 'virtualbox' => 'VirtualBox', 214 | 'safari' => 'Safari', 215 | 'chrome' => 'Chrome', 216 | 'ie' => 'IE', 217 | 'firefox' => 'Firefox', 218 | 'dash' => 'Dash', 219 | 'termal' => 'Termal', 220 | 'iterm' => 'iTerm', 221 | 'iterm2' => 'iTerm2', 222 | 'iwork' => 'iWork', 223 | 'itunes' => 'iTunes', 224 | 'iphoto' => 'iPhoto', 225 | 'ibook' => 'iBook', 226 | 'imessage' => 'iMessage', 227 | 'tweetbot' => 'TweetBot', 228 | 'sparrow' => 'Sparrow', 229 | 'photoshop' => 'Photoshop', 230 | 'office' => 'Office', 231 | 'word' => 'Word', 232 | 'excel' => 'Excel', 233 | 'powerpoint' => 'PowerPoint', 234 | 235 | # Misc 236 | 'app' => 'App', 237 | 'ios' => 'iOS', 238 | 'iphone' => 'iPhone', 239 | 'ipad' => 'iPad', 240 | 'android' => 'Android', 241 | 'osx' => 'OS X', 242 | 'mac' => 'Mac', 243 | 'imac' => 'iMac', 244 | 'macbookpro' => 'MacBook Pro', 245 | 'macbook' => 'MacBook', 246 | 'rmbp' => 'rMBP', 247 | 'wi-fi' => 'Wi-Fi', 248 | 'wifi' => 'Wi-Fi', 249 | 'vps' => 'VPS', 250 | 'vpn' => 'VPN', 251 | 'arm' => 'ARM', 252 | 'cpu' => 'CPU', 253 | 'pdf' => 'PDF', 254 | ); 255 | --------------------------------------------------------------------------------