开发者社区> 问答> 正文

一个关于文本匹配的问题

建立一个文本文件,其内容如下。对于任一给定的单词,在该文本中找寻匹配的字符串,输出结果
Information security means protecting
information and information systems from unauthorized access, use, disclosure,
disruption, modification, or destruction. The terms information security,
computer security and information assurance are frequently used
interchangeably.
具体要求:
1、实现文件的读取操作,而不是将文本以字符串形式预存于程序中

2、进行单词匹配时,不区分大小写

3、统计需要匹配的单词在该文本中出现的次数和位置(即该单词是文本的第几个单词),并输出
4、对于基本匹配但不完全匹配的单词,能够给出提醒信息(例如,当用户试图匹配单词“securite”时,系统能够提示用户,是否是需要匹配“security”)
最好是java的图形化界面,其他的也可以

展开
收起
蛮大人123 2016-02-20 10:25:00 2327 0
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪

    对于这个题我的思路是:先从文件中读入内容到程序,然后对于用户输入的目标串进行匹配,匹配成功的结果存入Map中,文本扫描完毕,Map中的键值对就是所要求的目标串的位置和值,假若匹配不成功,则才从头检索目标串的子串,循环进行查找,找到相近的就是你所说的基本匹配 我写的代码如下,不足之处欢迎指正:

    public class FileTest 
     {
     public static void main(String[] args)throws Exception
     {
     int count=0,index=0;
     HashMap map=new HashMap();
     Scanner sc=null;
     boolean flag=false;
        System.out.println("待查找的目标串为:");
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String mode = br.readLine();
    
        sc=new Scanner(new File("F:\\myclass\\test.txt"));
        while(sc.hasNext())
        {
            index++;
            if(sc.next().equalsIgnoreCase(mode))
            {
                flag=true;
                map.put(""+index,sc.next());
                count++;
            }
        }
        if(flag)
        {
            System.out.println("查到目标串count:"+count+"的分别为:");
            for(Object key:map.keySet())
                System.out.println("索引Key:"+key+"----->"+map.get(key));
        }
        else
        {
            sc=new Scanner(new File("F:\\myclass\\test.txt"));
            q:while(mode.length()>0)
            {
                mode=mode.substring(0,mode.length()-1);
                while(sc.hasNext())
                {
                    String s=sc.next();
                    if(s.compareTo(mode)==(s.length()-mode.length()))
       {
                         System.out.println("未找到目标串,但是找到相似的串"+s);
                         break q;
                    }
                }
            }
    
        }
    }
    }
    2019-07-17 18:45:08
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载