|
|
@@ -18,18 +18,18 @@ use App\Http\Api\AuthApi;
|
|
|
|
|
|
class WbwLookupController extends Controller
|
|
|
{
|
|
|
- private $dictList = [
|
|
|
- '85dcc61c-c9e1-4ae0-9b44-cd6d9d9f0d01',//社区汇总
|
|
|
- '4d3a0d92-0adc-4052-80f5-512a2603d0e8',// system irregular
|
|
|
- '8359757e-9575-455b-a772-cc6f036caea0',// system sandhi
|
|
|
- '61f23efb-b526-4a8e-999e-076965034e60',// pali myanmar grammar
|
|
|
- 'eae9fd6f-7bac-4940-b80d-ad6cd6f433bf',// Concise P-E Dict
|
|
|
- '2f93d0fe-3d68-46ee-a80b-11fa445a29c6',// unity
|
|
|
- 'beb45062-7c20-4047-bcd4-1f636ba443d1',// U Hau Sein
|
|
|
- '8833de18-0978-434c-b281-a2e7387f69be',// 巴汉增订
|
|
|
- '3acf0c0f-59a7-4d25-a3d9-bf394a266ebd',// 汉译パーリ语辞典-黃秉榮
|
|
|
- '9ce6a53b-e28f-4fb7-b69d-b35fd5d76a24',//缅英字典
|
|
|
- ];
|
|
|
+ private $dictList = [
|
|
|
+ '85dcc61c-c9e1-4ae0-9b44-cd6d9d9f0d01', //社区汇总
|
|
|
+ '4d3a0d92-0adc-4052-80f5-512a2603d0e8', // system irregular
|
|
|
+ '8359757e-9575-455b-a772-cc6f036caea0', // system sandhi
|
|
|
+ '61f23efb-b526-4a8e-999e-076965034e60', // pali myanmar grammar
|
|
|
+ 'eae9fd6f-7bac-4940-b80d-ad6cd6f433bf', // Concise P-E Dict
|
|
|
+ '2f93d0fe-3d68-46ee-a80b-11fa445a29c6', // unity
|
|
|
+ 'beb45062-7c20-4047-bcd4-1f636ba443d1', // U Hau Sein
|
|
|
+ '8833de18-0978-434c-b281-a2e7387f69be', // 巴汉增订
|
|
|
+ '3acf0c0f-59a7-4d25-a3d9-bf394a266ebd', // 汉译パーリ语辞典-黃秉榮
|
|
|
+ '9ce6a53b-e28f-4fb7-b69d-b35fd5d76a24', //缅英字典
|
|
|
+ ];
|
|
|
/**
|
|
|
* Create a new command instance.
|
|
|
*
|
|
|
@@ -54,82 +54,85 @@ class WbwLookupController extends Controller
|
|
|
{
|
|
|
|
|
|
//
|
|
|
- $startAt = microtime(true)*1000;
|
|
|
+ $startAt = microtime(true) * 1000;
|
|
|
|
|
|
$this->initSysDict();
|
|
|
|
|
|
- $words = \explode(',',$request->get("word"));
|
|
|
- $bases = \explode(',',$request->get("base"));
|
|
|
+ $words = \explode(',', $request->get("word"));
|
|
|
+ $bases = \explode(',', $request->get("base"));
|
|
|
# 查询深度
|
|
|
- $deep = $request->get("deep",2);
|
|
|
- $result = $this->lookup($words,$bases,$deep);
|
|
|
- $endAt = microtime(true)*1000;
|
|
|
+ $deep = $request->get("deep", 2);
|
|
|
+ $result = $this->lookup($words, $bases, $deep);
|
|
|
+ $endAt = microtime(true) * 1000;
|
|
|
|
|
|
|
|
|
- return $this->ok(["rows"=>$result,
|
|
|
- "count"=>count($result),
|
|
|
- "time"=>(int)($endAt-$startAt)]);
|
|
|
+ return $this->ok([
|
|
|
+ "rows" => $result,
|
|
|
+ "count" => count($result),
|
|
|
+ "time" => (int)($endAt - $startAt)
|
|
|
+ ]);
|
|
|
}
|
|
|
|
|
|
//查用户字典获取全部结果
|
|
|
- public function lookup($words,$bases,$deep){
|
|
|
- $wordPool = array();
|
|
|
- $output = array();
|
|
|
+ public function lookup($words, $bases, $deep)
|
|
|
+ {
|
|
|
+ $wordPool = array();
|
|
|
+ $output = array();
|
|
|
foreach ($words as $word) {
|
|
|
- $wordPool[$word] = ['base' => false,'done' => false,'apply' => false];
|
|
|
- }
|
|
|
- foreach ($bases as $base) {
|
|
|
- $wordPool[$base] = ['base' => true,'done' => false,'apply' => false];
|
|
|
- }
|
|
|
+ $wordPool[$word] = ['base' => false, 'done' => false, 'apply' => false];
|
|
|
+ }
|
|
|
+ foreach ($bases as $base) {
|
|
|
+ $wordPool[$base] = ['base' => true, 'done' => false, 'apply' => false];
|
|
|
+ }
|
|
|
/**
|
|
|
* 先查询字典名称
|
|
|
*/
|
|
|
- $dict_info = DictInfo::whereIn('id',$this->dictList)->select('id','shortname')->get();
|
|
|
+ $dict_info = DictInfo::whereIn('id', $this->dictList)->select('id', 'shortname')->get();
|
|
|
$dict_name = [];
|
|
|
foreach ($dict_info as $key => $value) {
|
|
|
# code...
|
|
|
$dict_name[$value->id] = $value->shortname;
|
|
|
}
|
|
|
$caseman = new CaseMan();
|
|
|
- for ($i=0; $i < $deep; $i++) {
|
|
|
+ for ($i = 0; $i < $deep; $i++) {
|
|
|
$newBase = array();
|
|
|
|
|
|
$newWords = [];
|
|
|
foreach ($wordPool as $word => $info) {
|
|
|
# code...
|
|
|
- if($info['done'] === false){
|
|
|
+ if ($info['done'] === false) {
|
|
|
$newWords[] = $word;
|
|
|
$wordPool[$word]['done'] = true;
|
|
|
}
|
|
|
}
|
|
|
- $data = UserDict::whereIn('word',$newWords)
|
|
|
- ->whereIn('dict_id',$this->dictList)
|
|
|
- ->leftJoin('dict_infos', 'user_dicts.dict_id', '=', 'dict_infos.id')
|
|
|
- ->orderBy('confidence','desc')
|
|
|
- ->get();
|
|
|
+ $data = UserDict::whereIn('word', $newWords)
|
|
|
+ ->whereIn('dict_id', $this->dictList)
|
|
|
+ ->leftJoin('dict_infos', 'user_dicts.dict_id', '=', 'dict_infos.id')
|
|
|
+ ->orderBy('confidence', 'desc')
|
|
|
+ ->get();
|
|
|
foreach ($data as $row) {
|
|
|
# code...
|
|
|
- array_push($output,$row);
|
|
|
- if(!empty($row->parent) && !isset($wordPool[$row->parent]) ){
|
|
|
+ array_push($output, $row);
|
|
|
+ if (!empty($row->parent) && !isset($wordPool[$row->parent])) {
|
|
|
//将parent 插入待查询列表
|
|
|
- $wordPool[$row->parent] = ['base' => true,'done' => false,'apply' => false];
|
|
|
+ $wordPool[$row->parent] = ['base' => true, 'done' => false, 'apply' => false];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- //处理查询结果中的拆分信息
|
|
|
- $newWordPart = array();
|
|
|
- foreach ($wordPool as $word => $info) {
|
|
|
- if(!empty($info['factors'])){
|
|
|
- $factors = \explode('+',$info['factors']);
|
|
|
- foreach ($factors as $factor) {
|
|
|
- # 将没有的拆分放入单词查询列表
|
|
|
- if(!isset($wordPool[$factor])){
|
|
|
- $wordPool[$factor] = ['base' => true,'done' => false,'apply' => false];
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ //处理查询结果中的拆分信息
|
|
|
+ $newWordPart = array();
|
|
|
+ foreach ($wordPool as $word => $info) {
|
|
|
+ if (!empty($info['factors'])) {
|
|
|
+ $factors = \explode('+', $info['factors']);
|
|
|
+ foreach ($factors as $factor) {
|
|
|
+ # 将没有的拆分放入单词查询列表
|
|
|
+ if (!isset($wordPool[$factor])) {
|
|
|
+ $wordPool[$factor] = ['base' => true, 'done' => false, 'apply' => false];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
return $output;
|
|
|
}
|
|
|
@@ -137,16 +140,17 @@ class WbwLookupController extends Controller
|
|
|
/**
|
|
|
*
|
|
|
*/
|
|
|
- private function langCheck($query,$lang){
|
|
|
- if($query===[]){
|
|
|
+ private function langCheck($query, $lang)
|
|
|
+ {
|
|
|
+ if ($query === []) {
|
|
|
return true;
|
|
|
- }else{
|
|
|
- if(in_array(strtolower($lang),$query)){
|
|
|
+ } else {
|
|
|
+ if (in_array(strtolower($lang), $query)) {
|
|
|
return true;
|
|
|
- }else{
|
|
|
- $langFamily = explode('-',$lang)[0];
|
|
|
+ } else {
|
|
|
+ $langFamily = explode('-', $lang)[0];
|
|
|
foreach ($query as $value) {
|
|
|
- if(strpos($value,$langFamily) !== false){
|
|
|
+ if (strpos($value, $langFamily) !== false) {
|
|
|
return true;
|
|
|
}
|
|
|
}
|
|
|
@@ -154,31 +158,31 @@ class WbwLookupController extends Controller
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
- private function wbwPreference($word,$field,$userId){
|
|
|
+ private function wbwPreference($word, $field, $userId)
|
|
|
+ {
|
|
|
$prefix = 'wbw-preference';
|
|
|
$fieldMap = [
|
|
|
- 'type'=>1,
|
|
|
- 'grammar'=>2,
|
|
|
- 'meaning'=>3,
|
|
|
- 'factors'=>4,
|
|
|
- 'factorMeaning'=>5,
|
|
|
- 'parent'=>6,
|
|
|
- 'part'=>7,
|
|
|
- 'case'=>8,
|
|
|
- ];
|
|
|
+ 'type' => 1,
|
|
|
+ 'grammar' => 2,
|
|
|
+ 'meaning' => 3,
|
|
|
+ 'factors' => 4,
|
|
|
+ 'factorMeaning' => 5,
|
|
|
+ 'parent' => 6,
|
|
|
+ 'part' => 7,
|
|
|
+ 'case' => 8,
|
|
|
+ ];
|
|
|
$fieldId = $fieldMap[$field];
|
|
|
$myPreference = RedisClusters::get("{$prefix}/{$word}/{$fieldId}/{$userId}");
|
|
|
- if(!empty($myPreference)){
|
|
|
- Log::debug($word.'命中我的wbw-'.$field,['data'=>$myPreference]);
|
|
|
- return ['value'=>$myPreference,'status'=>5];
|
|
|
- }else{
|
|
|
+ if (!empty($myPreference)) {
|
|
|
+ Log::debug($word . '命中我的wbw-' . $field, ['data' => $myPreference]);
|
|
|
+ return ['value' => $myPreference, 'status' => 5];
|
|
|
+ } else {
|
|
|
$myPreference = RedisClusters::get("{$prefix}/{$word}/3/0");
|
|
|
- if(!empty($myPreference)){
|
|
|
- Log::debug($word.'命中社区wbw-'.$field,['data'=>$myPreference]);
|
|
|
- return ['value'=>$myPreference,'status'=>5];
|
|
|
+ if (!empty($myPreference)) {
|
|
|
+ Log::debug($word . '命中社区wbw-' . $field, ['data' => $myPreference]);
|
|
|
+ return ['value' => $myPreference, 'status' => 5];
|
|
|
}
|
|
|
}
|
|
|
- //Log::debug($word.'未命中'.$field);
|
|
|
return false;
|
|
|
}
|
|
|
/**
|
|
|
@@ -191,12 +195,12 @@ class WbwLookupController extends Controller
|
|
|
{
|
|
|
//
|
|
|
$user = AuthApi::current($request);
|
|
|
- if(!$user ){
|
|
|
+ if (!$user) {
|
|
|
//未登录用户
|
|
|
- return $this->error(__('auth.failed'),401,401);
|
|
|
+ return $this->error(__('auth.failed'), 401, 401);
|
|
|
}
|
|
|
|
|
|
- $startAt = microtime(true)*1000;
|
|
|
+ $startAt = microtime(true) * 1000;
|
|
|
|
|
|
// system regular
|
|
|
$this->initSysDict();
|
|
|
@@ -204,50 +208,54 @@ class WbwLookupController extends Controller
|
|
|
$channel = Channel::find($request->get('channel_id'));
|
|
|
$orgData = $request->get('data');
|
|
|
$lang = [];
|
|
|
- foreach ($request->get('lang',[]) as $value) {
|
|
|
+ foreach ($request->get('lang', []) as $value) {
|
|
|
$lang[] = strtolower($value);
|
|
|
}
|
|
|
//句子中的单词
|
|
|
$words = [];
|
|
|
foreach ($orgData as $word) {
|
|
|
# code...
|
|
|
- if( isset($word['type']) && $word['type']['value'] === '.ctl.'){
|
|
|
+ if (isset($word['type']) && $word['type']['value'] === '.ctl.') {
|
|
|
continue;
|
|
|
}
|
|
|
- if(!empty($word['real']['value'])){
|
|
|
+ if (!empty($word['real']['value'])) {
|
|
|
$words[] = $word['real']['value'];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- $result = $this->lookup($words,[],2);
|
|
|
+ $result = $this->lookup($words, [], 2);
|
|
|
$indexed = $this->toIndexed($result);
|
|
|
|
|
|
foreach ($orgData as $key => $word) {
|
|
|
- if( isset($word['type']) && $word['type']['value'] === '.ctl.'){
|
|
|
+ if (isset($word['type']) && $word['type']['value'] === '.ctl.') {
|
|
|
continue;
|
|
|
}
|
|
|
- if(empty($word['real']['value'])){
|
|
|
+ if (empty($word['real']['value'])) {
|
|
|
continue;
|
|
|
}
|
|
|
$data = $word;
|
|
|
|
|
|
- $preference = $this->wbwPreference($word['real']['value'],'meaning',$user['user_id']);
|
|
|
- if($preference!==false){
|
|
|
+ $preference = $this->wbwPreference($word['real']['value'], 'meaning', $user['user_id']);
|
|
|
+ if ($preference !== false) {
|
|
|
$data['meaning'] = $preference;
|
|
|
}
|
|
|
- $preference = $this->wbwPreference($word['real']['value'],'factors',$user['user_id']);
|
|
|
- if($preference!==false){
|
|
|
+ $preference = $this->wbwPreference($word['real']['value'], 'factors', $user['user_id']);
|
|
|
+ if ($preference !== false) {
|
|
|
$data['factors'] = $preference;
|
|
|
}
|
|
|
- $preference = $this->wbwPreference($word['real']['value'],'factorMeaning',$user['user_id']);
|
|
|
- if($preference!==false){
|
|
|
+ $preference = $this->wbwPreference($word['real']['value'], 'factorMeaning', $user['user_id']);
|
|
|
+ if ($preference !== false) {
|
|
|
$data['factorMeaning'] = $preference;
|
|
|
}
|
|
|
- $preference = $this->wbwPreference($word['real']['value'],'case',$user['user_id']);
|
|
|
- if($preference!==false){
|
|
|
+ $preference = $this->wbwPreference($word['real']['value'], 'case', $user['user_id']);
|
|
|
+ if ($preference !== false) {
|
|
|
$data['case'] = $preference;
|
|
|
}
|
|
|
- if(isset($indexed[$word['real']['value']])){
|
|
|
+ $preference = $this->wbwPreference($word['real']['value'], 'parent', $user['user_id']);
|
|
|
+ if ($preference !== false) {
|
|
|
+ $data['parent'] = $preference;
|
|
|
+ }
|
|
|
+ if (isset($indexed[$word['real']['value']])) {
|
|
|
//parent
|
|
|
$case = [];
|
|
|
$parent = [];
|
|
|
@@ -258,130 +266,133 @@ class WbwLookupController extends Controller
|
|
|
$case2 = [];
|
|
|
foreach ($indexed[$word['real']['value']] as $value) {
|
|
|
//非base优先
|
|
|
- if(strstr($value->type,'base') === FALSE){
|
|
|
+ if (strstr($value->type, 'base') === FALSE) {
|
|
|
$increment = 10;
|
|
|
- }else{
|
|
|
+ } else {
|
|
|
$increment = 1;
|
|
|
}
|
|
|
//将全部结果加上得分放入数组
|
|
|
- if($value->type !== '.cp.'){
|
|
|
- $parent = $this->insertValue([$value->parent],$parent,$increment);
|
|
|
+ if ($value->type !== '.cp.') {
|
|
|
+ $parent = $this->insertValue([$value->parent], $parent, $increment);
|
|
|
}
|
|
|
- if(isset($data['case']) && $data['case']['status']<5){
|
|
|
- if(!empty($value->type) && $value->type !== ".cp."){
|
|
|
- $case = $this->insertValue([$value->type."#".$value->grammar],$case,$increment);
|
|
|
+ if (isset($data['case']) && $data['case']['status'] < 5) {
|
|
|
+ if (!empty($value->type) && $value->type !== ".cp.") {
|
|
|
+ $case = $this->insertValue([$value->type . "#" . $value->grammar], $case, $increment);
|
|
|
}
|
|
|
}
|
|
|
- if($data['factors']['status'] < 50){
|
|
|
- $factors = $this->insertValue([$value->factors],$factors,$increment);
|
|
|
+ if ($data['factors']['status'] < 50) {
|
|
|
+ $factors = $this->insertValue([$value->factors], $factors, $increment);
|
|
|
}
|
|
|
- if(isset($data['factorMeaning']) && $data['factorMeaning']['status'] < 50){
|
|
|
- $factorMeaning = $this->insertValue([$value->factormean],$factorMeaning,$increment,false);
|
|
|
+ if (isset($data['factorMeaning']) && $data['factorMeaning']['status'] < 50) {
|
|
|
+ $factorMeaning = $this->insertValue([$value->factormean], $factorMeaning, $increment, false);
|
|
|
}
|
|
|
|
|
|
- if($data['meaning']['status'] < 50){
|
|
|
- if($this->langCheck($lang,$value->language)){
|
|
|
- $meaning = $this->insertValue(explode('$',$value->mean),$meaning,$increment,false);
|
|
|
+ if ($data['meaning']['status'] < 50) {
|
|
|
+ if ($this->langCheck($lang, $value->language)) {
|
|
|
+ $meaning = $this->insertValue(explode('$', $value->mean), $meaning, $increment, false);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- if(count($case)>0){
|
|
|
+ if (count($case) > 0) {
|
|
|
arsort($case);
|
|
|
$first = array_keys($case)[0];
|
|
|
- $data['case'] = ['value'=>$first==="_null"?"":$first,'status'=>3];
|
|
|
+ $data['case'] = ['value' => $first === "_null" ? "" : $first, 'status' => 3];
|
|
|
}
|
|
|
- if(count($parent)>0){
|
|
|
+ if (count($parent) > 0) {
|
|
|
arsort($parent);
|
|
|
$first = array_keys($parent)[0];
|
|
|
- $data['parent'] = ['value'=>$first==="_null"?"":$first,'status'=>3];
|
|
|
+ $data['parent'] = ['value' => $first === "_null" ? "" : $first, 'status' => 3];
|
|
|
+ } else {
|
|
|
+ $data['parent'] = ['value' => "", 'status' => 3];
|
|
|
}
|
|
|
- if(count($factors)>0 && empty($data['factors']['value'])){
|
|
|
+ if (count($factors) > 0 && empty($data['factors']['value'])) {
|
|
|
arsort($factors);
|
|
|
$first = array_keys($factors)[0];
|
|
|
- $data['factors'] = ['value'=>$first==="_null"?"":$first,'status'=>3];
|
|
|
+ $data['factors'] = ['value' => $first === "_null" ? "" : $first, 'status' => 3];
|
|
|
}
|
|
|
|
|
|
- if(count($factorMeaning)>0){
|
|
|
+ if (count($factorMeaning) > 0) {
|
|
|
arsort($factorMeaning);
|
|
|
$first = array_keys($factorMeaning)[0];
|
|
|
- $data['factorMeaning'] = ['value'=>$first==="_null"?"":$first,'status'=>5];
|
|
|
+ $data['factorMeaning'] = ['value' => $first === "_null" ? "" : $first, 'status' => 5];
|
|
|
}
|
|
|
- if(isset($data['factorMeaning']) && $data['factorMeaning']['status']<5){
|
|
|
+ if (isset($data['factorMeaning']) && $data['factorMeaning']['status'] < 5) {
|
|
|
$wbwFactorMeaning = [];
|
|
|
- if(!empty($data['factors']['value'])){
|
|
|
- foreach (explode("+",$data['factors']['value']) as $factor) {
|
|
|
- $preference = $this->wbwPreference($factor,'meaning',$user['user_id']);
|
|
|
- if($preference!==false){
|
|
|
+ if (!empty($data['factors']['value'])) {
|
|
|
+ foreach (explode("+", $data['factors']['value']) as $factor) {
|
|
|
+ $preference = $this->wbwPreference($factor, 'meaning', $user['user_id']);
|
|
|
+ if ($preference !== false) {
|
|
|
$wbwFactorMeaning[] = $preference['value'];
|
|
|
- }else{
|
|
|
+ } else {
|
|
|
$wbwFactorMeaning[] = $factor;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- $data['factorMeaning'] = ['value'=>implode('+',$wbwFactorMeaning),'status'=>3];
|
|
|
+ $data['factorMeaning'] = ['value' => implode('+', $wbwFactorMeaning), 'status' => 3];
|
|
|
}
|
|
|
|
|
|
- if(empty($data['meaning']['value']) && !empty($data['parent']['value'])){
|
|
|
- if(isset($indexed[$data['parent']['value']])){
|
|
|
+ if (empty($data['meaning']['value']) && !empty($data['parent']['value'])) {
|
|
|
+ if (isset($indexed[$data['parent']['value']])) {
|
|
|
foreach ($indexed[$data['parent']['value']] as $value) {
|
|
|
//根据base 查找词意
|
|
|
//非base优先
|
|
|
$increment = 10;
|
|
|
- if($this->langCheck($lang,$value->language)){
|
|
|
- $meaning = $this->insertValue(explode('$',$value->mean),$meaning,$increment,false);
|
|
|
+ if ($this->langCheck($lang, $value->language)) {
|
|
|
+ $meaning = $this->insertValue(explode('$', $value->mean), $meaning, $increment, false);
|
|
|
}
|
|
|
//查找词源
|
|
|
- if(!empty($value->parent) && $value->parent !== $value->word && strstr($value->type,"base") !== FALSE ){
|
|
|
- $parent2 = $this->insertValue([$value->grammar."$".$value->parent],$parent2,1,false);
|
|
|
+ if (!empty($value->parent) && $value->parent !== $value->word && strstr($value->type, "base") !== FALSE) {
|
|
|
+ $parent2 = $this->insertValue([$value->grammar . "$" . $value->parent], $parent2, 1, false);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- if(count($meaning)>0){
|
|
|
+ if (count($meaning) > 0) {
|
|
|
arsort($meaning);
|
|
|
$first = array_keys($meaning)[0];
|
|
|
- $data['meaning'] = ['value'=>$first==="_null"?"":$first,'status'=>3];
|
|
|
+ $data['meaning'] = ['value' => $first === "_null" ? "" : $first, 'status' => 3];
|
|
|
}
|
|
|
- if(count($parent2)>0){
|
|
|
+ if (count($parent2) > 0) {
|
|
|
arsort($parent2);
|
|
|
- $first = explode("$",array_keys($parent2)[0]);
|
|
|
- $data['parent2'] = ['value'=>$first[1],'status'=>3];
|
|
|
- $data['grammar2'] = ['value'=>$first[0],'status'=>3];
|
|
|
+ $first = explode("$", array_keys($parent2)[0]);
|
|
|
+ $data['parent2'] = ['value' => $first[1], 'status' => 3];
|
|
|
+ $data['grammar2'] = ['value' => $first[0], 'status' => 3];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- if(!isset($data['factorMeaning']['value']) ||
|
|
|
- $this->fmEmpty($data['factorMeaning']['value'])){
|
|
|
+ if (
|
|
|
+ !isset($data['factorMeaning']['value']) ||
|
|
|
+ $this->fmEmpty($data['factorMeaning']['value'])
|
|
|
+ ) {
|
|
|
$factorMeaning = [];
|
|
|
//生成自动的拆分意思
|
|
|
$autoMeaning = '';
|
|
|
- $currFactors = explode('+',$data['factors']['value']) ;
|
|
|
+ $currFactors = explode('+', $data['factors']['value']);
|
|
|
$autoFM = [];
|
|
|
foreach ($currFactors as $factor) {
|
|
|
- $subFactors = explode('-',$factor) ;
|
|
|
+ $subFactors = explode('-', $factor);
|
|
|
$autoSubFM = [];
|
|
|
foreach ($subFactors as $subFactor) {
|
|
|
- $preference = $this->wbwPreference($subFactor,'factorMeaning',$user['user_id']);
|
|
|
- if($preference !== false){
|
|
|
+ $preference = $this->wbwPreference($subFactor, 'factorMeaning', $user['user_id']);
|
|
|
+ if ($preference !== false) {
|
|
|
$autoSubFM[] = $preference['value'];
|
|
|
- }else{
|
|
|
- $preference = $this->wbwPreference($subFactor,'meaning',$user['user_id']);
|
|
|
- if($preference !== false){
|
|
|
+ } else {
|
|
|
+ $preference = $this->wbwPreference($subFactor, 'meaning', $user['user_id']);
|
|
|
+ if ($preference !== false) {
|
|
|
$autoSubFM[] = $preference['value'];
|
|
|
- }else{
|
|
|
+ } else {
|
|
|
$autoSubFM[] = '';
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
}
|
|
|
- $autoFM[] = implode('-',$autoSubFM);
|
|
|
- $autoMeaning .= implode('',$autoSubFM);
|
|
|
+ $autoFM[] = implode('-', $autoSubFM);
|
|
|
+ $autoMeaning .= implode('', $autoSubFM);
|
|
|
}
|
|
|
- $autoMeaning .= implode('',$autoFM);
|
|
|
- if(count($autoFM) > 0){
|
|
|
- $data['factorMeaning'] = ['value'=>implode('+',$autoFM),'status'=>3];
|
|
|
- if(empty($data['meaning']['value']) && !empty($autoMeaning)){
|
|
|
- $data['meaning'] = ['value'=>$autoMeaning,'status'=>3];
|
|
|
+ $autoMeaning .= implode('', $autoFM);
|
|
|
+ if (count($autoFM) > 0) {
|
|
|
+ $data['factorMeaning'] = ['value' => implode('+', $autoFM), 'status' => 3];
|
|
|
+ if (empty($data['meaning']['value']) && !empty($autoMeaning)) {
|
|
|
+ $data['meaning'] = ['value' => $autoMeaning, 'status' => 3];
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -392,10 +403,11 @@ class WbwLookupController extends Controller
|
|
|
return $this->ok($orgData);
|
|
|
}
|
|
|
|
|
|
- private function fmEmpty($value){
|
|
|
- if(str_replace(['+','-',' '],'',$value) === ''){
|
|
|
+ private function fmEmpty($value)
|
|
|
+ {
|
|
|
+ if (str_replace(['+', '-', ' '], '', $value) === '') {
|
|
|
return true;
|
|
|
- }else{
|
|
|
+ } else {
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
@@ -405,54 +417,54 @@ class WbwLookupController extends Controller
|
|
|
* @param string $sentId
|
|
|
* @return \Illuminate\Http\Response
|
|
|
*/
|
|
|
- public function show(Request $request,string $sentId)
|
|
|
+ public function show(Request $request, string $sentId)
|
|
|
{
|
|
|
- $startAt = microtime(true)*1000;
|
|
|
+ $startAt = microtime(true) * 1000;
|
|
|
|
|
|
$channel = Channel::find($request->get('channel_id'));
|
|
|
|
|
|
//查询句子中的单词
|
|
|
- $sent = \explode('-',$sentId);
|
|
|
- $wbw = WbwTemplate::where('book',$sent[0])
|
|
|
- ->where('paragraph',$sent[1])
|
|
|
- ->whereBetween('wid',[$sent[2],$sent[3]])
|
|
|
- ->orderBy('wid')
|
|
|
- ->get();
|
|
|
+ $sent = \explode('-', $sentId);
|
|
|
+ $wbw = WbwTemplate::where('book', $sent[0])
|
|
|
+ ->where('paragraph', $sent[1])
|
|
|
+ ->whereBetween('wid', [$sent[2], $sent[3]])
|
|
|
+ ->orderBy('wid')
|
|
|
+ ->get();
|
|
|
$words = [];
|
|
|
foreach ($wbw as $row) {
|
|
|
- if($row->type !== '.ctl.' && !empty($row->real)){
|
|
|
+ if ($row->type !== '.ctl.' && !empty($row->real)) {
|
|
|
$words[] = $row->real;
|
|
|
}
|
|
|
}
|
|
|
- $result = $this->lookup($words,[],2);
|
|
|
+ $result = $this->lookup($words, [], 2);
|
|
|
$indexed = $this->toIndexed($result);
|
|
|
|
|
|
//生成自动填充结果
|
|
|
$wbwContent = [];
|
|
|
foreach ($wbw as $row) {
|
|
|
- $type = $row->type=='?'? '':$row->type;
|
|
|
- $grammar = $row->gramma=='?'? '':$row->gramma;
|
|
|
- $part = $row->part=='?'? '':$row->part;
|
|
|
- if(!empty($type) || !empty($grammar)){
|
|
|
+ $type = $row->type == '?' ? '' : $row->type;
|
|
|
+ $grammar = $row->gramma == '?' ? '' : $row->gramma;
|
|
|
+ $part = $row->part == '?' ? '' : $row->part;
|
|
|
+ if (!empty($type) || !empty($grammar)) {
|
|
|
$case = "{$type}#$grammar";
|
|
|
- }else{
|
|
|
+ } else {
|
|
|
$case = "";
|
|
|
}
|
|
|
$data = [
|
|
|
- 'sn'=>[$row->wid],
|
|
|
- 'word'=>['value'=>$row->word,'status'=>3],
|
|
|
- 'real'=> ['value'=>$row->real,'status'=>3],
|
|
|
- 'meaning'=> ['value'=>[],'status'=>3],
|
|
|
- 'type'=> ['value'=>$type,'status'=>3],
|
|
|
- 'grammar'=> ['value'=>$grammar,'status'=>3],
|
|
|
- 'case'=> ['value'=>$case,'status'=>3],
|
|
|
- 'style'=> ['value'=>$row->style,'status'=>3],
|
|
|
- 'factors'=> ['value'=>$part,'status'=>3],
|
|
|
- 'factorMeaning'=> ['value'=>'','status'=>3],
|
|
|
- 'confidence'=> 0.5
|
|
|
- ];
|
|
|
- if($row->type !== '.ctl.' && !empty($row->real)){
|
|
|
- if(isset($indexed[$row->real])){
|
|
|
+ 'sn' => [$row->wid],
|
|
|
+ 'word' => ['value' => $row->word, 'status' => 3],
|
|
|
+ 'real' => ['value' => $row->real, 'status' => 3],
|
|
|
+ 'meaning' => ['value' => [], 'status' => 3],
|
|
|
+ 'type' => ['value' => $type, 'status' => 3],
|
|
|
+ 'grammar' => ['value' => $grammar, 'status' => 3],
|
|
|
+ 'case' => ['value' => $case, 'status' => 3],
|
|
|
+ 'style' => ['value' => $row->style, 'status' => 3],
|
|
|
+ 'factors' => ['value' => $part, 'status' => 3],
|
|
|
+ 'factorMeaning' => ['value' => '', 'status' => 3],
|
|
|
+ 'confidence' => 0.5
|
|
|
+ ];
|
|
|
+ if ($row->type !== '.ctl.' && !empty($row->real)) {
|
|
|
+ if (isset($indexed[$row->real])) {
|
|
|
//parent
|
|
|
$case = [];
|
|
|
$parent = [];
|
|
|
@@ -463,67 +475,70 @@ class WbwLookupController extends Controller
|
|
|
$case2 = [];
|
|
|
foreach ($indexed[$row->real] as $value) {
|
|
|
//非base优先
|
|
|
- if(strstr($value->type,'base') === FALSE){
|
|
|
+ if (strstr($value->type, 'base') === FALSE) {
|
|
|
$increment = 10;
|
|
|
- }else{
|
|
|
+ } else {
|
|
|
$increment = 1;
|
|
|
}
|
|
|
//将全部结果加上得分放入数组
|
|
|
- $parent = $this->insertValue([$value->parent],$parent,$increment);
|
|
|
- $case = $this->insertValue([$value->type."#".$value->grammar],$case,$increment);
|
|
|
- $factors = $this->insertValue([$value->factors],$factors,$increment);
|
|
|
- $factorMeaning = $this->insertValue([$value->factormean],$factorMeaning,$increment);
|
|
|
- $meaning = $this->insertValue(explode('$',$value->mean),$meaning,$increment,false);
|
|
|
+ $parent = $this->insertValue([$value->parent], $parent, $increment);
|
|
|
+ $case = $this->insertValue([$value->type . "#" . $value->grammar], $case, $increment);
|
|
|
+ $factors = $this->insertValue([$value->factors], $factors, $increment);
|
|
|
+ $factorMeaning = $this->insertValue([$value->factormean], $factorMeaning, $increment);
|
|
|
+ $meaning = $this->insertValue(explode('$', $value->mean), $meaning, $increment, false);
|
|
|
}
|
|
|
- if(count($case)>0){
|
|
|
+ if (count($case) > 0) {
|
|
|
arsort($case);
|
|
|
$first = array_keys($case)[0];
|
|
|
- $data['case'] = ['value'=>$first==="_null"?"":$first,'status'=>3];
|
|
|
+ $data['case'] = ['value' => $first === "_null" ? "" : $first, 'status' => 3];
|
|
|
}
|
|
|
- if(count($parent)>0){
|
|
|
+ if (count($parent) > 0) {
|
|
|
arsort($parent);
|
|
|
$first = array_keys($parent)[0];
|
|
|
- $data['parent'] = ['value'=>$first==="_null"?"":$first,'status'=>3];
|
|
|
+ $data['parent'] = ['value' => $first === "_null" ? "" : $first, 'status' => 3];
|
|
|
}
|
|
|
- if(count($factors)>0){
|
|
|
+ if (count($factors) > 0) {
|
|
|
arsort($factors);
|
|
|
$first = array_keys($factors)[0];
|
|
|
- $data['factors'] = ['value'=>$first==="_null"?"":$first,'status'=>3];
|
|
|
+ $data['factors'] = ['value' => $first === "_null" ? "" : $first, 'status' => 3];
|
|
|
}
|
|
|
- if(count($factorMeaning)>0){
|
|
|
+ if (count($factorMeaning) > 0) {
|
|
|
arsort($factorMeaning);
|
|
|
$first = array_keys($factorMeaning)[0];
|
|
|
- $data['factorMeaning'] = ['value'=>$first==="_null"?"":$first,'status'=>3];
|
|
|
+ $data['factorMeaning'] = ['value' => $first === "_null" ? "" : $first, 'status' => 3];
|
|
|
}
|
|
|
|
|
|
//根据base 查找词意
|
|
|
- if(!empty($data['parent'])){
|
|
|
- if(isset($indexed[$data['parent']['value']])){
|
|
|
+ if (!empty($data['parent'])) {
|
|
|
+ if (isset($indexed[$data['parent']['value']])) {
|
|
|
foreach ($indexed[$data['parent']['value']] as $value) {
|
|
|
//非base优先
|
|
|
$increment = 10;
|
|
|
- $meaning = $this->insertValue(explode('$',$value->mean),$meaning,$increment,false);
|
|
|
+ $meaning = $this->insertValue(explode('$', $value->mean), $meaning, $increment, false);
|
|
|
}
|
|
|
- }else{
|
|
|
+ } else {
|
|
|
//Log::error("no set parent".$data['parent']['value']);
|
|
|
}
|
|
|
}
|
|
|
- if(count($meaning)>0){
|
|
|
+ if (count($meaning) > 0) {
|
|
|
arsort($meaning);
|
|
|
$first = array_keys($meaning)[0];
|
|
|
- $data['meaning'] = ['value'=>$first==="_null"?"":$first,'status'=>3];
|
|
|
+ $data['meaning'] = ['value' => $first === "_null" ? "" : $first, 'status' => 3];
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
$wbwContent[] = $data;
|
|
|
}
|
|
|
- $endAt = microtime(true)*1000;
|
|
|
- return $this->ok(["rows"=>$wbwContent,
|
|
|
- "count"=>count($wbwContent),
|
|
|
- "time"=>(int)($endAt-$startAt)]);
|
|
|
+ $endAt = microtime(true) * 1000;
|
|
|
+ return $this->ok([
|
|
|
+ "rows" => $wbwContent,
|
|
|
+ "count" => count($wbwContent),
|
|
|
+ "time" => (int)($endAt - $startAt)
|
|
|
+ ]);
|
|
|
}
|
|
|
|
|
|
- private function toIndexed($words){
|
|
|
+ private function toIndexed($words)
|
|
|
+ {
|
|
|
//转成索引数组
|
|
|
$indexed = [];
|
|
|
foreach ($words as $key => $value) {
|
|
|
@@ -536,21 +551,22 @@ class WbwLookupController extends Controller
|
|
|
/**
|
|
|
* $empty:是否允许空值
|
|
|
*/
|
|
|
- private function insertValue($value,$container,$increment,$empty=true){
|
|
|
+ private function insertValue($value, $container, $increment, $empty = true)
|
|
|
+ {
|
|
|
foreach ($value as $one) {
|
|
|
- if($empty === false){
|
|
|
- if($this->fmEmpty($one)){
|
|
|
+ if ($empty === false) {
|
|
|
+ if ($this->fmEmpty($one)) {
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
- $one=trim($one);
|
|
|
+ $one = trim($one);
|
|
|
$key = $one;
|
|
|
- if(empty($key)){
|
|
|
+ if (empty($key)) {
|
|
|
$key = '_null';
|
|
|
}
|
|
|
- if(isset($container[$key])){
|
|
|
+ if (isset($container[$key])) {
|
|
|
$container[$key] += $increment;
|
|
|
- }else{
|
|
|
+ } else {
|
|
|
$container[$key] = $increment;
|
|
|
}
|
|
|
}
|