最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

TF-IDF理解及其Java實(shí)現(xiàn)代碼實(shí)例

 更新時(shí)間:2017年11月16日 08:55:20   作者:ywl925  
這篇文章主要介紹了TF-IDF理解及其Java實(shí)現(xiàn)代碼實(shí)例,簡單介紹了tfidf算法及其相應(yīng)公式,然后分享了Java實(shí)現(xiàn)代碼,具有一定參考價(jià)值,需要的朋友可以了解下。

TF-IDF

前言

前段時(shí)間,又具體看了自己以前整理的TF-IDF,這里把它發(fā)布在博客上,知識就是需要不斷的重復(fù)的,否則就感覺生疏了。

TF-IDF理解

TF-IDF(term frequency–inverse document frequency)是一種用于資訊檢索與資訊探勘的常用加權(quán)技術(shù), TFIDF的主要思想是:如果某個(gè)詞或短語在一篇文章中出現(xiàn)的頻率TF高,并且在其他文章中很少出現(xiàn),則認(rèn)為此詞或者短語具有很好的類別區(qū)分能力,適合用來分類。TFIDF實(shí)際上是:TF * IDF,TF詞頻(Term Frequency),IDF反文檔頻率(Inverse Document Frequency)。TF表示詞條在文檔d中出現(xiàn)的頻率。IDF的主要思想是:如果包含詞條t的文檔越少,也就是n越小,IDF越大,則說明詞條t具有很好的類別區(qū)分能力。如果某一類文檔C中包含詞條t的文檔數(shù)為m,而其它類包含t的文檔總數(shù)為k,顯然所有包含t的文檔數(shù)n=m + k,當(dāng)m大的時(shí)候,n也大,按照IDF公式得到的IDF的值會小,就說明該詞條t類別區(qū)分能力不強(qiáng)。但是實(shí)際上,如果一個(gè)詞條在一個(gè)類的文檔中頻繁出現(xiàn),則說明該詞條能夠很好代表這個(gè)類的文本的特征,這樣的詞條應(yīng)該給它們賦予較高的權(quán)重,并選來作為該類文本的特征詞以區(qū)別與其它類文檔。這就是IDF的不足之處.

TF公式:

以上式子中是該詞在文件中的出現(xiàn)次數(shù),而分母則是在文件中所有字詞的出現(xiàn)次數(shù)之和。

IDF公式:

|D|:語料庫中的文件總數(shù)

:包含詞語 ti 的文件數(shù)目(即 ni,j不等于0的文件數(shù)目)如果該詞語不在語料庫中,就會導(dǎo)致被除數(shù)為零,因此一般情況下使用

然后

TF-IDF實(shí)現(xiàn)(Java)

這里采用了外部插件IKAnalyzer-2012.jar,用其進(jìn)行分詞

具體代碼如下:

