如何用JSoup和diffator算法评估两段HTML文档的相似度?

2026-04-15 08:013阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计128个文字,预计阅读时间需要1分钟。

如何用JSoup和diffator算法评估两段HTML文档的相似度?

java/** * 从HTML中提取纯文本内容 * @param HTML内容 * @return 提取的纯文本 */public static String getPlainText(String ) { if (StringUtils.isBlank()) { return ; } Element ebody=Jsoup.parseBodyFragment().body(); return ebody.select(code).text();}

如何用JSoup和diffator算法评估两段HTML文档的相似度?

gistfile1.txt

/** * 从一段HTML中萃取纯文本 * @param html * @return */ public static String getPlainText(String html){ if(StringUtils.isBlank(html)) return ""; Element ebody = Jsoup.parseBodyFragment(html).body(); ebody.select("code").remove(); ebody.select("pre").remove(); ebody.select("img").remove(); return ebody.text(); } /** * 计算两篇内容的相似度 * @param html1 * @param html2 * @return * @throws IOException */ public static double similarity(String html1, String html2) throws IOException { String text1 = getPlainText(html1); String text2 = getPlainText(html2); Content ct1 = new Content(tokenizer(text1)); Content ct2 = new Content(tokenizer(text2)); return ContentComparator.compareStatic(ct1, ct2); }

本文共计128个文字,预计阅读时间需要1分钟。

如何用JSoup和diffator算法评估两段HTML文档的相似度?

java/** * 从HTML中提取纯文本内容 * @param HTML内容 * @return 提取的纯文本 */public static String getPlainText(String ) { if (StringUtils.isBlank()) { return ; } Element ebody=Jsoup.parseBodyFragment().body(); return ebody.select(code).text();}

如何用JSoup和diffator算法评估两段HTML文档的相似度?

gistfile1.txt

/** * 从一段HTML中萃取纯文本 * @param html * @return */ public static String getPlainText(String html){ if(StringUtils.isBlank(html)) return ""; Element ebody = Jsoup.parseBodyFragment(html).body(); ebody.select("code").remove(); ebody.select("pre").remove(); ebody.select("img").remove(); return ebody.text(); } /** * 计算两篇内容的相似度 * @param html1 * @param html2 * @return * @throws IOException */ public static double similarity(String html1, String html2) throws IOException { String text1 = getPlainText(html1); String text2 = getPlainText(html2); Content ct1 = new Content(tokenizer(text1)); Content ct2 = new Content(tokenizer(text2)); return ContentComparator.compareStatic(ct1, ct2); }