在编辑文章中,很多时候都需要自动根据文章内容获取关键字的功能,因此,本文主要是说明如何在yii中使用织梦开源的分词算法编写一个独立的扩展,可以在不同的模块中使用,步骤如下:
1 到这里下载其他朋友整理的织梦分词算法库;
2 解压下载的分词库并放到yii extensions/fenci目录下面;
3 在extensions/fenci添加FenciConnectorAction.php和GetTagsInput.php;
4 在对应的module中添加ConnectorController.php,用于实现在类中调用外部action,即是用于调用extensions/fenci中的FenciConnectorAction.php;
下面说明如何编写需要添加的文件的代码:
1 FenciConnectorAction.php 提供给需要调用的模块调用的外部action,用于根据内容获取分词
<?php class FenciConnectorAction extends CAction { public $contents = ''; public function run(){ require_once(dirname(__FILE__) . '/SplitWord.class.php'); $cfg_soft_lang='utf-8'; $sp = new SplitWord($cfg_soft_lang, $cfg_soft_lang); $sp->SetSource($this->contents, $cfg_soft_lang, $cfg_soft_lang); $sp->SetResultType(3); $sp->StartAnalysis(TRUE); $keywords = $sp->GetFinallyResult(' '); $keywords = preg_replace("/[ ]{1,}/", ",", trim($keywords)); echo $keywords; } } ?>
2 GetTagsInput.php 用于在form表单中调用自动生成表单元素和绑定ajax调用分词action的js
<?php class GetTagsInput extends CInputWidget { public $url = ''; public $connectorRoute = false; public $contentAttribute = ''; public function init() { if (empty($this->connectorRoute)) throw new CException('$connectorRoute must be set!'); $this->url = Yii::app()->createUrl($this->connectorRoute); } public function run() { list($name, $id) = $this->resolveNameID(); if (isset($this->htmlOptions['id'])) $id = $this->htmlOptions['id']; else $this->htmlOptions['id'] = $id; if (isset($this->htmlOptions['name'])) $name = $this->htmlOptions['name']; else $this->htmlOptions['name'] = $name; $contHtmlOptions = $this->htmlOptions; $contHtmlOptions['id'] = $id . 'container'; echo CHtml::openTag('div', $contHtmlOptions); $inputOptions = array('id' => $id, 'style' => 'float:left;', 'size' => '80'); if ($this->hasModel()) echo CHtml::activeTextField($this->model, $this->attribute, $inputOptions); else echo CHtml::textField($name, $this->value, $inputOptions); echo CHtml::button('自动获取', array('id' => $id . 'autoGenerate', 'class' => 'btn')); echo CHtml::closeTag('div'); $content_id = $this->model->getModelClass().'_'.$this->contentAttribute; $object_id = $this->model->getModelClass().'_'.$this->attribute; $script = <<<EOD window.autoGetTags = function(){ var content_id = '$content_id'; var object_id = '$object_id'; $.ajax({ url: '$this->url', type:'post', cache:false, data:{ contents : $("#"+content_id).val() }, success: function(datas, textStatus, xhr){ $("#"+object_id).val(datas) } }); } EOD; $cs = Yii::app()->getClientScript(); $cs->registerScript('GetTagsInput#global', $script); $js = '$("#' . $id . 'autoGenerate").click(function(){window.autoGetTags()});'; $cs->registerScript('GetTagsInput#' . $id, $js); } } ?>
3 ConnectorController.php 用于实现外部调用扩展中的FenciConnectorAction.php
<?php class ConnectorController extends CController { public function actions() { return array( 'fenci' => array( 'class' => 'ext.fenci.FenciConnectorAction', 'contents' => isset($_POST['contents'])?$_POST['contents']:'' ) ); } } ?>
4 最终在表单中的调用方式:
<tr> <td width="150"><?php echo $form->labelEx($model,'tags'); ?></td> <td> <?php $this->widget('ext.fenci.GetTagsInput', array( 'model' => $model, 'attribute' => 'tags',//分词对应的字段 'contentAttribute' => 'title',//指定从哪个字段值获取分词 'connectorRoute' => 'admin/connector/fenci',//设置调用外部action 即是扩展获取分词的action )); ?> </td> </tr>
5 效果图:
分词扩展的目录结构如下:
点击自动获取之后就可以自动根据绑定的关键字数据源得到最终的分词结果并显示在目标字段上面,不过这里对于返回的非词语需要再处理一下。
这里附上如何在yii的controller中调用外部action的说明供作参考
如果需要转载,请注明来源,支持原创,多谢合作!