单词检索统计程序(Java)

题目要求:


本项目的设计要求可以分为三个部分实现:其一,建立一个文本文件,文件名由用户用键盘输入;其二,给定单词计数,输入一个不含空格的单词,统计输出该单词在文本中的出现次数;其三,检索给定单词,输入一个单词,检索并输出该单词所在的行号、该行中出现的次数以及在该行中的相应位置。

  1. 建立文件的实现思路是:
    (1) 定义一个串变量;
    (2) 定义文本文件;
    (3) 输入文件名,打开该文件;
    (4) 循环读入文本行,写入文本文件,其过程如下:
    while(不是文件输入结束){
    读入一文本行至串变量;
    串变量写入文件;
    输入是否结束输入标志;

    (5) 关闭文件。
  2. 给定单词计数的实现思路是:
    该功能需要用到模式匹配算法,逐行扫描文本文件。匹配一个,计数器加1,直到整个文件扫描结束;然后输出单词出现的次数。
    串是非数值处理中的主要对象,如在信息检索、文本编辑、符号处理等许多领域,得到越来越广泛的应用。在串的基本操作中,在主串中查找模式串的模式匹配算法是文本处理中最常用、最重要的操作之一,称为模式匹配或串匹配,就是求子串在主串中首次出现的位置。朴素模式匹配算法的基本思路是将给定字串与主串从第一个字符开始比较,找到首次与子串完全匹配的子串为止,并记住该位置。但为了实现统计子串出现的个数,不仅需要从主串的第一个字符位置开始比较,而且需要从主串的任一位置检索匹配字符串。
    其实现过程如下:
    (1) 输入要检索的文本文件名,打开相应的文件;
    (2) 输入要检索统计的单词;
    (3) 循环读文本文件,读入一行,将其送入定义好的串中,并求该串的实际长度,调用串匹配函数进行计数。具体描述如下:
    while(不是文件结束){
    读入一行并到串中;
    求出串长度;
    模式匹配函数计数;

    (4) 关闭文件,输出统计结果。
  3. 检索单词出现在文本文件中的行号、次数及其位置的实现思路是:
    这个设计要求同上一个设计类似,但是要相对复杂一些。其实现过程描述如下:
    (1) 输入要检索的文本文件名,打开相应的文件;
    (2) 输入要检索统计的单词;
    (3) 行计数器置初值0;
    (4) while(不是文件结束){
    读入一行到指定串中;
    求出串长度;
    行单词计数器0;
    调用模式匹配函数匹配单词定位、该行匹配单词计数;
    行号计数器加1;
    if(行单词计数器!=0)输出行号、该行有匹配单词的个数以及相应的位置;
    }
    4. 主控菜单程序的结构
    该部分内容如下:
    (1) 头文件包含;
    (2) 菜单选项包括:
    1. 建立文件
    2. 单词计数
    3. 单词定位
    4. 退出程序
    (3) 选择1-4执行相应的操作,其他字符为非法。

功能展示:
① 主界面
在这里插入图片描述
②加载文件
在这里插入图片描述

③:单词计数
在这里插入图片描述
④:单词检索
在这里插入图片描述

源代码:
在这里插入图片描述

MainJFrame

package ex3;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

public class MainJFrame extends JFrame implements ActionListener {
    JPanel functionPanel, textPanel;
    JButton button_file, button_count, button_search;
    JScrollPane scrollTxt;
    static JTextArea text;
    static Vector<String> vec = new Vector<String>();
    public MainJFrame(){
        super("单词检索统计系统 ");
        this.setLayout(new BorderLayout());
        this.setSize(450, 600);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //添加功能面板
        functionPanel = new JPanel(new FlowLayout());
        functionPanel.setBorder(BorderFactory.createLineBorder(Color.blue, 2));
        this.getContentPane().add(BorderLayout.NORTH, functionPanel);
        //添加按钮
        button_file = new JButton("加载文件");
        button_count = new JButton("单词计数");
        button_search = new JButton("单词检索");
        functionPanel.add(button_file);
        functionPanel.add(button_count);
        functionPanel.add(button_search);
        //添加监听器
        button_file.addActionListener(this);
        button_count.addActionListener(this);
        button_search.addActionListener(this);
        //添加文本面板
        textPanel = new JPanel();
        this.getContentPane().add(BorderLayout.CENTER, textPanel);
        //添加文本框
        text = new JTextArea(30,40);
        text.setBackground(Color.PINK);
        text.setLineWrap(true);
        text.setFont(new Font("楷体",Font.BOLD,16));
        scrollTxt = new JScrollPane(text);
        scrollTxt.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        textPanel.add(scrollTxt);

        this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == button_file){
            new ReadFile();
        }
        else if (e.getSource() == button_count){
            new WordCount();
        }
        else if(e.getSource() == button_search){
            new WordSearch();
        }
    }

    public static void main(String[] args){
        new MainJFrame();
    }
}

ReadFile

package ex3;


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

public class ReadFile extends JFrame implements ActionListener {
    JLabel label_path;
    JTextField textPath;
    JButton button_query;
    String path;
    public ReadFile(){
        super("加载文件");
        this.setLayout(new FlowLayout());
        this.setSize(360,160);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        label_path = new JLabel("路径:");
        this.getContentPane().add(label_path);
        textPath = new JTextField(25);
        this.getContentPane().add(textPath);
        button_query = new JButton("确定");
        this.add(button_query);
        button_query.addActionListener(this);

        this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        path = textPath.getText();
        if(e.getSource() == button_query){
            if(path == ""){
                JOptionPane.showMessageDialog(null, "请输入路径!");
            }
            else{
                String encoding = "utf-8";
                try{
                    MainJFrame.text.setText("");
                    File file = new File(path);
                    if (file.isFile() && file.exists()){
                        InputStreamReader read = new InputStreamReader(new FileInputStream(file),encoding);
                        BufferedReader bufferedReader = new BufferedReader(read);
                        String lineTxt = null;
                        while((lineTxt = bufferedReader.readLine()) != null){
                            MainJFrame.vec.add(lineTxt);
                            MainJFrame.text.append(lineTxt);
                            MainJFrame.text.append("\n");
                        }
                        read.close();
                    }
                    else{
                        System.out.println("文件不存在!");
                    }

                }
                catch(Exception ee){
                    System.out.println("读取文件出错");
                    ee.printStackTrace();
                }

//                MainJFrame.text.setText(MainJFrame.vec.toString());
                JOptionPane.showMessageDialog(null, "加载成功!");
            }
        }
    }

}

WordCount

package ex3;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class WordCount extends JFrame implements ActionListener {
    JLabel label_word;
    JTextField text_word;
    JButton button_word;
    JTextArea textAreaWord;
    JPanel panel1, panel2;
    JScrollPane scrollWord;
    public WordCount(){
        super("单词统计");
        this.setLayout(new BorderLayout());
        this.setSize(360,200);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        panel1 = new JPanel(new FlowLayout());
        this.getContentPane().add(BorderLayout.NORTH, panel1);
        label_word = new JLabel("单词:");
        panel1.add(label_word, FlowLayout.LEFT);
        text_word = new JTextField(10);
        panel1.add(text_word);
        button_word = new JButton("确认");
        panel1.add(button_word);
        button_word.addActionListener(this);

        panel2 = new JPanel();
        this.getContentPane().add(BorderLayout.CENTER, panel2);
        textAreaWord = new JTextArea(5, 30);
        textAreaWord.setBackground(Color.PINK);
        textAreaWord.setLineWrap(true);
        textAreaWord.setFont(new Font("楷体",Font.BOLD,16));
        scrollWord = new JScrollPane(textAreaWord);
        scrollWord.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        panel2.add(scrollWord);

        this.setVisible(true);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == button_word){
            textAreaWord.setText("");
            String s = text_word.getText();
            textAreaWord.setText(WordCount.wordCountFunc(s));
        }
    }
    public static String wordCountFunc(String s){
        int cnt = 0;
        boolean flag = false;
        for (int i = 0; i < MainJFrame.vec.size(); i++){
            String t = MainJFrame.vec.get(i);
            int k  = 0, m = 0;
            for (int j = 0; j < t.length() && j <= t.length() - s.length() + 1; j++){
                k = 0;
                m = j;
                while (k < s.length() && s.charAt(k) == t.charAt(m)){
                    k++;
                    m++;
                }
                if (k >= s.length())
                    cnt++;
            }
        }
        String t2 = "单词" + s + "出现次数:" + Integer.toString(cnt);
        return t2;
    }
}

