visuddhinanda 2 lat temu
rodzic
commit
5d239d25fc

+ 89 - 0
app/Console/Commands/UpgradeWbwWeight.php

@@ -0,0 +1,89 @@
+<?php
+
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+use App\Models\WbwTemplate;
+
+class UpgradeWbwWeight extends Command
+{
+    /**
+     * The name and signature of the console command.
+     * php artisan upgrade:wbw.weight 66
+     * @var string
+     */
+    protected $signature = 'upgrade:wbw.weight {book?} {para?}';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'upgrade wbw template weight by word bold ';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return int
+     */
+    public function handle()
+    {
+        $book = $this->argument('book');
+        $para = $this->argument('para');
+
+        for ($iBook=1; $iBook <= 217 ; $iBook++) {
+            if($book && $book != $iBook ){
+                continue;
+            }
+            $this->info('running book='.$iBook);
+            $maxPara = WbwTemplate::where("book",$iBook)->max('paragraph');
+		    $bar = $this->output->createProgressBar($maxPara);
+            for ($iPara=1; $iPara <= $maxPara ; $iPara++) {
+                if($para && $para != $iPara ){
+                    continue;
+                }
+                $bar->advance();
+                $words = WbwTemplate::where("book",$iBook)
+                                    ->where("paragraph",$iPara)
+                                    ->orderBy('wid','asc')
+                                    ->get();
+                $start = -1;
+                $bold = 0;
+                for ($iWord=0; $iWord < count($words); $iWord++) {
+                    WbwTemplate::where('id',$words[$iWord]->id)->update(['weight'=>1]);
+                    if($words[$iWord]->style === 'bld'){
+                        if($start === -1){
+                            $start = $iWord;
+                            $bold = 1;
+                        }else{
+                            $bold++;
+                        }
+                    }else{
+                        if($start>=0){
+                            $weight = 10 / pow($bold,2);
+                            for ($i=$start; $i < $iWord ; $i++) {
+                                $wordWeight = 1 + $weight;
+                                $result = WbwTemplate::where('id',$words[$i]->id)
+                                                    ->update(['weight'=>(int)$wordWeight]);
+                            }
+                            $start = -1;
+                            $bold = 0;
+                        }
+                    }
+                }
+            }
+            $bar->finish();
+        }
+        return 0;
+    }
+}

+ 151 - 0
app/Http/Controllers/SearchPaliWbwController.php

@@ -0,0 +1,151 @@
+<?php
+
+namespace App\Http\Controllers;
+
+
+use Illuminate\Http\Request;
+use Illuminate\Support\Facades\DB;
+use App\Models\WbwTemplate;
+use App\Http\Resources\SearchPaliWbwResource;
+use App\Http\Resources\SearchBookResource;
+
+class SearchPaliWbwController extends Controller
+{
+    /**
+     * Display a listing of the resource.
+     *
+     * @return \Illuminate\Http\Response
+     */
+    public function index(Request $request)
+    {
+        //获取书的范围
+        $bookId = [];
+        $search = new SearchController;
+        if($request->has('book')){
+            $bookId = [(int)$request->get('book')];
+        }else if($request->has('tags')){
+            //查询搜索范围
+            //查询搜索范围
+            $tagItems = explode(';',$request->get('tags'));
+
+            foreach ($tagItems as $tagItem) {
+                $bookId = array_merge($bookId,$search->getBookIdByTags(explode(',',$tagItem)));
+            }
+        }
+
+        $keyWords = explode(',',$request->get('key'));
+        switch ($request->get('view')) {
+            case 'pali':
+                $table = WbwTemplate::whereIn('real',$keyWords)
+                                        ->groupBy(['book','paragraph'])
+                                        ->selectRaw('book,paragraph,sum(weight) as rank');
+                break;
+            case 'para':
+                $table = WbwTemplate::whereIn('word',$keyWords)
+                                    ->groupBy(['book','paragraph'])
+                                    ->selectRaw('book,paragraph');
+                break;
+            default:
+                return $this->error('unknown param view='.$request->get('view'),500,500);
+                break;
+        }
+        $placeholderWord = implode(",",array_fill(0, count($keyWords), '?')) ;
+        $whereWord = "real in ({$placeholderWord})";
+        $whereBookId = '';
+        if(count($bookId)>0){
+            $table =  $table->whereIn('pcd_book_id',$bookId);
+            $placeholderBookId = implode(",",array_fill(0, count($bookId), '?')) ;
+            $whereBookId = " and pcd_book_id in ({$placeholderBookId}) ";
+        }
+        $queryCount = "SELECT count(*) FROM ( SELECT book,paragraph FROM wbw_templates WHERE $whereWord $whereBookId  GROUP BY book,paragraph) T;";
+        $count = DB::select($queryCount,array_merge($keyWords,$bookId));
+
+        $table =  $table->orderBy('rank','desc');
+        $table =  $table->skip($request->get("offset",0))
+                        ->take($request->get('limit',10));
+
+        $result = $table->get();
+        return $this->ok([
+            "rows"=>SearchPaliWbwResource::collection($result),
+            "count"=>$count[0]->count,
+            ]);
+    }
+
+    public function book_list(Request $request){
+        //获取书的范围
+        $bookId = [];
+        $search = new SearchController;
+        if($request->has('tags')){
+            //查询搜索范围
+            //查询搜索范围
+            $tagItems = explode(';',$request->get('tags'));
+
+            foreach ($tagItems as $tagItem) {
+                $bookId = array_merge($bookId,$search->getBookIdByTags(explode(',',$tagItem)));
+            }
+        }
+        $keyWords = explode(',',$request->get('key'));
+        switch ($request->get('view')) {
+            case 'pali':
+                $table = WbwTemplate::whereIn('real',$keyWords);
+                break;
+            case 'para':
+                $table = WbwTemplate::whereIn('word',$keyWords);
+                break;
+            default:
+                return $this->error('unknown param view='.$request->get('view'),500,500);
+                break;
+        }
+        if(count($bookId)>0){
+            $table = $table->whereIn('pcd_book_id',$bookId);
+        }
+        $table = $table->groupBy('pcd_book_id')
+                       ->selectRaw('pcd_book_id,count(*) as co');
+        $result = $table->get();
+        return $this->ok(["rows"=>SearchBookResource::collection($result),"count"=>count($result)]);
+    }
+    /**
+     * Store a newly created resource in storage.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @return \Illuminate\Http\Response
+     */
+    public function store(Request $request)
+    {
+
+    }
+
+    /**
+     * Display the specified resource.
+     *
+     * @param  \App\Models\WbwTemplate  $wbwTemplate
+     * @return \Illuminate\Http\Response
+     */
+    public function show(WbwTemplate $wbwTemplate)
+    {
+        //
+    }
+
+    /**
+     * Update the specified resource in storage.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @param  \App\Models\WbwTemplate  $wbwTemplate
+     * @return \Illuminate\Http\Response
+     */
+    public function update(Request $request, WbwTemplate $wbwTemplate)
+    {
+        //
+    }
+
+    /**
+     * Remove the specified resource from storage.
+     *
+     * @param  \App\Models\WbwTemplate  $wbwTemplate
+     * @return \Illuminate\Http\Response
+     */
+    public function destroy(WbwTemplate $wbwTemplate)
+    {
+        //
+    }
+}

