6
6
7
7
import java .sql .*;
8
8
import java .util .ArrayList ;
9
+ import java .util .HashSet ;
9
10
import java .util .List ;
10
11
11
12
import static java .lang .Integer .min ;
@@ -108,27 +109,28 @@ public static List<String> getTag(List<Tag> tagList,ArticleBean articleBean)
108
109
{
109
110
for (Tag tag :tagList )
110
111
{
111
- String default_tag = Word2PinYin (tag .getName ());
112
- String desc [] = tag .getDescription ().split (" " );
113
- String now_tag = Word2PinYin (temp_tag [i ]);
114
- if (StringHasChinese (tag .getName ()) || StringHasChinese (temp_tag [i ]))
115
- {
116
- if (editDistance (default_tag ,now_tag ) <3 )
112
+
113
+ String default_tag = Word2PinYin (tag .getName ());
114
+ String desc [] = tag .getDescription ().split (" " );
115
+ String now_tag = Word2PinYin (temp_tag [i ]);
116
+ if (StringHasChinese (tag .getName ()) || StringHasChinese (temp_tag [i ]))
117
117
{
118
+ if (editDistance (default_tag ,now_tag ) <3 )
119
+ {
118
120
119
- resultTag .add (tag .getName ());
120
- continue ;
121
+ resultTag .add (tag .getName ());
122
+ continue ;
123
+ }
121
124
}
122
- }
123
- else
124
- {
125
- if (editDistance (default_tag ,now_tag ) ==0 )
125
+ else
126
126
{
127
+ if (editDistance (default_tag ,now_tag ) ==0 )
128
+ {
127
129
128
- resultTag .add (tag .getName ());
129
- continue ;
130
+ resultTag .add (tag .getName ());
131
+ continue ;
132
+ }
130
133
}
131
- }
132
134
133
135
134
136
for (int k = 0 ; k <desc .length ; k ++)
@@ -153,52 +155,56 @@ public static List<String> getTag(List<Tag> tagList,ArticleBean articleBean)
153
155
}
154
156
155
157
158
+
159
+
160
+
156
161
}
157
162
}
158
163
}
159
164
160
165
for (Tag tag :tagList )
161
166
{
162
- String default_tag = Word2PinYin (tag .getName ());
163
- String desc [] = tag .getDescription ().split (" " );
164
- if (clean_content .toLowerCase ().contains (default_tag .toLowerCase ()))
165
- {
166
- resultTag .add (tag .getName ());
167
- continue ;
168
- }
169
- else if (title .toLowerCase ().contains (default_tag .toLowerCase ()))
170
- {
171
- resultTag .add (default_tag );
172
- continue ;
173
- }else
174
- {
175
- for (int k = 0 ; k <desc .length ; k ++)
167
+
168
+ String default_tag = Word2PinYin (tag .getName ());
169
+ String desc [] = tag .getDescription ().split (" " );
170
+ if (clean_content .toLowerCase ().contains (default_tag .toLowerCase ()))
176
171
{
177
- if (clean_content .toLowerCase ().contains (desc [k ].toLowerCase ()) && !desc [k ].equals ("" ))
178
- {
179
- System .out .println (clean_content .toLowerCase ());
180
- System .out .println (desc [k ].toLowerCase ());
181
- resultTag .add (tag .getName ());
182
- continue ;
183
- }
172
+ resultTag .add (tag .getName ());
173
+ continue ;
174
+ }
175
+ else if (title .toLowerCase ().contains (default_tag .toLowerCase ()))
176
+ {
177
+ resultTag .add (tag .getName ());
178
+ continue ;
179
+ }else
180
+ {
181
+ for (int k = 0 ; k <desc .length ; k ++)
182
+ {
183
+ if (clean_content .toLowerCase ().contains (desc [k ].toLowerCase ()) && !desc [k ].equals ("" ))
184
+ {
185
+
186
+ resultTag .add (tag .getName ());
187
+ continue ;
188
+ }
189
+ }
184
190
}
185
- }
191
+
192
+
186
193
187
194
}
188
195
189
196
}
190
- return resultTag ;
197
+ return new ArrayList < String >( new HashSet < String >( resultTag )) ;
191
198
192
199
}
193
- //求编辑距离
200
+
201
+ //求编辑距离 利用动态规划
194
202
public static int editDistance (String str1 , String str2 ) {
195
203
Preconditions .checkNotNull (str1 );
196
204
Preconditions .checkNotNull (str2 );
197
205
198
206
int len1 = str1 .length ();
199
207
int len2 = str2 .length ();
200
-
201
- // len1+1, len2+1, because finally return dp[len1][len2]
202
208
int [][] dp = new int [len1 + 1 ][len2 + 1 ];
203
209
204
210
for (int i = 0 ; i <= len1 ; i ++) {
@@ -218,7 +224,7 @@ public static int editDistance(String str1, String str2) {
218
224
//update dp value for +1 length
219
225
dp [i + 1 ][j + 1 ] = dp [i ][j ];
220
226
} else {
221
- dp [i + 1 ][j + 1 ] = 1 + min (dp [i +1 ][j ], min (dp [i ][j +1 ], dp [i ][j ])); // 这里不需要递归实现了
227
+ dp [i + 1 ][j + 1 ] = 1 + min (dp [i +1 ][j ], min (dp [i ][j +1 ], dp [i ][j ]));
222
228
}
223
229
}
224
230
}
@@ -297,8 +303,18 @@ public static void main(String [] args)
297
303
{
298
304
str += s +"," ;
299
305
}
300
- System .out .println (id +" " +tags );
301
- getMysqlData .UpdateArticle (id ,str ,clean_content ,keyword ,summary );
306
+ if (str .length () != 0 )
307
+ {
308
+ System .out .println (id +" " +tags );
309
+ System .out .println (str .substring (0 ,str .length ()-1 ));
310
+ getMysqlData .UpdateArticle (id ,str .substring (0 ,str .length ()-1 ),clean_content ,keyword ,summary );
311
+ }else
312
+ {
313
+ System .out .println (id +" " +tags );
314
+ System .out .println (str );
315
+ getMysqlData .UpdateArticle (id ,str ,clean_content ,keyword ,summary );
316
+ }
317
+
302
318
303
319
}
304
320
}
0 commit comments