Skip to content

add keywords #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions app/Events/ExtractKeywordsEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App\Events;

use App\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class ExtractKeywordsEvent extends Event
{
use SerializesModels;

public $params;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct($params)
{
$this->params = $params;
}

/**
* Get the channels the event should be broadcast on.
*
* @return array
*/
public function broadcastOn()
{
return [];
}
}
23 changes: 23 additions & 0 deletions app/Helpers/ExtractKeywords.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Helpers;

use Fukuball\Jieba\Jieba;
use Fukuball\Jieba\Finalseg;
use Fukuball\Jieba\JiebaAnalyse;

class ExtractKeywords
{
const TOP_K = 10;

public static function getKeywords($content)
{
Jieba::init();
Finalseg::init();
JiebaAnalyse::init();

$keywords = JiebaAnalyse::extractTags($content, self::TOP_K);

return $keywords;
}
}
76 changes: 76 additions & 0 deletions app/Listeners/ExtractKeywordsListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace App\Listeners;

use App\Events\ExtractKeywordsEvent;
use App\TopicKeywords;
use App\TimelineKeyWords;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class ExtractKeywordsListener
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}

/**
* Handle the event.
*
* @param ExtractKeywordsEvent $event
* @return void
*/
public function handle(ExtractKeywordsEvent $event)
{
$params = $event->params;

$type = $params['type'];
$content = $params['content'];
$id = $params['id'];

try {
switch ($type) {
case 1:
$this->storeTimelineKeywords($content, $id);
break;
case 2:
$this->storeTopicKeywords($content, $id);
break;
}
} catch (Exception $e) {
\Log::error($e->getMessage());
}
}

private function storeTimelineKeywords($content, $id)
{
$keywords = \App\Helpers\ExtractKeywords::getKeywords($content);

foreach ($keywords as $keyword => $score) {
$timelineKeyword = new TimelineKeyWords;
$timelineKeyword->timeline_id = $id;
$timelineKeyword->keyword = $keyword;
$timelineKeyword->score = $score;
$timelineKeyword->save();
}
}

private function storeTopicKeywords($content, $id)
{
$keywords = \App\Helpers\ExtractKeywords::getKeywords($content);

foreach ($keywords as $keyword => $score) {
$topicKeyword = new TopicKeywords;
$topicKeyword->topic_id = $id;
$topicKeyword->keyword = $keyword;
$topicKeyword->score = $score;
$topicKeyword->save();
}
}
}
3 changes: 3 additions & 0 deletions app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class EventServiceProvider extends ServiceProvider
'App\Events\TriggerNoticeEvent' => [
'App\Listeners\TriggerNoticeListener',
],
'App\Events\ExtractKeywordsEvent' => [
'App\Listeners\ExtractKeywordsListener',
],
];

/**
Expand Down
16 changes: 16 additions & 0 deletions app/TimelineKeywords.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class TimelineKeywords extends Model
{
use SoftDeletes;

public function timelines()
{
return $this->belongsToMany(Timeline::clsss , 'timeline_keywords', 'timeline_id', 'timeline_keyword_id');
}
}
16 changes: 16 additions & 0 deletions app/TopicKeywords.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class TopicKeywords extends Model
{
use SoftDeletes;

public function timelines()
{
return $this->belongsToMany(Timeline::clsss , 'timeline_keywords', 'timeline_id', 'timeline_keyword_id');
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"zgldh/qiniu-laravel-storage": "^0.6.2",
"jpush/jpush": "v3.5.*",
"barryvdh/laravel-cors": "^0.8.6",
"asm89/stack-cors": "^1.0.0"
"asm89/stack-cors": "^1.0.0",
"fukuball/jieba-php": "dev-master"
},
"require-dev": {
"fzaninotto/faker": "~1.4",
Expand Down
56 changes: 54 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateTimelineKeywordsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('timeline_keywords', function (Blueprint $table) {
$table->increments('id');
$table->integer('timeline_id')->index()->unsigned();
$table->string('keyword')->comment('关键词');
$table->double('score')->comment('权重');
$table->softDeletes();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('timeline_keywords');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateTopicKeywordsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('topic_keywords', function (Blueprint $table) {
$table->increments('id');
$table->integer('topic_id')->index()->unsigned();
$table->string('keyword')->comment('关键词');
$table->double('score')->comment('权重');
$table->softDeletes();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('topic_keywords');
}
}