Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
lkfs committed Aug 27, 2020
1 parent fdbc587 commit fd919b7
Show file tree
Hide file tree
Showing 7 changed files with 16,020 additions and 10,495 deletions.
40 changes: 27 additions & 13 deletions app/Http/Controllers/WordGroupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Controllers;

use App\Models\MNewWord;
use App\Models\MWordGroup;
use App\Repositories\NewWordRepository;
use App\Repositories\WordGroupRepository;
use Illuminate\Http\Request;
Expand Down Expand Up @@ -45,9 +46,10 @@ public function index()
*
* @return \Illuminate\Http\Response
*/
public function create()
public function create(Request $request)
{
return $this->repository->make();
$word = $request->input('word');
return view("new_words.word_group_edit",['word'=>$word]);
}

/**
Expand All @@ -58,7 +60,29 @@ public function create()
*/
public function store(Request $request)
{
//
$word_group = $request->input('word_group');

$pattern = '/[\x{4e00}-\x{9fa5}]/u';

if (preg_match_all($pattern, $word_group, $matches)) {
Log::info('$matches =' . json_encode($matches));
$words = collect($matches[0]);
if($words->count()>=2){
$m_word_group = new MWordGroup();
$m_word_group->word_group = $word_group;
$m_word_group->excellent = 3;
$m_word_group->save();
return array(
'code'=>1,
'message'=>'success'
);
}
}
return array(
'code'=>-1,
'message'=>'词组至少包含两个汉字'
);

}

/**
Expand Down Expand Up @@ -110,14 +134,4 @@ public function destroy($id)
);
}

public function replace(Request $request)
{
$raw_word_group = $request->input('raw_word_group');
$new_word_group = $request->input('new_word_group');
$this->repository->replace($raw_word_group,$new_word_group);
return array(
'code'=>1,
'message'=>'success'
);
}
}
12 changes: 12 additions & 0 deletions app/Repositories/NewWordRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ public function getWords($grade, $term, $write = true)
->get();
$newWords = $newWords->map(function ($newWord, $key) use ($write, $pattern) {
$wordGroups = MWordGroup::where("word_group", 'like', '%' . $newWord->word . '%')
->orderBy('excellent')
->orderBy('grade')
->orderBy('term')
->limit(10)
Expand All @@ -357,6 +358,17 @@ public function getWords($grade, $term, $write = true)
// foreach ($matches[0] as $word){
// }
$wordGroup->word_group_wrap = str_replace($newWord->word, $newWord->pinyin, $wordGroup->word_group);
switch ($newWord->word){
case '';
$wordGroup->word_group_wrap .= '(男)';
break;
case '';
$wordGroup->word_group_wrap .= '(女)';
break;
case '';
$wordGroup->word_group_wrap .= '(动物)';
break;
}
return $wordGroup;
});
$newWord->word_wrap = '';
Expand Down
7 changes: 2 additions & 5 deletions app/Repositories/WordGroupRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,8 @@ public function split()

}

public function replace($raw_word_group, $new_word_group){
MWordGroup::where('word_group', 'like', $raw_word_group.'%')
->update(
array('word_group'=>$new_word_group)
);
public function delete($word_group){
MWordGroup::where('word_group', $word_group)->delete();
}

}
152 changes: 152 additions & 0 deletions app/Repositories/WordGroupV3Repository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<?php


namespace App\Repositories;

use App\Models\MNewWord;
use App\Models\MWordGroup;
use Exception;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\Log;

/**
* 词组
* @package App\Repositories
*/
class WordGroupV3Repository extends BaseRepository
{
private $splitServer = array('114.67.84.223', '120.26.6.172', '116.196.101.207');
private $splitIndex = 0;

private function computeGradeAndTerm($wordGroup)
{
$pattern = '/[\x{4e00}-\x{9fa5}]/u';
$grade = -1;
$term = -1;
if (preg_match_all($pattern, $wordGroup, $matches)) {
Log::info('$matches =' . json_encode($matches));
$newWords = collect($matches[0]);
$newWords->each(function ($item, $key) use (&$grade, &$term, $wordGroup) {
$newWord = MNewWord::where('word', $item)
->first();
if ($newWord) {
if (($grade * 10 + $term) < ($newWord->grade * 10 + $newWord->term)) {
$grade = $newWord->grade;
$term = $newWord->term;
}
} else {
Log::info('生僻字 = ' . $wordGroup);
$grade = 9;
$term = 9;
}
});
}
return array($grade, $term);
}

//调用梁斌分词,过滤乱七八糟的词组
private function checkWord($words)
{
if (is_array($words)) $words = implode(',', $words);

Log::info('fenci = ' . $words);
$client = new Client([
'base_uri' => 'http://api.pullword.com/',
'timeout' => 5.0,
]);
$url = 'http://' . $this->splitServer[$this->splitIndex++] . '/get.php?source=' . urlencode($words) . ' &param1=0.8&param2=0';
if ($this->splitIndex >= 3) $this->splitIndex = 0;
try {
$response = $client->request('GET', $url);
$code = $response->getStatusCode();
if ($code == 200) {
$body = $response->getBody();
$pattern = '/[\x{4e00}-\x{9fa5}].*/u';
if (preg_match_all($pattern, $body, $matches)) {
$result = $matches[0];
foreach ($result as $key => $item) {
$result[$key] = str_replace("\r", "", $item);

}
return $result;
}
return null;
}
} catch (Exception $e) {
Log::info('sleep 60 seconds');
sleep(60);
return $this->checkWord($words);
}
}

//调用百度汉语,查找词组
private function searchWordGroup($word)
{
Log::info('baidu = ' . $word);
$client = new Client([
'base_uri' => 'https://hanyu.baidu.com',
'timeout' => 5.0,
]);
$url = 'https://hanyu.baidu.com/s?wd=' . urlencode($word) . '&from=zici';
try {
$response = $client->request('GET', $url);
$code = $response->getStatusCode();
if ($code == 200) {
$body = $response->getBody();
$pinyinPattern = '/<a href=".*ptype=term">([\x{4e00}-\x{9fa5}].*)<\/a>/u';
if (preg_match_all($pinyinPattern, $body, $matches)) {
return $this->checkWord($matches[1]);
}
return null;
} else {
throw new Exception('$word = ' . $word . ' error, $code = ' . $code);
}
} catch (Exception $e) {
Log::info('sleep 60 seconds for baidu');
sleep(60);
return $this->searchWordGroup($word);
}
}

/**
* 词组拆分为单字,便于拼音标注
*/
public function split()
{
$pattern = '/[\x{4e00}-\x{9fa5}]/u';
$newWords = MNewWord::orderBy('grade')
->orderBy('term')
->get();
$wordGroups = MWordGroup::orderBy('grade')
->orderBy('term')
->get();
$wordGroups->each(function ($item, $key) use ($pattern, $newWords) {
if (preg_match_all($pattern, $item->word_group, $matches)) {
$split_json = array();
foreach ($matches[0] as $word) {
$new_word = $newWords->where('word', $word)->first();
$split_json[$word] = array(
'grade' => $new_word->grade,
'term' => $new_word->term,
'pinyin' => $new_word->pinyin,
);
}
Log::info(json_encode($split_json));
MWordGroup::where('word_group', $item->word_group)
->update(
array('split_json' => json_encode($split_json))
);
}
});

}

public function replace($raw_word_group, $new_word_group)
{
MWordGroup::where('word_group', 'like', $raw_word_group . '%')
->update(
array('word_group' => $new_word_group)
);
}

}
Loading

0 comments on commit fd919b7

Please sign in to comment.