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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
| using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Web;
using System.Text.RegularExpressions;
namespace 网页关键词处理
{
class html
{
private const int cn_max = 8;
private const int cn_min = 2;
private const int en_max = 84;
private const int en_min = 2;
private const string endchars = " ;:,:,..。'“”??_{}/\\·[]()()、…ˉ-!!<>《》〉〈〕〔【】〗〖[]{}『』」「¥—";
private string title;
public string Title
{
get { return title; }
}
private string body;
public string Body
{
get { return body; }
}
private string keywords;
public string Keywords
{
get { return keywords; }
}
private string description;
public string Description
{
get { return description; }
}
public html(string code) {
//获取标题
this.title = this.getTitle(code).Trim();
this.body = this.getBody(code).Trim();
}
//获取body
private string getBody(string code) {
string strfrom = NoHTML(code);
//return strfrom;
int strlength = strfrom.Length;
int chscount, endasc, cx, ca, oldcount=0;
string tempstr, oldchar="";
char endchar;
Dictionary<string,int> strdic = new Dictionary<string,int>();
//中英文分词
for (var i = 0; i < strlength; i++) {
chscount = 0;
for (var j = 1; j < en_max; j++) {
if ((i + j) <= strlength){
tempstr = strfrom.Substring(i, j);
endasc = (int)Encoding.Default.GetBytes(tempstr)[j-1];
endchar = tempstr[j-1];
if (endchars.IndexOf(endchar) != -1 || (endasc >= 0 && endasc <= 47) || (endasc >= 58 && endasc <= 64) || (endasc >= 91 && endasc <= 96))
{
break;
}else{
if(endasc <= 0 || endasc >= 254)chscount++;
if (chscount > cn_max) break;
if (tempstr.Length > 1){
//标题匹配加倍权重
ca = (strlength - strfrom.Replace(tempstr, String.Empty).Length) / tempstr.Length;
cx = this.title.IndexOf(tempstr) != -1? ca*2 : ca;
//丢弃只出现一次的词语
if(cx>0){
if (!strdic.ContainsKey(tempstr)){
strdic.Add(tempstr, cx);
}
if (oldcount > 0 && tempstr.IndexOf(oldchar) == 0){
strdic[oldchar] = oldcount - cx;
//丢弃只出现一次的词语
if(strdic[oldchar]<2)strdic.Remove(oldchar);
}
oldchar = tempstr;
oldcount= cx;
};
}
}
}
}
}
int c = strdic.Count;
string[] a = new string[c];
int[] b = new int[c];
int ti = 0;
string upa="",doa="";
int upb=0,dob=0;
keys[] keys = new keys[c];
strdic.Keys.CopyTo(a,0);
strdic.Values.CopyTo(b,0);
for(int i=c-1;i>=0;i--){
if(i>0){
upa = a[i];
upb = b[i];
doa = a[i-1];
dob = b[i-1];
if(doa.IndexOf(upa)==doa.Length-upa.Length){
upb = upb - dob;
}
if(upb>1){//丢弃只出现一次的词语
keys[ti] = new keys();
keys[ti].Str = upa;
keys[ti].Count=upb;
ti++;
}
}else{
keys[ti] = new keys();
keys[ti].Str = doa;
keys[ti].Count=dob;
}
}
//按匹配次数排序
bool py = true;
int pyd, pye, pyf;
string pys;
c = ti;
while(py){
py = false;
for(int i=0;i<c;i++){
if ((i + 1) >= c)break;
pyd = keys[i].Count;
pye = keys[i+1].Count;
if(pyd<pye){
pyf = keys[i].Count;
pys = keys[i].Str;
keys[i].Count = keys[i+1].Count;
keys[i].Str = keys[i+1].Str;
keys[i+1].Count = pyf;
keys[i+1].Str = pys;
py = true;
}
}
}
string keystring="";
for (int i = 0; i < c; i++) {
keystring += keys[i].Str;
if (keystring.Length >= 255)
break;
else
keystring += " ";
}
if (keystring.Length > 255) keystring = keystring.Substring(0, 255);
return keystring;
}
public string NoHTML(string Htmlstring)
{
//删除脚本
Htmlstring = Regex.Replace(Htmlstring, @"(?is)<script.*?</script>"," ");
Htmlstring = Regex.Replace(Htmlstring, @"(?is)<style.*?</style>", " ");
Htmlstring = Regex.Replace(Htmlstring, @"(?is)<!--.*?-->", " ");
Htmlstring = Regex.Replace(Htmlstring, @"(?is)<.*?>", " ");
Htmlstring = HttpUtility.HtmlDecode(Htmlstring);
//删除符号
//Htmlstring = Htmlstring.Replace("(", " ");
//Htmlstring = Htmlstring.Replace(")", " ");
//Htmlstring = Htmlstring.Replace("|", " ");
//Htmlstring = Htmlstring.Replace(",", " ");
//Htmlstring = Htmlstring.Replace("@", " ");
//Htmlstring = Htmlstring.Replace(";", " ");
//Htmlstring = Htmlstring.Replace(".", " ");
//Htmlstring = Htmlstring.Replace("]", " ");
//Htmlstring = Htmlstring.Replace("[", " ");
//Htmlstring = Htmlstring.Replace(">", " ");
//Htmlstring = Htmlstring.Replace("<", " ");
//Htmlstring = Htmlstring.Replace("*", " ");
Htmlstring = Regex.Replace(Htmlstring, @"(?is)(\s+|\n+)", " ");
return Htmlstring;
}
//获取描述
private string getDescription(string code) {
Regex r = new Regex("(?is)(?<=<Title>).*?(?=</Title>)");
Match m = r.Match(code);
return m.Success ? m.Value : "";
}
//获取关键词
private string getKeywords(string code) {
Regex r = new Regex("(?is)(?<=<Title>).*?(?=</Title>)");
Match m = r.Match(code);
return m.Success ? m.Value : "";
}
//获取页面标题
private string getTitle(string code) {
Regex r = new Regex("(?is)(?<=<Title>).*?(?=</Title>)");
Match m = r.Match(code);
string tit = m.Success? m.Value:"";
if (tit.Length > 50) tit = tit.Substring(0, 50);
return tit;
}
}
} |
Leave a Reply