+ 57 - 0
app/Http/Resources/SearchPaliWbwResource.php

@@ -0,0 +1,57 @@
+<?php
+
+namespace App\Http\Resources;
+
+use Illuminate\Http\Resources\Json\JsonResource;
+use App\Models\PaliText;
+
+class SearchPaliWbwResource extends JsonResource
+{
+    /**
+     * Transform the resource into an array.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
+     */
+    public function toArray($request)
+    {
+        $data = [
+            "book"=>$this->book,
+            "paragraph"=> $this->paragraph,
+        ];
+        if(isset($this->rank)){
+            $data["rank"] = $this->rank;
+        }
+        $paliText = PaliText::where('book',$this->book)
+                    ->where('paragraph',$this->paragraph)
+                    ->first();
+        if($paliText){
+            $data['path'] = json_decode($paliText->path);
+            if($paliText->level<100){
+                $data["paliTitle"] = $paliText->toc;
+            }else{
+                $data["paliTitle"] = PaliText::where('book',$this->book)
+                                            ->where('paragraph',$paliText->parent)
+                                            ->value('toc');
+            }
+            $keyWords = explode(',',$request->get('key'));
+            $keyWordsUpper=$keyWords;
+            foreach ($keyWords as $key => $word) {
+                if(mb_substr($word,-3,null,"UTF-8")==='nti'){
+                    $keyWordsUpper[] = mb_substr($word,0,mb_strlen($word,"UTF-8")-3,"UTF-8");
+                }else if(mb_substr($word,-3,null,"UTF-8")==='ti'){
+                    $keyWordsUpper[] = mb_substr($word,0,mb_strlen($word,"UTF-8")-2,"UTF-8");
+                }
+            }
+            foreach ($keyWords as $key => $word) {
+                $keyWordsUpper[] = mb_strtoupper(mb_substr($word,0,1,"UTF-8"),"UTF-8").mb_substr($word,1,null,"UTF-8");
+            }
+            $keyReplace = array();
+            foreach ($keyWordsUpper as $key => $word) {
+                $keyReplace[] = "<span class='hl'>{$word}</span>";
+            }
+            $data["highlight"] = str_replace($keyWordsUpper,$keyReplace,$paliText->html);
+        }
+        return $data;
+    }
+}

+ 35 - 0
database/migrations/2023_11_20_022513_add_weight_in_wbw_templates.php

@@ -0,0 +1,35 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class AddWeightInWbwTemplates extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::table('wbw_templates', function (Blueprint $table) {
+            //
+            $table->integer('weight')->index()->default(1);
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::table('wbw_templates', function (Blueprint $table) {
+            //
+            $table->dropColumn('weight');
+
+        });
+    }
+}

+ 26 - 0
tests/Feature/SearchPaliWbwTest.php

@@ -0,0 +1,26 @@
+<?php
+
+namespace Tests\Feature;
+
+use Illuminate\Foundation\Testing\RefreshDatabase;
+use Illuminate\Foundation\Testing\WithFaker;
+use Tests\TestCase;
+
+class SearchPaliWbwTest extends TestCase
+{
+    /**
+     * A basic feature test example.
+     *
+     * @return void
+     */
+    public function test_store()
+    {
+        $response = $this->post('/api/v2/search-pali-wbw',
+                    [
+                        'view'=>'pali',
+                        'words'=>['jātāni','jātā'],
+                    ]);
+
+        $response->assertOk();
+    }
+}