企業負面信息采集和分級系統設計與實現《網站規劃與設計》期末論文4

5??系統實現

5.1??搭建腳手架

實現系統的第一步是搭建腳手架。通過包管理器composer,可以快速開始自己的應用:

Composer?create-project?Laravel/Laravel?ENICGsys?^5.5

項目的目錄結構如圖5-1所示。

 

圖5-1??項目目錄結構

app文件夾包含項目的模型和控制器,是實現業務邏輯和數據訪問的核心。views文件夾包含前端頁面,是結果展示的核心。routes文件夾中的文件定義了項目的路由,是訪問方法所需的重要文件。

搭建腳手架之后修改配置文件,對數據庫進行簡單的配置和連接。對.env做如下配置:

DB_CONNECTION=mysql

DB_HOST=127.0.0.1

DB_PORT=3306

DB_DATABASE=enicgsys

DB_USERNAME=root

DB_PASSWORD=

5.2??路由規劃

對于系統使用的每一種方法,必須規定至少一個可以定位到它的URI,所以設計一個功能首先需要設計它的路由

/**

*?為前臺頁面設計路由,提供訪問前臺頁面的方法

*/

Route::get('article/{id}',?'HomeController@show');

Route::get('select',?'HomeController@select');

Route::post('select',?'HomeController@select');

Route::get('/','HomeController@index');

Route::get('/home','HomeController@index');

 

/**

*?對后臺管理模塊設置路由組,統一管理同一前綴下的方法訪問

*/

Route::group(['middleware'?=>?'auth',?'namespace'?=>?'Admin',?'prefix'?=>?'admin'],function(){

Route::resource('/','HomeController');

Route::resource('NegativeWords','NegativeWordController');

Route::resource('NegativeInfos','NegativeInfoController');

Route::resource('Spider','SpiderController');

Route::post('Spider/spider','SpiderController@spider');

Route::post('Spider/wenkuDL','SpiderController@wenkuDL');

});

 

5.3??模型的創建與實現

為每一種資源設計一種模型,首先創建模型

php?artisan?make:model?NegativInfo

其他模型創建方法相同。

在Laravel中,一個模型應該繼承自Model類,之后可以通過ORM去操作數據。于是我們設計模型NegativeInfo模型如下:

class?NegativeInfo?extends?Model

{

protected?$table?=?'negative_infos';

protected?$fillable?=?['title','source','time',?'content',?'level'];

}

我們可以通過NegativeInfo中的方法實現對數據庫中negative_infos表的使用。

其他模型的設計方法相同。

5.4??控制器的創建與實現

為每一個功能模塊創建對應的控制器,為每一種資源創建一個對應的模型

創建一個負面信息管理模塊的控制器如下:

php?artisan?make:controller?NegativeInfoController

其他控制器創建過程相同。

5.4.1??NegativeInfoController的設計

NegativeInfoController是模型NegativeInfo對應的控制器,在NegativeInfoController中,應該實現對NegativeInfo的增刪改查等操作的業務邏輯。具體實現如下:

class?NegativeInfoController?extends?Controller

