forked from Moidea/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plugin.php
executable file
·135 lines (129 loc) · 4.97 KB
/
Plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
* 标签自动生成插件
*
* @package AutoTags
* @author DT27
* @version 1.0.0
* @link https://dt27.org
*/
class AutoTags_Plugin implements Typecho_Plugin_Interface
{
/**
* 激活插件方法,如果激活失败,直接抛出异常
*
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function activate()
{
Typecho_Plugin::factory('Widget_Contents_Post_Edit')->write = array('AutoTags_Plugin', 'write');
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @static
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function deactivate(){}
/**
* 获取插件配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form 配置面板
* @return void
*/
public static function config(Typecho_Widget_Helper_Form $form)
{
$isActive = new Typecho_Widget_Helper_Form_Element_Radio('isActive',
array(
'1' => '是',
'0' => '否',
),'1', _t('是否启用标签自动提取功能'), _t('自动提取功能在文章已存在标签时不生效.'));
$form->addInput($isActive);
}
/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form){}
/**
* 发布文章时自动提取标签
*
* @access public
* @return void
*/
public static function write($contents, $edit)
{
$html = $contents['text'];
$isMarkdown = (0 === strpos($html, '<!--markdown-->'));
if($isMarkdown){
$html = Markdown::convert($html);
}
$text = str_replace("\n", '', trim(strip_tags(html_entity_decode($html))));
$autoTags = Typecho_Widget::widget('Widget_Options')->plugin('AutoTags');
//插件启用,且未手动设置标签
if($autoTags->isActive == 1 && !$contents['tags']) {
Typecho_Widget::widget('Widget_Metas_Tag_Admin')->to($tags);
foreach($tags->stack as $tag){
$tagNames[] = $tag['name'];
}
//str_replace("\n", '', trim(strip_tags($contents['text'])))
//过滤 html 标签等无用内容
$postString = json_encode($text);
$ch = curl_init('http://api.bosonnlp.com/tag/analysis?space_mode=0&oov_level=0&t2s=0');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS,$postString);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array(
'Content-Type: application/json',
'Accept: application/json',
'X-Token: fpm1fDvA.5220.GimJs8QvViSK'
)
);
$result = curl_exec($ch);
curl_close($ch);
$result = json_decode($result);
$ignoreTag = array('w', 'wkz', 'wky', 'wyz', 'wyy', 'wj', 'ww', 'wt', 'wd', 'wf', 'wn', 'wm', 'ws', 'wp', 'wb', 'wh', 'email', 'tel', 'id', 'ip', 'url', 'o', 'y', 'u', 'uzhe', 'ule', 'ugou', 'ude', 'usou', 'udeng', 'uyy', 'udh', 'uzhi', 'ulian', 'c', 'p', 'pba', 'pbei', 'd', 'dl', 'q', 'm', 'r', 'z', 'b', 'bl', 'a', 'ad', 'an', 'al', 'v', 'vd', 'vshi', 'vyou', 'vl', 'f', 's', 't', 'nl');
$sourceTags = array();
foreach($result[0]->tag as $key => $tag){
if(!in_array($tag, $ignoreTag)){
if(in_array($result[0]->word[$key], $tagNames)){
if(in_array($result[0]->word[$key], $sourceTags)) continue;
$sourceTags[] = $result[0]->word[$key];
}
}
}
$contents['tags'] = implode(',', array_unique($sourceTags));
if(count($contents['tags'])<3){
$ch = curl_init('http://api.bosonnlp.com/keywords/analysis?top_k=5');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS,$postString);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array(
'Content-Type: application/json',
'Accept: application/json',
'X-Token: fpm1fDvA.5220.GimJs8QvViSK'
)
);
$result = curl_exec($ch);
curl_close($ch);
$result = json_decode($result);
foreach($result as $re){
$a[] = $re[1];
}
$contents['tags'] = $contents['tags']?$contents['tags'].','.implode(',', $a):implode(',', $a);
}
}
return $contents;
}
}