企業(yè)負(fù)面信息采集和分級(jí)系統(tǒng)設(shè)計(jì)與實(shí)現(xiàn)《網(wǎng)站規(guī)劃與設(shè)計(jì)》期末論文4
5??系統(tǒng)實(shí)現(xiàn)
5.1??搭建腳手架
實(shí)現(xiàn)系統(tǒng)的第一步是搭建腳手架。通過(guò)包管理器composer,可以快速開(kāi)始自己的應(yīng)用:
Composer?create-project?Laravel/Laravel?ENICGsys?^5.5
項(xiàng)目的目錄結(jié)構(gòu)如圖5-1所示。
圖5-1??項(xiàng)目目錄結(jié)構(gòu)
app文件夾包含項(xiàng)目的模型和控制器,是實(shí)現(xiàn)業(yè)務(wù)邏輯和數(shù)據(jù)訪問(wèn)的核心。views文件夾包含前端頁(yè)面,是結(jié)果展示的核心。routes文件夾中的文件定義了項(xiàng)目的路由,是訪問(wèn)方法所需的重要文件。
搭建腳手架之后修改配置文件,對(duì)數(shù)據(jù)庫(kù)進(jìn)行簡(jiǎn)單的配置和連接。對(duì).env做如下配置:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=enicgsys
DB_USERNAME=root
DB_PASSWORD=
5.2??路由規(guī)劃
對(duì)于系統(tǒng)使用的每一種方法,必須規(guī)定至少一個(gè)可以定位到它的URI,所以設(shè)計(jì)一個(gè)功能首先需要設(shè)計(jì)它的路由
/**
*?為前臺(tái)頁(yè)面設(shè)計(jì)路由,提供訪問(wèn)前臺(tái)頁(yè)面的方法
*/
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');
/**
*?對(duì)后臺(tái)管理模塊設(shè)置路由組,統(tǒng)一管理同一前綴下的方法訪問(wèn)
*/
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??模型的創(chuàng)建與實(shí)現(xiàn)
為每一種資源設(shè)計(jì)一種模型,首先創(chuàng)建模型
php?artisan?make:model?NegativInfo
其他模型創(chuàng)建方法相同。
在Laravel中,一個(gè)模型應(yīng)該繼承自Model類(lèi),之后可以通過(guò)ORM去操作數(shù)據(jù)。于是我們?cè)O(shè)計(jì)模型NegativeInfo模型如下:
class?NegativeInfo?extends?Model
{
protected?$table?=?'negative_infos';
protected?$fillable?=?['title','source','time',?'content',?'level'];
}
我們可以通過(guò)NegativeInfo中的方法實(shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)中negative_infos表的使用。
其他模型的設(shè)計(jì)方法相同。
5.4??控制器的創(chuàng)建與實(shí)現(xiàn)
為每一個(gè)功能模塊創(chuàng)建對(duì)應(yīng)的控制器,為每一種資源創(chuàng)建一個(gè)對(duì)應(yīng)的模型
創(chuàng)建一個(gè)負(fù)面信息管理模塊的控制器如下:
php?artisan?make:controller?NegativeInfoController
其他控制器創(chuàng)建過(guò)程相同。
5.4.1??NegativeInfoController的設(shè)計(jì)
NegativeInfoController是模型NegativeInfo對(duì)應(yīng)的控制器,在NegativeInfoController中,應(yīng)該實(shí)現(xiàn)對(duì)NegativeInfo的增刪改查等操作的業(yè)務(wù)邏輯。具體實(shí)現(xiàn)如下:
class?NegativeInfoController?extends?Controller
{
/**
????*實(shí)現(xiàn)了負(fù)面信息管理模塊的入口訪問(wèn)
????*/
public?function?index()
{
Return?view('admin/NegativeInfo/index')->
withNegativeInfos(NegativeInfo::all());
}
/**
????*實(shí)現(xiàn)了負(fù)面信息管理模塊中,新增和編輯子功能對(duì)應(yīng)頁(yè)面的跳轉(zhuǎn)
????*/
public?function?create()
{
return?view('admin/NegativeInfo/create');
}
public?function?edit($id)
{
return?view('admin/NegativeInfo/edit')->
withNegativeInfo(NegativeInfo::find($id));
}
/**
????*實(shí)現(xiàn)了負(fù)面信息管理模塊中,新增信息子功能對(duì)應(yīng)的方法
????*/
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('保存失??!');
}
}
/**
????*實(shí)現(xiàn)了負(fù)面信息管理模塊中,更新信息子功能對(duì)應(yīng)的方法
????*/
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('信息修改失?。?);
}
}
/**
????*實(shí)現(xiàn)了負(fù)面信息管理模塊中,刪除信息子功能對(duì)應(yīng)的方法
????*/
public?function?destroy($id)
{
if(NegativeInfo::find($id)->delete()){
return?redirect('admin/NegativeInfos');
};
return?redirect()->back()->withInput()->withErrors('刪除失??!');
}
}
5.4.2??NegativeWordController的設(shè)計(jì)
NegativeWordController是模型NegativeWord對(duì)應(yīng)的控制器,在NegativeWordController中,應(yīng)該實(shí)現(xiàn)對(duì)NegativeWord的增刪改查等操作的業(yè)務(wù)邏輯。此部分業(yè)務(wù)邏輯與NegativeInfoController相似,在此不做贅述,詳細(xì)內(nèi)容見(jiàn)附錄。
5.4.3??SpiderController的設(shè)計(jì)
SpiderController是本系統(tǒng)的核心部分。其實(shí)現(xiàn)了一個(gè)基于搜索引擎的網(wǎng)絡(luò)爬蟲(chóng)和負(fù)面信息分級(jí)系統(tǒng)。具體設(shè)計(jì)如下:
/**
?*?爬蟲(chóng)模塊入口
?*?@param?Request?$request?從表單獲取的請(qǐng)求
?*/
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>';
//獲取到搜索結(jié)果鏈接指向的頁(yè)面
$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);
});
}
/**
?*?獲取所請(qǐng)求的地址文本根節(jié)點(diǎn)
?*?@param?string?$url?想要請(qǐng)求的地址
?*?@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("沒(méi)有解析到可用鏈接");
}
return?$urlList;
}
/**
?*?分析頁(yè)面,提取標(biāo)題,時(shí)間,正文和來(lái)源
?*?@param?Crawler?$html?需要被解析的頁(yè)面
?*/
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);
//完整性校驗(yàn)
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;
//重復(fù)性校驗(yàn)
$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>不是負(fù)面的:'.$NegativeInfo->level.'<br>';
}
else?if($NegativeInfo->save())?{
return?redirect('admin/NegativeInfos');
}?else?{
return?redirect()->back()->withInput()->
withErrors('保存失??!');
}
}
}
}
/**
?*?分析內(nèi)容,計(jì)算負(fù)面等級(jí)
?*?@param?string?$content?被分析的文本
?*?@return?int?負(fù)面等級(jí)
?*/
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?'負(fù)面等級(jí):';
var_dump($level);
return?(int)$level;
}
5.4.4??HomeController的設(shè)計(jì)
HomeController實(shí)現(xiàn)了用戶查看和篩選負(fù)面信息的業(yè)務(wù)邏輯,具體實(shí)現(xiàn)如下:
/**
*展示指定id的負(fù)面信息詳情
*/
public?function?show($id)
{
return?view('show')->withNegativeInfo(NegativeInfo::find($id));
}
/**
*從請(qǐng)求接受企業(yè)信息并從模型篩選后返回給頁(yè)面
*/
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??視圖的實(shí)現(xiàn)
視圖是用戶直接使用和觀看的部分。對(duì)于每一個(gè)控制器都應(yīng)該有對(duì)應(yīng)的視圖或視圖組存在。下面描述幾種最主要的視圖的實(shí)現(xiàn)。
5.5.1??HomeController下的視圖
- home頁(yè)面:
Home頁(yè)面是用戶進(jìn)入系統(tǒng)的門(mén)戶,核心代碼如圖5-2。
圖5-2??home頁(yè)面代碼
圖5-3??home頁(yè)面效果
- show頁(yè)面
Show頁(yè)面是展示負(fù)面信息詳情的頁(yè)面,核心代碼如圖5-4。
圖5-4??show頁(yè)面代碼
圖5-5??show頁(yè)面效果
5.5.2??NegativeInfoController下的視圖
1.index頁(yè)面
此頁(yè)面是進(jìn)入負(fù)面信息管理頁(yè)面的門(mén)戶頁(yè)面,核心代碼如圖5-6。
圖5-6??index頁(yè)面代碼
圖5-7??index頁(yè)面效果
2.create頁(yè)面
此頁(yè)面是管理員進(jìn)行新增負(fù)詞時(shí)訪問(wèn)的頁(yè)面,核心代碼如圖5-8。
圖5-8??create頁(yè)面代碼
圖5-9??create頁(yè)面效果
3.edit頁(yè)面
此頁(yè)面是管理員編輯負(fù)面信息時(shí)訪問(wèn)的頁(yè)面,核心代碼如圖5-10。
圖5-10??edit頁(yè)面代碼
圖5-11??edit頁(yè)面效果
5.5.3??NegativeWordController下的視圖
NegativeWordController下的視圖與NegativeInfoController下的視圖結(jié)構(gòu)類(lèi)似,在此不做贅述,詳情見(jiàn)附錄。
5.5.4??SpiderController下的視圖
SpiderController下的視圖提供了訪問(wèn)爬蟲(chóng)的入口,核心代碼如圖5-12。
圖5-12??spider下index頁(yè)面代碼
圖5-13??index頁(yè)面效果
0 Comments.