{

/**

????*實現了負面信息管理模塊的入口訪問

????*/

public?function?index()

{

Return?view('admin/NegativeInfo/index')->

withNegativeInfos(NegativeInfo::all());

}

 

/**

????*實現了負面信息管理模塊中,新增和編輯子功能對應頁面的跳轉

????*/

public?function?create()

{

return?view('admin/NegativeInfo/create');

}

 

public?function?edit($id)

{

return?view('admin/NegativeInfo/edit')->

withNegativeInfo(NegativeInfo::find($id));

}

 

/**

????*實現了負面信息管理模塊中,新增信息子功能對應的方法

????*/

public?function?store(Request?$request)

{

$NegativeInfo?=?new?NegativeInfo;

$NegativeInfo->title?=?$request->get('title');

$NegativeInfo->source?=?$request->get('source');

$NegativeInfo->time?=?$request->get('time');

$NegativeInfo->content?=?$request->get('content');

$NegativeInfo->company?=?$request->get('company');

$NegativeInfo->level?=?0;

if?($NegativeInfo->save())?{

return?redirect('admin/NegativeInfos');

}?else?{

return?redirect()->back()->withInput()->

withErrors('保存失?。?);

}

}

 

/**

????*實現了負面信息管理模塊中,更新信息子功能對應的方法

????*/

public?function?update(Request?$request,$id)

{

$NegativeInfo?=?NegativeInfo::find($id);

$NegativeInfo->title?=?$request->get('title');

$NegativeInfo->source?=?$request->get('source');

$NegativeInfo->time?=?$request->get('time');

$NegativeInfo->content?=?$request->get('content');

$NegativeInfo->company?=?$request->get('company');

$NegativeInfo->level?=?$request->get('level');

if?($NegativeInfo->save())?{

return?redirect('admin/NegativeInfos');

}?else?{

return?redirect()->back()->withInput()->

withErrors('信息修改失??!');

}

}

 

/**

????*實現了負面信息管理模塊中,刪除信息子功能對應的方法

????*/

public?function?destroy($id)

{

if(NegativeInfo::find($id)->delete()){

return?redirect('admin/NegativeInfos');

};

return?redirect()->back()->withInput()->withErrors('刪除失敗!');

}

}

5.4.2??NegativeWordController的設計

NegativeWordController是模型NegativeWord對應的控制器,在NegativeWordController中,應該實現對NegativeWord的增刪改查等操作的業務邏輯。此部分業務邏輯與NegativeInfoController相似,在此不做贅述,詳細內容見附錄。

5.4.3??SpiderController的設計

SpiderController是本系統的核心部分。其實現了一個基于搜索引擎的網絡爬蟲和負面信息分級系統。具體設計如下:

/**

?*?爬蟲模塊入口

?*?@param?Request?$request?從表單獲取的請求

?*/

public?function?spider(Request?$request)

{

$bashUrl?=?'http://www.baidu.com/s?';

$company?=?$request->keyWords;

$keyWords?=?$company."虧損?抄襲?違約?處罰";

$site?=?array("sina.com.cn",?"163.com");

$params?=?"wd=$keyWords%20site:".$site[0]."&lm=100&rn=50";

$url?=?$bashUrl.$params;

echo?$bashUrl.$params."<br>";

 

try{

$htmlBaidu?=?$this->get_html($url);

}

catch(Exception?$e){

echo?"yichang".$e->getMessage()."\n";

}

file_put_contents(base_path('resources/docs/')

.'php_'."$keyWords.html",?$htmlBaidu->html());

$urlFile?=?base_path('resources/docs/')

.'url_'.'php_'."$keyWords.html";

$urlFileContent?=?'';

$urlList?=$this->get_url($htmlBaidu);

$urlList->each(function($node,$i)?use(&$fileFlow,$company,&$urlFileContent){

$urlFileContent?.=?$node->html()."\n";

$url?=?$node->text();

echo?"url$i:".$url.'<br>';

//獲取到搜索結果鏈接指向的頁面

$html?=?$this->get_html($url);

file_put_contents(base_path('resources/docs/')

.'php_'."url$i"."_"."$company.html",?$html->html());

$this->dom_resovle_sina($html,?$company);

});

}

 

/**

?*?獲取所請求的地址文本根節點

?*?@param?string?$url?想要請求的地址

?*?@return?Crawler

*/

private?function?get_html($url)

{

$goutteClient?=?new?GoutteClient();

$allow_redirects?=?[

'max'?????????????=>?10,????????//?allow?at?most?10?redirects.

'strict'??????????=>?false,????//?use?"strict"?RFC?compliant???//redirects.

'referer'?????????=>?true,??????//?add?a?Referer?header

'protocols'???????=>?['https','http'],?//?only?allow?https?URLs

'on_redirect'?????=>?'',

'track_redirects'?=>?true

];

$headers?=?['User-Agent'?=>?'Mozilla/5.0(Macintosh;IntelMacOSX10_7_0)AppleWebKit/535.11(KHTML,likeGecko)Chrome/17.0.963.56Safari/535.11',

];

$crawler?=?$goutteClient->request('GET',?$url,?[

'header'????????????=>?$headers,

'allow_redirects'???=>?$allow_redirects

]);

return?$crawler;

}

 

/**

?*?從HTML獲取其中的鏈接

?*?@param?string?$html?html文本

?*?@return?list

?*/

private?function?get_url(Crawler?$crawler)

{

$XPath?=?"http://h3[@class='t']/a/@href";

$urlList?=?$crawler->filterXPath($XPath);

if(is_null($urlList)){

throw?new?Exception("沒有解析到可用鏈接");

}

return?$urlList;

}

 

/**

?*?分析頁面,提取標題,時間,正文和來源

?*?@param?Crawler?$html?需要被解析的頁面

?*/

private?function?dom_resovle_sina(Crawler?$html,?$company)

{

$titleXPath?=?"http://h1[@class='main-title'?or?@id='artibodyTitle'?or?@id='main_title']";

$timeXPath?=?"http://span[@class='date'?or?@id='pub_date'?or

@class='titer']";

$sourceXPath?=?"http://*[contains(@class,'source')?and

not(contains(@class,'date')?or

contains(@class,'time'))?or?@data-

sudaclick='content_media']";

$contentXPath?=?"http://div[@class='article'?or?@id='artibody']";

$title?=?$html->filterXPath($titleXPath);

$time?=?$html->filterXPath($timeXPath);

$content?=?$html->filterXPath($contentXPath);

$source?=?$html->filterXPath($sourceXPath);

//完整性校驗

