日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java tf值搜索_搜索引擎优化 TF_IDF之Java实现

發布時間:2025/4/5 java 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java tf值搜索_搜索引擎优化 TF_IDF之Java实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

實現之前,我們要事先說明一些問題:

我們用Redis對數據進行持久化,存兩種形式的MAP:

key值為term,value值為含有該term的url

key值為url,value值為map,記錄term及在文章中出現的次數

總的計算公式如下:

1.計算詞頻TF

這里通過給出url地址,獲取搜索詞term在此url中的數量,計算出TF

獲取url中的詞匯總數

/**

* @Author Ragty

* @Description 獲取url中的詞匯總數

* @Date 11:18 2019/6/4

**/

public Integer getWordCount(String url) {

String redisKey = urlSetKey(url);

Map map = jedis.hgetAll(redisKey);

Integer count = 0;

for(Map.Entry entry: map.entrySet()) {

count += Integer.valueOf(entry.getValue());

}

return count;

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

返回搜索項在url中出現的次數

/**

* @Author Ragty

* @Description 返回搜索項在url中出現的次數

* @Date 22:12 2019/5/14

**/

public Integer getTermCount(String url,String term) {

String redisKey = urlSetKey(url);

String count = jedis.hget(redisKey,term);

return new Integer(count);

}

1

2

3

4

5

6

7

8

9

10

獲取搜索詞的詞頻

/**

* @Author Ragty

* @Description 獲取搜索詞的詞頻(Term Frequency)

* @Date 11:25 2019/6/4

**/

public BigDecimal getTermFrequency(String url,String term) {

if (!isIndexed(url)) {

System.out.println("Doesn‘t indexed.");

return null;

}

Integer documentCount = getWordCount(url);

Integer termCount = getTermCount(url,term);

return documentCount==0 ? new BigDecimal(0) : new BigDecimal(termCount).divide(new BigDecimal(documentCount),6,BigDecimal.ROUND_HALF_UP);

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

2.計算逆文檔頻率

計算逆文檔頻率,需要計算文檔總數,以及包含該搜索詞的文章數

獲取redis索引文章的總數

/**

* @Author Ragty

* @Description 獲取redis索引文章的總數

* @Date 19:46 2019/6/5

**/

public Integer getUrlCount() {

Integer count = 0;

count = urlSetKeys().size();

return count;

}

1

2

3

4

5

6

7

8

9

10

獲取含有搜索詞的文章數

/**

* @Author Ragty

* @Description 獲取含有搜索詞的文章數

* @Date 22:42 2019/6/5

**/

public Integer getUrlTermCount(String term) {

Integer count = 0;

count = getUrls(term).size();

return count;

}

1

2

3

4

5

6

7

8

9

10

計算逆文檔頻率IDF(InverseDocumnetFrequency)

/**

* @Author Ragty

* @Description 計算逆文檔頻率IDF(InverseDocumnetFrequency)

* @Date 23:32 2019/6/5

**/

public BigDecimal getInverseDocumentFrequency(String term) {

Integer totalUrl = getUrlCount();

Integer urlTermCount = getUrlTermCount(term);

Double xx = new BigDecimal(totalUrl).divide(new BigDecimal(urlTermCount),6,BigDecimal.ROUND_HALF_UP).doubleValue();

BigDecimal idf = new BigDecimal(Math.log10(xx));

return idf;

}

1

2

3

4

5

6

7

8

9

10

11

12

3.獲取TF-IDF

/**

* @Author Ragty

* @Description 獲取tf-idf值

* @Date 23:34 2019/6/5

**/

public BigDecimal getTFIDF(String url,String term) {

BigDecimal tf = getTermFrequency(url, term);

BigDecimal idf = getInverseDocumentFrequency(term);

BigDecimal tfidf =tf.multiply(idf);

return tfidf;

}

1

2

3

4

5

6

7

8

9

10

11

4.數據測試

這里我采用我自己爬取的部分數據,進行一下簡單的測試(可能因為數據集的原因導致部分結果不準確)

測試類方法:

/**

* @Author Ragty

* @Description 獲取tfidf下的相關性

* @Date 8:47 2019/6/6

**/

private static BigDecimal getRelevance(String url,String term,JedisIndex index) {

BigDecimal tfidf = index.getTFIDF(url,term);

return tfidf;

}

/**

* @Author Ragty

* @Description 執行搜索

* @Date 23:49 2019/5/30

**/

public static WikiSearch search(String term,JedisIndex index) {

Map map = new HashMap();

Set urls = index.getUrls(term);

for (String url: urls) {

BigDecimal tfidf = getRelevance(url,term,index).setScale(6,BigDecimal.ROUND_HALF_UP);

map.put(url,tfidf);

}

return new WikiSearch(map);

}

/**

* @Author Ragty

* @Description 按搜索項頻率順序打印內容

* @Date 13:46 2019/5/30

**/

private void print() {

List> entries = sort();

for(Entry entry: entries) {

System.out.println(entry.getKey()+" "+entry.getValue());

}

}

/**

* @Author Ragty

* @Description 根據相關性對數據排序

* @Date 13:54 2019/5/30

**/

public List> sort(){

List> entries = new LinkedList>(map.entrySet());

Comparator> comparator = new Comparator>() {

@Override

public int compare(Entry o1, Entry o2) {

return o2.getValue().compareTo(o1.getValue());

}

};

Collections.sort(entries,comparator);

return entries;

}

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

測試代碼:

public static void main(String[] args) throws IOException {

Jedis jedis = JedisMaker.make();

JedisIndex index = new JedisIndex(jedis);

// search for the first term

String term1 = "java";

System.out.println("Query: " + term1);

WikiSearch search1 = search(term1, index);

search1.print();

// search for the second term

String term2 = "programming";

System.out.println("Query: " + term2);

WikiSearch search2 = search(term2, index);

search2.print();

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

測試結果:

Query: java

https://baike.baidu.com/item/LiveScript 0.029956

https://baike.baidu.com/item/Java/85979 0.019986

https://baike.baidu.com/item/Brendan%20Eich 0.017188

https://baike.baidu.com/item/%E7%94%B2%E9%AA%A8%E6%96%87/471435 0.013163

https://baike.baidu.com/item/Sun/69463 0.005504

https://baike.baidu.com/item/Rhino 0.004401

https://baike.baidu.com/item/%E6%8E%92%E7%89%88%E5%BC%95%E6%93%8E 0.003452

https://baike.baidu.com/item/javascript 0.002212

https://baike.baidu.com/item/js/10687961 0.002212

https://baike.baidu.com/item/%E6%BA%90%E7%A0%81 0.002205

https://baike.baidu.com/item/%E6%BA%90%E7%A0%81/344212 0.002205

https://baike.baidu.com/item/%E8%84%9A%E6%9C%AC%E8%AF%AD%E8%A8%80 0.001989

https://baike.baidu.com/item/SQL 0.001779

https://baike.baidu.com/item/PHP/9337 0.001503

https://baike.baidu.com/item/iOS/45705 0.001499

https://baike.baidu.com/item/Netscape 0.000863

https://baike.baidu.com/item/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F 0.000835

https://baike.baidu.com/item/Mac%20OS%20X 0.000521

https://baike.baidu.com/item/C%E8%AF%AD%E8%A8%80 0.000318

Query: programming

https://baike.baidu.com/item/C%E8%AF%AD%E8%A8%80 0.004854

https://baike.baidu.com/item/%E8%84%9A%E6%9C%AC%E8%AF%AD%E8%A8%80 0.002529

---------------------

原文:https://www.cnblogs.com/ly570/p/11106215.html

總結

以上是生活随笔為你收集整理的java tf值搜索_搜索引擎优化 TF_IDF之Java实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 国产精品一线 | 无码人妻丰满熟妇区五十路 | 精品国产乱码久久久久久免费 | 国产精品区在线 | 大桥未久av在线 | 九九自拍偷拍 | 波多野吉衣在线观看视频 | 亚洲天堂中文在线 | 亚洲色图36p| 欧美激情亚洲激情 | 国产精品久久久久久久久久小说 | 9999免费视频 | 天堂网av在线播放 | 假日游船法国满天星 | 貂蝉被到爽流白浆在线观看 | 亚洲欧美激情小说另类 | 国产人妻久久精品一区二区三区 | 欧美一级一片 | 午夜av毛片 | 久草免费新视频 | 一道本无吗一区 | 国产熟女精品视频 | 久久社区视频 | 超碰人人国产 | 色伊伊| 中文字幕欧美日韩 | 午夜高清 | 成年人免费网站 | 国产在线拍| 亚洲欧美在线综合 | 成人亚洲视频 | 色老头在线视频 | 精品视频在线免费观看 | 欧洲精品一区二区三区 | 久久久久久久久国产精品一区 | 青青国产精品视频 | 99re在线视频免费观看 | 成人女同av免费观看 | 天堂中文在线播放 | 久久久久久人妻一区二区三区 | 成人毛片一级 | 日韩黄色在线观看 | 久操视频精品 | 亚洲私人影院 | av最新版天堂资源在线 | 男人天堂一区 | yy111122少妇光屁股影院 | 波多野结衣啪啪 | av国语 | 波多野结衣av在线观看 | 超碰95在线 | www.蜜桃av.com| 青草视频在线免费观看 | 亚洲国产精品视频在线 | 亚洲最大成人av | 黑人巨大精品人妻一区二区 | 亚洲欧美成人一区二区三区 | 亚洲色图3p | 永久免费的av网站 | h网站在线 | 日本免费视频 | 91精品综合久久久久久五月天 | 日本视频免费在线 | 成人在线免费视频 | eeuss国产一区二区三区 | 国产福利不卡 | 亚洲成年人影院 | 久久精品国内 | 三级大片在线观看 | 婷婷激情综合网 | 亚洲aaa视频 | 亚洲色中色 | 亚洲女同视频 | 99九九久久 | 国产乱淫av麻豆国产免费 | 国产精品久久午夜夜伦鲁鲁 | 老湿影院av | 色奇米| 成av人片一区二区三区久久 | 亚洲aⅴ在线 | 老司机午夜福利视频 | 艳母日本动漫在线观看 | 欧美亚洲色综久久精品国产 | 日本激情视频在线观看 | 天堂av.com| 一区二区三区四区免费 | 日日躁夜夜躁白天躁晚上躁91 | 中国老妇性视频 | 久久久资源网 | 国产午夜大片 | 亚洲一区二区三区乱码 | 国产网站大全 | 福利在线观看 | 黄色污污网站在线观看 | 亚洲精品理论片 | 国产成人久久精品麻豆二区 | 黄色av网站在线看 | 亚洲综合色一区 | 韩日激情视频 |