当前位置: 首页 > news >正文

比较两个文本,找出在另一文本中出现过的串并换某种格式输出

【问题】

I am writing a program to read two files and then compare them word by word and line by line. Basically, I need to check if the first line in the first text file is a substring of any line in the second file then display the first word of each line of the second file that it is a substring of then repeat the process with all the other lines of the first file. Additionally, I need to do this without using java functions like contains().

For each line in the first file, I need to check the first word with each word in the lines of the second file till I find a match. Once I find a match I need to check if the second word in the first file is the same as the next word in the second file and so on until the end of the line in the first file. If the entire line in the first file is contained in a line of the second file then the program must print the first word of that line from the second file.

For example
File1.txt
like parks
went out
go out

File2.txt
I like to go out because I like parks
Ben does not go out much
Shelly went out often but does not like parks
Harry does not go out neither does he like parks

Desired Output:
q1. like parks
I
Shelly
Harry
q2. went out
Shelly
q3. go out
I
Ben
Harry

// Import io so we can use file objects

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.IOException;

import java.io.*;

public class wordc {

public static void main(String[] args) {

    try {

        //reads the files

        BufferedReader bf1 = new BufferedReader(new FileReader("File1.txt"));

        BufferedReader bf2 = new BufferedReader(new FileReader("File2.txt"));

        int k =0, l = 0, i = 0, j = 0, count = 0, linecount1 = 0, linecount2 = 0, wordcount1 = 0, wordcount2 = 0;

            String line1, line2;

            //counts the number of lines in File1

        while((line1 = bf1.readLine()) != null)

        {

            linecount1++;

        }

        //counts the number of lines in File2

        while((line2 = bf2.readLine()) != null)

        {

            linecount2++;

        }

        // loop to iterate through File1

        while((line1 = bf1.readLine()) != null && k < linecount1)

        {

            System.out.println("q"+ k++ + "line1");

            //store words in the current line in the File1 in a word array

            String[] word1 = line1.split(" ");

            //number of words in the line

            wordcount1 = word1.length;

                //loop to iterate through File2

                while ((line2 = bf1.readLine()) != null && l < linecount2)

                {

                    //store words in current line in the File2 in a word array

                    String[] word2 = line2.split(" ");

                    // number of words in the line

                    wordcount2 = word2.length;

                    count = 0;

                    while(j < wordcount1)

                    {

                        while(i < wordcount2)

                    {

                     //compare first word in word1 array to first word in word2 array

                     //continue to compare till a match is found

                     //once a match is found increament count

                      // and compare the next word in the word1 array with the next word in the word2 array

                      //and so on

                            if (word1[j].equals(word2[i]))

                            {

                                i++;

                                j++;

                                count++;

                            }

                          //if the current word in word1 does not match the word in word2

                          //check the current word in word1 with the next word in word2

                            else

                            {

                                i++;

                                break;

                            }

                        }

                    }

                    //if the number of words in a line in File1 matched a portion of a line in File2

                    //print the first word of that line

                    if(count == wordcount1)

                        System.out.println(line2[l]);

                    l++;

                }

                k++;

        }

            bf1.close();

            bf2.close();

    }

    catch (IOException e) {

        System.out.println("IO Error Occurred:" + e.toString());

    }

}

}

Thanks in advance for all the help! :)

【回答】

这个问题并不复杂,使用双层循环结合字符串操作(查询、拆分、合并、定位)就可实现,但是从底层写起确实很复杂,可以用SPL辅助实现,只需三行代码:

A
1=file("D:\\file1.txt").read@n()
2=file("D:\\file2.txt").read@n()
3=A1.conj(A2.select(pos(~,A1.~)).(~.words()(1)))

A1:按行读入文本file1.txt

A2:按行读入文本file2.txt

A3:函数conj对集合成员进行循环计算,最后合并各个子集。函数select对集合成员筛选,函数pos可判断字符串包含关系,函数words将字符串按单词拆分。

conj\select\pos都是循环函数,可以大幅降低循环语句的使用,代码更简练

       写好的脚本如何在应用程序中调用,可以参考Java 如何调用 SPL 脚本

 

相关文章:

  • Windows下安装虚拟环境
  • SpringMVC之注解驱动的控制器
  • Flask--路由配置
  • Linux定时器
  • iNFTnews | 一词解答区块链技术普及的制胜关键
  • 京准,PTP时间同步服务器在运营商通信网应用
  • 判断数组类型的方法(Array.isArray)以及Math数字对象
  • zemax---Tangential plane, meridian plane and sagittal plane(切线面,子午面与弧矢面)(完结)
  • Java配置42-配置redis高可用(sentinel监控)
  • 共话龙蜥:中国操作系统到底有没有角力世界舞台的实力?
  • vue独立提供模板下载功能
  • 怎么判断MES系统好不好?MES又是如何帮企业省钱的?
  • 数据治理:为什么不见BI作关联分析
  • 聚氨基酯偶联牛血清白蛋白/人血清白蛋白/卵清白蛋白纳米粒PAE-BSA/HSA/OVA(合成路线)
  • 应用开发类API推荐
  • 网络传输文件的问题
  • 4月23日世界读书日 网络营销论坛推荐《正在爆发的营销革命》
  • CSS魔法堂:Absolute Positioning就这个样
  • Hexo+码云+git快速搭建免费的静态Blog
  • JS进阶 - JS 、JS-Web-API与DOM、BOM
  • k8s 面向应用开发者的基础命令
  • Linux后台研发超实用命令总结
  • Odoo domain写法及运用
  • React Transition Group -- Transition 组件
  • Vue实战(四)登录/注册页的实现
  • ------- 计算机网络基础
  • 前端每日实战 2018 年 7 月份项目汇总(共 29 个项目)
  • 如何使用 JavaScript 解析 URL
  • 使用Envoy 作Sidecar Proxy的微服务模式-4.Prometheus的指标收集
  • 适配iPhoneX、iPhoneXs、iPhoneXs Max、iPhoneXr 屏幕尺寸及安全区域
  • 教程:使用iPhone相机和openCV来完成3D重建(第一部分) ...
  • 摩拜创始人胡玮炜也彻底离开了,共享单车行业还有未来吗? ...
  • ​低代码平台的核心价值与优势
  • ​第20课 在Android Native开发中加入新的C++类
  • #define、const、typedef的差别
  • (+4)2.2UML建模图
  • (07)Hive——窗口函数详解
  • (4) PIVOT 和 UPIVOT 的使用
  • (八)光盘的挂载与解挂、挂载CentOS镜像、rpm安装软件详细学习笔记
  • (五)网络优化与超参数选择--九五小庞
  • (终章)[图像识别]13.OpenCV案例 自定义训练集分类器物体检测
  • (最完美)小米手机6X的Usb调试模式在哪里打开的流程
  • ****Linux下Mysql的安装和配置
  • ***测试-HTTP方法
  • ./和../以及/和~之间的区别
  • .NET CORE 第一节 创建基本的 asp.net core
  • .net core 源码_ASP.NET Core之Identity源码学习
  • .Net Remoting(分离服务程序实现) - Part.3
  • .NET 程序如何获取图片的宽高(框架自带多种方法的不同性能)
  • .NET 命令行参数包含应用程序路径吗?
  • .net反混淆脱壳工具de4dot的使用
  • .net获取当前url各种属性(文件名、参数、域名 等)的方法
  • .NET基础篇——反射的奥妙
  • .NET连接数据库方式
  • @ConfigurationProperties注解对数据的自动封装