倒排索引的java实现

 假设有3篇文章,file1, file2, file3,文件内容如下: 

 

文件内容代码   收藏代码
  1. file1 (单词1,单词2,单词3,单词4....)  
  2.   
  3. file2 (单词a,单词b,单词c,单词d....)  
  4.   
  5. file3 (单词1,单词a,单词3,单词d....)  
 

    那么建立的倒排索引就是这个样子:

 

文件内容代码   收藏代码
  1. 单词1 (file1,file3)  
  2.   
  3. 单词2 (file1)  
  4.   
  5. 单词3 (file1,file3)  
  6.   
  7. 单词a (file2, file3)  
  8.   
  9. ....  

 

而词频就是每个单词在文件中出现的相应次数,本文计算的是每个单词在所有文件中出现的总次数,如果有更简洁有效的写法,欢迎交流。

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


public class IntertedIndex {
	
	private Map<String, ArrayList<String>> map=new HashMap<>();
	private ArrayList<String> list;
	private Map<String, Integer> nums=new HashMap<>();
	
	public void CreateIndex(String filepath){

		String[] words = null;
		try {
		
			File file=new File(filepath);
			BufferedReader reader=new BufferedReader(new FileReader(file));
			String s=null;
			while((s=reader.readLine())!=null){
				//获取单词
				words=s.split(" ");
				
			}
			
			for (String string : words) {
			
				if (!map.containsKey(string)) {
					list=new ArrayList<String>();
					list.add(filepath);
					map.put(string, list);
					nums.put(string, 1);
				}else {
					list=map.get(string);
					//如果没有包含过此文件名,则把文件名放入
					if (!list.contains(filepath)) {
						list.add(filepath);
					}
					//文件总词频数目
					int count=nums.get(string)+1;
					nums.put(string, count);
				}
			}
			reader.close();
			
		} catch (IOException e) {
			
			e.printStackTrace();
		}
	
		
	}
	public static void main(String[] args) {
		IntertedIndex index=new IntertedIndex();
		
		for(int i=1;i<=3;i++){
			String path="E:\\data\\"+i+".txt";
			index.CreateIndex(path);
		}
		for (Map.Entry<String, ArrayList<String>> map : index.map.entrySet()) {
			System.out.println(map.getKey()+":"+map.getValue());
		}

		for (Map.Entry<String, Integer> num : index.nums.entrySet()) {
			System.out.println(num.getKey()+":"+num.getValue());
		}
	}
}
文件内容:


1.txt:i live in hangzhou where are you

2.txt:i love you i love you

3.txt:i love you today is a good day


运行结果



  • 11
    点赞
  • 69
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
倒排索引(Inverted Index)是一种常见的文本索引方法,主要用于全文搜索、信息检索等领域。在倒排索引中,每个单词都与包含该单词的文档列表相关联,通过该索引可以快速找到包含某个单词的文档。 下面是一个简单的 Java 实现: ```java import java.util.*; public class InvertedIndex { private Map<String, List<Integer>> index = new HashMap<>(); public void addDocument(int docId, String[] words) { for (String word : words) { List<Integer> docs = index.getOrDefault(word, new ArrayList<>()); docs.add(docId); index.put(word, docs); } } public List<Integer> search(String query) { return index.getOrDefault(query, new ArrayList<>()); } public static void main(String[] args) { InvertedIndex invertedIndex = new InvertedIndex(); invertedIndex.addDocument(1, new String[]{"apple", "banana", "orange"}); invertedIndex.addDocument(2, new String[]{"banana", "pear", "grape"}); invertedIndex.addDocument(3, new String[]{"orange", "pear", "apple"}); List<Integer> result = invertedIndex.search("banana"); System.out.println(result); } } ``` 在上述实现中,`addDocument` 方法用于向倒排索引中添加文档,其中 `docId` 表示文档的编号,`words` 表示文档中包含的单词。该方法会将每个单词与包含该单词的文档列表相关联,如果该单词还没有对应的文档列表,则创建一个新的列表。最后将单词和文档列表放入索引中。 `search` 方法用于根据查询词从倒排索引中查找包含该词的文档列表。如果倒排索引中不存在该词,则返回一个空列表。 在上述实现中,我们创建了一个包含三个文档倒排索引,并查找包含单词 "banana" 的文档列表。输出结果为 `[1, 2]`,表示包含 "banana" 的文档编号为 1 和 2。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值