WordSearch

package ex3;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class WordSearch extends JFrame implements ActionListener {
    JLabel label_word;
    JTextField text_word;
    JButton button_word;
    JTextArea textAreaWord;
    JPanel panel1, panel2;
    JScrollPane scrollWord;
    public WordSearch(){
        super("单词检索");
        this.setLayout(new BorderLayout());
        this.setSize(600,500);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        panel1 = new JPanel();
        this.getContentPane().add(BorderLayout.NORTH, panel1);
        label_word = new JLabel("单词:");
        panel1.add(label_word);
        text_word = new JTextField(10);
        panel1.add(text_word);
        button_word = new JButton("确认");
        panel1.add(button_word);
        button_word.addActionListener(this);

        panel2 = new JPanel();
        this.getContentPane().add(BorderLayout.CENTER, panel2);
        textAreaWord = new JTextArea(18, 40);
        textAreaWord.setBackground(Color.PINK);
        textAreaWord.setLineWrap(true);
        textAreaWord.setFont(new Font("楷体",Font.BOLD,16));
        scrollWord = new JScrollPane(textAreaWord);
        scrollWord.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        panel2.add(scrollWord);

        this.setVisible(true);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == button_word){
            textAreaWord.setText("");
            String s = text_word.getText();
            for (int i = 0; i < MainJFrame.vec.size(); i++){
                String ss = WordSearch.wordSearchFunc(s, MainJFrame.vec.get(i), i);
                if (ss != "")
                    textAreaWord.append(ss);
            }

        }
    }
    public static String wordSearchFunc(String s, String t, int n){
        int[] place = new int[100];
        int i = 0, j = 0, k = 0, index = 0, cnt = 0;
        boolean flag = false;
        for (i = 0; i <= t.length() - s.length() + 1; i++){
            j = i;
            k = 0;
            place[index] = i+1;
            while (k < s.length() && s.charAt(k) == t.charAt(j)){
                k++;
                j++;
            }
            if (k >= s.length()){
                flag = true;
                cnt++;
                index++;
            }
        }
        String ss;
        if (flag) {
            ss = "所在行数:" + Integer.toString(n + 1) + ", 出现次数:" + Integer.toString(cnt) + "出现位置:";
            for (i = 0; i < index; i++)
                ss = ss + "," + Integer.toString(place[i]);
            ss = ss + "\n";
        }
        else
            ss = "";
        return ss;
    }

}

源码链接:
链接:https://pan.baidu.com/s/1GrkFCencwzuvAFGqdx310Q
提取码:Code
复制这段内容后打开百度网盘手机App,操作更方便哦

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值