[英]storing keywords in set and reading them using maps Java
我創建了一個包含文本的文件。
我想使用Set
來存儲這些關鍵字,並使用Map
顯示所有關鍵字和重復次數,以讀取諸如“ end”,“ so”和“ because”之類的特定單詞。
你能告訴我我應該怎么做嗎?
...
openButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
int chosenFile = fileChooser.showOpenDialog(null);
if(chosenFile == JFileChooser.APPROVE_OPTION){
File selectedFile = fileChooser.getSelectedFile();
if ( selectedFile.canRead()){
Set<String> keywordList = new HashSet<String>();
keywordList.add("and");
keywordList.add("so");
keywordList.add("because”);
...
我不知道該如何使用Map
來優化關鍵字。
只需使用Map
作為Set
映射的鍵即可。
Map<String, Integer> keywordCount = new HashMap<String, Integer>();
keywordCount.add("and", 0);
keywordCount.add("so", 0);
keywordCount.add("because”, 0);
Set<String> keywords = keywordCount.keySet();
openButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
int chosenFile = fileChooser.showOpenDialog(null);
if(chosenFile == JFileChooser.APPROVE_OPTION){
File selectedFile = fileChooser.getSelectedFile();
if ( selectedFile.canRead()){
Set<String> keywordList = new HashSet<String>();
Map<String, Integer> keywordCount = new HashMap<String, Integer>();
keywordList.add("and");
keywordList.add("so");
keywordList.add("because”);
StringBuffer fileContent = new StringBuffer();
java.io.BufferedReader br;
try{
// Read file content
br = new java.io.BufferedReader(new java.io.InputStreamReader(new java.io.FileInputStream(selectedFile)));
String line = null;
while((line = br.readLine()) != null){
fileContent.append(line).append("\n");
}
}catch(java.io.IOException ioe){
ioe.printStackTrace();
}finally{
if(br != null){
br.close();
}
}
// Get the number of occurrences for each keyword.
Iterator<String> iterKeywords = keywordList.iterator();
while(iterKeywords.hasNext()){
String currentKeyword = iterKeywords.next();
Pattern p = Pattern.compile(currentKeyword);
Matcher m = p.matcher(fileContent.toString());
int count = 0;
while(m.find()){
count ++;
}
keywordCount.put(currentKeyword, new Integer(count));
}
System.out.println(keywordCount);
}
}
}
關於從文件中讀取文本,您可以遵循此處介紹的一些方法。
現在,關於提取關鍵字,您可以跳過Set
部分,直接從文本轉到Map
。
根據您的問題,我假設您已經有了關鍵字。
使用Apache Commons commons-lang3-3.1 StringUtils,您可以執行以下操作:
public static Map<String, Integer> countKeywords(String text, List<String> keywords) {
Map<String, Integer> result = new HashMap<>();
for (String keyword : keywords) {
int count = StringUtils.countMatches(text, keyword);
result.put(keyword, count);
}
return result;
}
並像這樣測試它:
public static void main(String args[]) {
String text = "Mirror mirror on the wall wall, true hope lies beyond the coast...";
List<String> keywords = new ArrayList<>();
keywords.add("mirror");
keywords.add("wall");
keywords.add("sword");
Map<String, Integer> result = countKeywords(text, keywords);
for (Map.Entry<String, Integer> entry : result.entrySet()) {
System.out.println("keyword: " + entry.getKey() + " >>> count: " + entry.getValue());
}
}
我只是將countKeywords
方法countKeywords
靜態,因此您可以復制/粘貼進行測試。
不要忘記下載commons-lang3-3.1.jar並將其添加到您的類路徑中。
該測試輸出如下內容:
keyword: mirror >>> count: 1
keyword: wall >>> count: 2
keyword: sword >>> count: 0
希望對您有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.