if(is_null($title->getNode(0))||is_null($time->getNode(0))

||is_null($content->getNode(0))||is_null($source->getNode(0))){

echo?"信息不完整<br>";

}else{

$NegativeInfo?=?new?NegativeInfo;

$NegativeInfo->title?=?$title->html();

$NegativeInfo->source?=?$source->html();

$NegativeInfo->time?=?$time->html();

$NegativeInfo->content?=?$content->html();

$NegativeInfo->company?=?$company;

//重復性校驗

$notExist?=?true;

$negativeInfos?=?NegativeInfo::all();

foreach($negativeInfos?as?$negativeInfo){

if($NegativeInfo->title?==?$negativeInfo->title

||$NegativeInfo->content?==?$negativeInfo->content){

$notExist?=?false;

}

}

if($notExist){

$NegativeInfo->level=$this->get_level($content->text());

//判斷消極還是積極

if($NegativeInfo->level?<=?0){

echo?'<br>不是負面的:'.$NegativeInfo->level.'<br>';

}

else?if($NegativeInfo->save())?{

return?redirect('admin/NegativeInfos');

}?else?{

return?redirect()->back()->withInput()->

withErrors('保存失敗!');

}

}

}

}

 

/**

?*?分析內容,計算負面等級

?*?@param?string?$content?被分析的文本

?*?@return?int?負面等級

?*/

private?function?get_level($content)

{

$text?=?$content;

$client?=?new?AipNlp(APP_ID,?API_KEY,?SECRET_KEY);

$returnResult=?$client->sentimentClassify($text);

echo?"return<br>";

var_dump($returnResult);

echo?"<br>";

foreach(array_keys($returnResult)?as?$key){

if($key?==?'error_msg'){

return?$level?=?-2;

}

}

$negativeLevel?=?$returnResult['items'][0]['negative_prob']?-?0.5;

echo?"<br>negativeLevel:$negativeLevel<br>";

if($negativeLevel?>?0){

$posProb?=?$negativeLevel?*?2;

$level?=?10*?$posProb?*?$returnResult['items'][0]['confidence'];

}

else{

$level?=?-1;

}

echo?'負面等級:';

var_dump($level);

return?(int)$level;

}

5.4.4??HomeController的設計

HomeController實現了用戶查看和篩選負面信息的業務邏輯,具體實現如下:

/**

*展示指定id的負面信息詳情

*/

public?function?show($id)

{

return?view('show')->withNegativeInfo(NegativeInfo::find($id));

}

 

/**

*從請求接受企業信息并從模型篩選后返回給頁面

*/

public?function?select(Request?$request)

{

if(is_null($request->company)){

return?view('home')->withNegativeInfos(NegativeInfo::all());

}

else{

return?view('home')->withNegativeInfos(

NegativeInfo::where('company',?'like',?"%$request->company%")

->get()

);

}

}

5.5??視圖實現

視圖是用戶直接使用和觀看的部分。對于每一個控制器都應該有對應的視圖或視圖組存在。下面描述幾種最主要的視圖的實現。

5.5.1??HomeController下的視圖

  1. home頁面:

Home頁面是用戶進入系統的門戶,核心代碼如圖5-2。

 

圖5-2??home頁面代碼

 

圖5-3??home頁面效果

  1. show頁面

Show頁面是展示負面信息詳情的頁面,核心代碼如圖5-4。

 

圖5-4??show頁面代碼

 

圖5-5??show頁面效果

5.5.2??NegativeInfoController下的視圖

1.index頁面

此頁面是進入負面信息管理頁面的門戶頁面,核心代碼如圖5-6。

 

圖5-6??index頁面代碼

 

圖5-7??index頁面效果

2.create頁面

此頁面是管理員進行新增負詞時訪問的頁面,核心代碼如圖5-8。

 

圖5-8??create頁面代碼

 

圖5-9??create頁面效果

3.edit頁面

此頁面是管理員編輯負面信息時訪問的頁面,核心代碼如圖5-10。

 

圖5-10??edit頁面代碼

 

圖5-11??edit頁面效果

5.5.3??NegativeWordController下的視圖

NegativeWordController下的視圖與NegativeInfoController下的視圖結構類似,在此不做贅述,詳情見附錄。

5.5.4??SpiderController下的視圖

SpiderController下的視圖提供了訪問爬蟲的入口,核心代碼如圖5-12。

 

圖5-12??spider下index頁面代碼

 

圖5-13??index頁面效果

更多
  • 該日志由 于2020年06月03日發表在 未分類 分類下, 你可以發表評論,并在保留原文地址 及作者的情況下引用到你的網站或博客。
  • 本文鏈接: 企業負面信息采集和分級系統設計與實現《網站規劃與設計》期末論文4 | 幫助信息-動天數據
  • 版權所有: 幫助信息-動天數據-轉載請標明出處
  • 【上一篇】 【下一篇】

    0 Comments.