首页 > 编程语言 >倒排索引的 JAVA 简单实现

倒排索引的 JAVA 简单实现

时间:2023-01-09 14:02:52浏览次数:45  
标签:JAVA 倒排 keyword filePath System 索引 new indexMap String

 

   倒排索引的简单 JAVA 实现,当玩具其实都很粗糙,简单实现下原理:

public class IntertedIndex {

    // 倒排索引
    private Map<String, List<String>> indexMap;
    // 关键词计数
    private Map<String, Integer> keywordNums;

    IntertedIndex() {
        this.indexMap = new HashMap<String, List<String>>();
        this.keywordNums = new HashMap<String, Integer>();
    }

    public static void main(String[] args) throws Exception {
        IntertedIndex intertedIndex = new IntertedIndex();
        intertedIndex.createIndexByFolder("C:\\Users\\Administrator\\Desktop\\logs");
        java.util.Scanner s = new java.util.Scanner(System.in);
        String inString;
        while ((inString = s.next()) != null) {
            System.out.println("查询关键字 :" + inString);
            try {
                intertedIndex.search(inString);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
//        System.out.println(intertedIndex.toString());
    }

    public void search(String keyword) {
        if (!this.indexMap.keySet().contains(keyword))
            throw new RuntimeException(" keyword : " + keyword + " is not in map ! ");
        System.out.println(">>>>>> " + keyword + " ( " + this.keywordNums.get(keyword) + " ) ");
        List<String> fileList = this.indexMap.get(keyword);
        for (int i = 0; i < fileList.size(); i++) {
            System.out.println("       >>> " + fileList.get(i));
        }
    }

    public void createIndexByFolder(String folderName) throws Exception {
        File foler = new File(folderName);
        File[] files = foler.listFiles();
        for (int i = 0; i < files.length; i++) {
            File file = files[i];
            String filePath = file.getAbsolutePath();
            System.out.println("deal file : " + filePath);
            this.createIndexByFile(filePath);
        }
    }

    public void createIndexByFile(String fileName) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), Charset.forName("GBK")));
        String row = null;
        try {
            while ((row = reader.readLine()) != null) {
                String[] keywords = row.split(" ");
                // 分词
                StringTokenizer str = new StringTokenizer(row, " ,:\" \rn()'-'.,!?,:“”‘’?-!。," + System.lineSeparator());
                while (str.hasMoreElements()) {
                    String keyword = str.nextElement().toString();
                    if (keyword.length() > 100) continue;
                    while (keyword.contains(" ")) keyword = keyword.replace(" ", "");
                    this.createIndexByKeyword(keyword, fileName);
                }
            }
        } catch (Exception e) {
            throw e;
        } finally {
            reader.close();
        }
    }

    public synchronized void createIndexByKeyword(String keyword, String fileName) {
        System.out.println("deal keyword : " + keyword);
        boolean isNew = false;
        if (isNew = !indexMap.containsKey(keyword)) indexMap.put(keyword, new ArrayList<String>());
        List<String> fileNameList = indexMap.get(keyword);
        if (isNew || !fileNameList.contains(fileName)) {
            fileNameList.add(fileName);
        }
        keywordNums.put(keyword, keywordNums.getOrDefault(keyword, 0) + 1);
    }

    @Override
    public String toString() {
        String res = "";
        Iterator<String> iterator = this.indexMap.keySet().iterator();
        while (iterator.hasNext()) {
            String keyword = iterator.next();
            res += ">>>>>> " + keyword + " (" + this.keywordNums.get(keyword) + " ) " + " <<<<<<";
            res += System.lineSeparator();
            List<String> fileList = this.indexMap.get(keyword);
            for (int i = 0; i < fileList.size(); i++) {
                String filePath = fileList.get(i);
                if (filePath.contains("/")) {
                    String[] pathArr = filePath.split("/");
                    filePath = pathArr[pathArr.length - 1];
                }
                res += "       >>> " + filePath;
                res += System.lineSeparator();
            }
        }
        return res;
    }
}

 

标签:JAVA,倒排,keyword,filePath,System,索引,new,indexMap,String
From: https://www.cnblogs.com/niuyourou/p/17036822.html

相关文章

  • Java内存区域有哪些构成?
    目录前言Java内存区域程序计数器虚拟机栈本地方法栈堆方法区字符串常量池运行时常量池直接内存小结作者:小牛呼噜噜|https://xiaoniuhululu.com计算机内功、JAVA底层......
  • javascript将文本转语音功能
    通过jiavascript将文本内容转化为语音播放,代码如下:<body><buttononclick="start()">点击</button><script>vartext='语音输入开始'var......
  • java根据xml节点地址获取指定节点内容
    备好几个前同事问过怎么获取xml指定节点内容后,终于决定写个工具类,今天特地分享给大家,写的不好,不要喷maven依赖包<dependency><groupId>dom4j</groupId><artifactId>do......
  • java文件夹和文件
    本文主要讲述java如何创建文件夹和文件题目:指定路径,判断当前路径是否有目标文件夹,如果没有,则创建;如果有,在目标文件夹下创建目标文件【txt文件】,并使用转换流+处理流写入......
  • java的读取和写入properties配置文件
    本文主要讲述java读取和写入properties文件操作一.介绍Properties类  Properties用于读取和写入Xx.properties文件,获取k-v二.Properties类的读取和写入Propertie......
  • Java中Static作用
    类成员:实例属性、实例方法、静态属性、静态方法、构造方法实例与静态的区分:看是否被static修饰,被static修饰的就是静态的,没有就是实例的在java之中可以使用static声明属......
  • Java面试的一些面试题
    ​10<<2=?tip:10的二进制为1010,左移两位即:101000,换算为十进制为2的5次方加上2的三次方等于40答:401.dr-xr-xr-r解释一下权限的含义tip:参考:Liunx学习总结(四)--文件的......
  • Java的this关键字和构造方法与匿名对象
    this关键字的作用 1.表示类中的属性2.可以使用this调用本类的构造方法3.this表示当前对象 1.1this调用本类中的属性classPerson{privateStringname;//姓......
  • java解决跨域问题
    什么是跨域 1)、协议不同2)、域名不同(IP地址不同)3)、端口号不同跨域是指一个页面想获取另一个页面中的资源,如果这两个页面的协议、域名、子域名、端口不同,或者两个页面......
  • Java_基础总结
    总结。  一、运行环境jdk:开发工具包jre:运行时环境jvm:虚拟机编译:使用javac,将.java源文件编译为.class文件。运行:使用java,运行.class文件......