package tfidf;
import java.io.*;
import java.util.*;
import org.wltea.analyzer.lucene.IKAnalyzer;
public class ReadFiles {
	/**
   * @param args
   */
	private static ArrayList<String> FileList = new ArrayList<String>();
	// the list of file
	//get list of file for the directory, including sub-directory of it
	public static List<String> readDirs(String filepath) throws FileNotFoundException, IOException
	  {
		try
		    {
			File file = new File(filepath);
			if(!file.isDirectory())
			      {
				System.out.println("輸入的[]");
				System.out.println("filepath:" + file.getAbsolutePath());
			} else
			      {
				String[] flist = file.list();
				for (int i = 0; i < flist.length; i++)
				        {
					File newfile = new File(filepath + "\\" + flist[i]);
					if(!newfile.isDirectory())
					          {
						FileList.add(newfile.getAbsolutePath());
					} else if(newfile.isDirectory()) //if file is a directory, call ReadDirs
					{
						readDirs(filepath + "\\" + flist[i]);
					}
				}
			}
		}
		catch(FileNotFoundException e)
		    {
			System.out.println(e.getMessage());
		}
		return FileList;
	}
	//read file
	public static String readFile(String file) throws FileNotFoundException, IOException
	  {
		StringBuffer strSb = new StringBuffer();
		//String is constant, StringBuffer can be changed.
		InputStreamReader inStrR = new InputStreamReader(new FileInputStream(file), "gbk");
		//byte streams to character streams
		BufferedReader br = new BufferedReader(inStrR);
		String line = br.readLine();
		while(line != null){
			strSb.append(line).append("\r\n");
			line = br.readLine();
		}
		return strSb.toString();
	}
	//word segmentation
	public static ArrayList<String> cutWords(String file) throws IOException{
		ArrayList<String> words = new ArrayList<String>();
		String text = ReadFiles.readFile(file);
		IKAnalyzer analyzer = new IKAnalyzer();
		words = analyzer.split(text);
		return words;
	}
	//term frequency in a file, times for each word
	public static HashMap<String, Integer> normalTF(ArrayList<String> cutwords){
		HashMap<String, Integer> resTF = new HashMap<String, Integer>();
		for (String word : cutwords){
			if(resTF.get(word) == null){
				resTF.put(word, 1);
				System.out.println(word);
			} else{
				resTF.put(word, resTF.get(word) + 1);
				System.out.println(word.toString());
			}
		}
		return resTF;
	}
	//term frequency in a file, frequency of each word
	public static HashMap<String, float> tf(ArrayList<String> cutwords){
		HashMap<String, float> resTF = new HashMap<String, float>();
		int wordLen = cutwords.size();
		HashMap<String, Integer> intTF = ReadFiles.normalTF(cutwords);
		Iterator iter = intTF.entrySet().iterator();
		//iterator for that get from TF
		while(iter.hasNext()){
			Map.Entry entry = (Map.Entry)iter.next();
			resTF.put(entry.getKey().toString(), float.parsefloat(entry.getValue().toString()) / wordLen);
			System.out.println(entry.getKey().toString() + " = "+ float.parsefloat(entry.getValue().toString()) / wordLen);
		}
		return resTF;
	}
	//tf times for file
	public static HashMap<String, HashMap<String, Integer>> normalTFAllFiles(String dirc) throws IOException{
		HashMap<String, HashMap<String, Integer>> allNormalTF = new HashMap<String, HashMap<String,Integer>>();
		List<String> filelist = ReadFiles.readDirs(dirc);
		for (String file : filelist){
			HashMap<String, Integer> dict = new HashMap<String, Integer>();
			ArrayList<String> cutwords = ReadFiles.cutWords(file);
			//get cut word for one file
			dict = ReadFiles.normalTF(cutwords);
			allNormalTF.put(file, dict);
		}
		return allNormalTF;
	}
	//tf for all file
	public static HashMap<String,HashMap<String, float>> tfAllFiles(String dirc) throws IOException{
		HashMap<String, HashMap<String, float>> allTF = new HashMap<String, HashMap<String, float>>();
		List<String> filelist = ReadFiles.readDirs(dirc);
		for (String file : filelist){
			HashMap<String, float> dict = new HashMap<String, float>();
			ArrayList<String> cutwords = ReadFiles.cutWords(file);
			//get cut words for one file
			dict = ReadFiles.tf(cutwords);
			allTF.put(file, dict);
		}
		return allTF;
	}
	public static HashMap<String, float> idf(HashMap<String,HashMap<String, float>> all_tf){
		HashMap<String, float> resIdf = new HashMap<String, float>();
		HashMap<String, Integer> dict = new HashMap<String, Integer>();
		int docNum = FileList.size();
		for (int i = 0; i < docNum; i++){
			HashMap<String, float> temp = all_tf.get(FileList.get(i));
			Iterator iter = temp.entrySet().iterator();
			while(iter.hasNext()){
				Map.Entry entry = (Map.Entry)iter.next();
				String word = entry.getKey().toString();
				if(dict.get(word) == null){
					dict.put(word, 1);
				} else {
					dict.put(word, dict.get(word) + 1);
				}
			}
		}
		System.out.println("IDF for every word is:");
		Iterator iter_dict = dict.entrySet().iterator();
		while(iter_dict.hasNext()){
			Map.Entry entry = (Map.Entry)iter_dict.next();
			float value = (float)Math.log(docNum / float.parsefloat(entry.getValue().toString()));
			resIdf.put(entry.getKey().toString(), value);
			System.out.println(entry.getKey().toString() + " = " + value);
		}
		return resIdf;
	}
	public static void tf_idf(HashMap<String,HashMap<String, float>> all_tf,HashMap<String, float> idfs){
		HashMap<String, HashMap<String, float>> resTfIdf = new HashMap<String, HashMap<String, float>>();
		int docNum = FileList.size();
		for (int i = 0; i < docNum; i++){
			String filepath = FileList.get(i);
			HashMap<String, float> tfidf = new HashMap<String, float>();
			HashMap<String, float> temp = all_tf.get(filepath);
			Iterator iter = temp.entrySet().iterator();
			while(iter.hasNext()){
				Map.Entry entry = (Map.Entry)iter.next();
				String word = entry.getKey().toString();
				float value = (float)float.parsefloat(entry.getValue().toString()) * idfs.get(word);
				tfidf.put(word, value);
			}
			resTfIdf.put(filepath, tfidf);
		}
		System.out.println("TF-IDF for Every file is :");
		DisTfIdf(resTfIdf);
	}
	public static void DisTfIdf(HashMap<String, HashMap<String, float>> tfidf){
		Iterator iter1 = tfidf.entrySet().iterator();
		while(iter1.hasNext()){
			Map.Entry entrys = (Map.Entry)iter1.next();
			System.out.println("FileName: " + entrys.getKey().toString());
			System.out.print("{");
			HashMap<String, float> temp = (HashMap<String, float>) entrys.getValue();
			Iterator iter2 = temp.entrySet().iterator();
			while(iter2.hasNext()){
				Map.Entry entry = (Map.Entry)iter2.next();
				System.out.print(entry.getKey().toString() + " = " + entry.getValue().toString() + ", ");
			}
			System.out.println("}");
		}
	}
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		String file = "D:/testfiles";
		HashMap<String,HashMap<String, float>> all_tf = tfAllFiles(file);
		System.out.println();
		HashMap<String, float> idfs = idf(all_tf);
		System.out.println();
		tf_idf(all_tf, idfs);
	}
}

