Java课程设计使用图形用户界面实现。任意选择一个文本文件,分析该文件,如果存在敏感词汇,报告每个词出现的次数。敏感词汇保存在sentive.txt文件中,每个词占一行,至少设置10个敏感词汇。在用户界面中可以对敏感词汇进行管理,添加删除修改查询。 扩展:不限于文本文件,可以是word文件,也可以是网络中某一个网页。代码怎么写
时间: 2023-12-10 18:41:49 浏览: 57
Java课程设计敏感词汇分析程序.doc
本题需要分为两部分来完成,一部分是实现敏感词汇的管理,另一部分是分析文本文件并报告敏感词汇出现的次数。
1. 敏感词汇管理
首先需要实现敏感词汇的添加、删除、修改和查询功能。这些功能可以通过使用Java的GUI工具包(如Swing)来实现。
添加敏感词汇:
用户可以在界面上输入敏感词汇,并点击添加按钮,将新的敏感词汇保存到sentive.txt文件中。
删除敏感词汇:
用户可以在界面上选择要删除的敏感词汇,并点击删除按钮,将敏感词汇从sentive.txt文件中删除。
修改敏感词汇:
用户可以在界面上选择要修改的敏感词汇,并点击修改按钮,弹出一个对话框,用户可以在对话框中输入新的敏感词汇,将原有的敏感词汇替换成新的敏感词汇。
查询敏感词汇:
用户可以在界面上输入关键字,并点击查询按钮,程序将在sentive.txt文件中查找包含该关键字的敏感词汇,并将结果显示在界面上。
2. 文本文件分析
读取文本文件:
用户可以在界面上选择要分析的文本文件,并点击打开按钮,程序将打开该文件并读取其中的内容。
分析文本文件:
程序将读取到的文本内容与sentive.txt文件中的敏感词汇进行比对,如果存在敏感词汇,则报告每个词出现的次数。
报告敏感词汇出现的次数:
程序将统计出现次数,并将结果显示在界面上。
代码示例:
```java
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.border.EmptyBorder;
public class SensitiveWordAnalyzer extends JFrame {
private JPanel contentPane;
private JTextField textFile;
private JTextField textKeyword;
private JList<String> listWords;
private Map<String, Integer> wordMap;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
SensitiveWordAnalyzer frame = new SensitiveWordAnalyzer();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public SensitiveWordAnalyzer() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblFile = new JLabel("File:");
lblFile.setBounds(10, 11, 46, 14);
contentPane.add(lblFile);
textFile = new JTextField();
textFile.setEditable(false);
textFile.setBounds(47, 8, 255, 20);
contentPane.add(textFile);
textFile.setColumns(10);
JButton btnOpen = new JButton("Open");
btnOpen.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
openFile();
}
});
btnOpen.setBounds(312, 7, 89, 23);
contentPane.add(btnOpen);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 36, 391, 94);
contentPane.add(scrollPane);
listWords = new JList<String>();
listWords.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
scrollPane.setViewportView(listWords);
JButton btnAdd = new JButton("Add");
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addWord();
}
});
btnAdd.setBounds(10, 141, 89, 23);
contentPane.add(btnAdd);
JButton btnDelete = new JButton("Delete");
btnDelete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
deleteWord();
}
});
btnDelete.setBounds(109, 141, 89, 23);
contentPane.add(btnDelete);
JButton btnModify = new JButton("Modify");
btnModify.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
modifyWord();
}
});
btnModify.setBounds(208, 141, 89, 23);
contentPane.add(btnModify);
JLabel lblKeyword = new JLabel("Keyword:");
lblKeyword.setBounds(10, 179, 46, 14);
contentPane.add(lblKeyword);
textKeyword = new JTextField();
textKeyword.setBounds(66, 176, 166, 20);
contentPane.add(textKeyword);
textKeyword.setColumns(10);
JButton btnSearch = new JButton("Search");
btnSearch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
searchKeyword();
}
});
btnSearch.setBounds(242, 175, 89, 23);
contentPane.add(btnSearch);
JButton btnAnalyze = new JButton("Analyze");
btnAnalyze.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
analyzeFile();
}
});
btnAnalyze.setBounds(341, 175, 89, 23);
contentPane.add(btnAnalyze);
wordMap = loadWords();
refreshList();
}
// 打开文件
private void openFile() {
JFileChooser chooser = new JFileChooser();
int ret = chooser.showOpenDialog(this);
if (ret == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
textFile.setText(file.getAbsolutePath());
}
}
// 添加敏感词汇
private void addWord() {
String word = JOptionPane.showInputDialog(this, "Enter sensitive word:");
if (word != null && !word.trim().isEmpty()) {
wordMap.put(word, 0);
saveWords();
refreshList();
}
}
// 删除敏感词汇
private void deleteWord() {
int index = listWords.getSelectedIndex();
if (index != -1) {
String word = listWords.getModel().getElementAt(index);
int ret = JOptionPane.showConfirmDialog(this, "Delete sensitive word \"" + word + "\"?", "Confirm", JOptionPane.YES_NO_OPTION);
if (ret == JOptionPane.YES_OPTION) {
wordMap.remove(word);
saveWords();
refreshList();
}
}
}
// 修改敏感词汇
private void modifyWord() {
int index = listWords.getSelectedIndex();
if (index != -1) {
String word = listWords.getModel().getElementAt(index);
String newWord = JOptionPane.showInputDialog(this, "Enter new sensitive word:", word);
if (newWord != null && !newWord.trim().isEmpty()) {
wordMap.remove(word);
wordMap.put(newWord, 0);
saveWords();
refreshList();
}
}
}
// 查询关键字
private void searchKeyword() {
String keyword = textKeyword.getText().trim();
if (!keyword.isEmpty()) {
StringBuilder sb = new StringBuilder();
for (String word : wordMap.keySet()) {
if (word.contains(keyword)) {
sb.append(word).append(" (").append(wordMap.get(word)).append(")").append("\n");
}
}
JOptionPane.showMessageDialog(this, sb.toString(), "Search Result", JOptionPane.PLAIN_MESSAGE);
}
}
// 分析文本文件
private void analyzeFile() {
String filename = textFile.getText().trim();
if (!filename.isEmpty()) {
File file = new File(filename);
if (file.exists() && file.isFile()) {
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line;
while ((line = reader.readLine()) != null) {
for (String word : wordMap.keySet()) {
int count = 0;
int pos = line.indexOf(word);
while (pos != -1) {
count++;
pos = line.indexOf(word, pos + word.length());
}
if (count > 0) {
int oldCount = wordMap.get(word);
wordMap.put(word, oldCount + count);
}
}
}
StringBuilder sb = new StringBuilder();
for (String word : wordMap.keySet()) {
int count = wordMap.get(word);
if (count > 0) {
sb.append(word).append(" (").append(count).append(")").append("\n");
}
}
JOptionPane.showMessageDialog(this, sb.toString(), "Analyze Result", JOptionPane.PLAIN_MESSAGE);
} catch (IOException e) {
JOptionPane.showMessageDialog(this, "Error: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
} else {
JOptionPane.showMessageDialog(this, "File not found: " + filename, "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
// 刷新敏感词汇列表
private void refreshList() {
String[] words = wordMap.keySet().toArray(new String[0]);
listWords.setListData(words);
}
// 加载敏感词汇列表
private Map<String, Integer> loadWords() {
Map<String, Integer> map = new HashMap<String, Integer>();
File file = new File("sentive.txt");
if (file.exists() && file.isFile()) {
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line;
while ((line = reader.readLine()) != null) {
map.put(line.trim(), 0);
}
} catch (IOException e) {
JOptionPane.showMessageDialog(this, "Error: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
} else {
JOptionPane.showMessageDialog(this, "File not found: sentive.txt", "Error", JOptionPane.ERROR_MESSAGE);
}
return map;
}
// 保存敏感词汇列表
private void saveWords() {
try (FileWriter writer = new FileWriter("sentive.txt")) {
for (String word : wordMap.keySet()) {
writer.write(word + "\n");
}
} catch (IOException e) {
JOptionPane.showMessageDialog(this, "Error: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
```
以上是一个简单的敏感词汇分析程序的实现,可以通过GUI界面进行敏感词汇的管理和文本文件的分析,并统计敏感词汇出现的次数。
阅读全文