結(jié)果如下圖:

常見問題

沒有加入lucene jar包

lucene包和je包版本不適合

總結(jié)

以上就是本文關(guān)于TF-IDF理解及其Java實(shí)現(xiàn)代碼實(shí)例的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:

java算法實(shí)現(xiàn)紅黑樹完整代碼示例

Java算法之堆排序代碼示例

Java 蒙特卡洛算法求圓周率近似值實(shí)例詳解

如有不足之處,歡迎留言指出。

相關(guān)文章

  • Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(46)

    Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(46)

    下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望可以幫到你
    2021-08-08
  • MyBatis discriminator標(biāo)簽原理實(shí)例解析

    MyBatis discriminator標(biāo)簽原理實(shí)例解析

    這篇文章主要為大家介紹了MyBatis discriminator標(biāo)簽實(shí)現(xiàn)原理實(shí)例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • SpringBoot實(shí)現(xiàn)登錄攔截器的方法詳解

    SpringBoot實(shí)現(xiàn)登錄攔截器的方法詳解

    其實(shí)spring?boot攔截器的配置方式和springMVC差不多,只有一些小的改變需要注意下就ok了。本文主要給大家介紹了關(guān)于如何在Springboot實(shí)現(xiàn)登陸攔截器功能,需要的朋友可以參考下
    2022-07-07
  • Java中的Pair詳細(xì)

    Java中的Pair詳細(xì)

    這篇文章主要介紹Java中的很有意思的Pair,下面文章會以Pair用法展開,感興趣的小伙伴可以參考下面文章的具體內(nèi)容
    2021-10-10
  • MyBatis批量插入的五種方式

    MyBatis批量插入的五種方式

    這篇文章主要介紹了MyBatis批量插入的五種方式,每種方式結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-11-11
  • controller接口跳轉(zhuǎn)到另一個(gè)controller接口的實(shí)現(xiàn)

    controller接口跳轉(zhuǎn)到另一個(gè)controller接口的實(shí)現(xiàn)

    這篇文章主要介紹了controller接口跳轉(zhuǎn)到另一個(gè)controller接口的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • 一篇文章輕松搞懂Java中的自旋鎖

    一篇文章輕松搞懂Java中的自旋鎖

    隨著互聯(lián)網(wǎng)的蓬勃發(fā)展,越來越多的互聯(lián)網(wǎng)企業(yè)面臨著用戶量膨脹而帶來的并發(fā)安全問題。這篇文章主要給大家介紹了關(guān)于Java中自旋鎖的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • Java中引用類型class的用法總結(jié)

    Java中引用類型class的用法總結(jié)

    這篇文章主要為大家詳細(xì)介紹了Java中引用類型class的用法的相關(guān)資料,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Java有一定幫助,感興趣的可以了解一下
    2022-10-10
  • ajax實(shí)時(shí)監(jiān)測與springboot的實(shí)例分析

    ajax實(shí)時(shí)監(jiān)測與springboot的實(shí)例分析

    本文將介紹如何使用 AJAX 技術(shù)結(jié)合 Spring Boot 構(gòu)建一個(gè)實(shí)時(shí)反饋用戶輸入的應(yīng)用,我們將創(chuàng)建一個(gè)簡單的輸入框,當(dāng)用戶在輸入框中鍵入文本時(shí),應(yīng)用將異步地向后端發(fā)送請求,感興趣的朋友跟隨小編一起看看吧
    2024-07-07
  • SpringBoot?整合?Elasticsearch?實(shí)現(xiàn)海量級數(shù)據(jù)搜索功能

    SpringBoot?整合?Elasticsearch?實(shí)現(xiàn)海量級數(shù)據(jù)搜索功能

    這篇文章主要介紹了SpringBoot?整合?Elasticsearch?實(shí)現(xiàn)海量級數(shù)據(jù)搜索,本文主要圍繞?SpringBoot?整合?ElasticSearch?接受數(shù)據(jù)的插入和搜索使用技巧,在實(shí)際的使用過程中,版本號尤其的重要,不同版本的?es,對應(yīng)的?api?是不一樣,需要的朋友可以參考下
    2022-07-07

最新評論

河南省| 博野县| 柳江县| 建阳市| 临洮县| 吉水县| 大田县| 宜黄县| 珲春市| 股票| 吉隆县| 万荣县| 顺义区| 都江堰市| 大足县| 哈尔滨市| 大埔区| 聂拉木县| 万安县| 陇西县| 鹤岗市| 正宁县| 长寿区| 三河市| 潜江市| 南丰县| 江口县| 顺义区| 大庆市| 桂东县| 和林格尔县| 阳山县| 阜宁县| 平邑县| 衡水市| 云和县| 大足县| 凯里市| 特克斯县| 乐昌市| 漠河县|