0

I created a file that contains a text.

I want to read specific words like "end", "so", and "because" using Set to store these keywords and using Map to display all the keywords and the number of times repeated.

Can you show me how I should go about doing that?

...
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”);
...

I don’t know how I could go from here to use Map to fine the keyword.

rbento
  • 5,069
  • 1
  • 40
  • 51
Marocain
  • 11
  • 3

3 Answers3

0

Just use a Map as the keys in a map form a 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();
rbento
  • 5,069
  • 1
  • 40
  • 51
BevynQ
  • 7,616
  • 4
  • 21
  • 34
  • Thanks for your reply. I still don't know how I can link that to the file. I think I need to use Scanner but I don't know how to code that. – Marocain Mar 08 '13 at 02:45
  • try [how-to-read-word-by-word-from-file](http://stackoverflow.com/questions/8825320/how-to-read-word-by-word-from-file) – BevynQ Mar 08 '13 at 02:48
0
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);
        }
    }
}
Ravindra Gullapalli
  • 8,591
  • 3
  • 41
  • 66
0

About reading text from file you could follow some approaches presented here.

Now about extracting the keywords, you could just skip the Set part and go straight from text to a Map.

As per you question I assume you already have the keywords.

Using Apache Commons commons-lang3-3.1 StringUtils, you could do something like this:

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;
}

And test it like this:

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());
    }
}

I just made the countKeywords method static so you could copy/paste to test it.

Don't forget to download commons-lang3-3.1.jar and add it to your classpath.

This test outputs something like this:

keyword: mirror >>> count: 1
keyword: wall >>> count: 2
keyword: sword >>> count: 0

I hope it helps.

Community
  • 1
  • 1
rbento
  • 5,069
  • 1
  • 40
  • 51
  • You're welcome. Please, kindly accept my answer if it was helpful. If answers are helpful and you accept them, more people would get motivated to help you in case you need it again. Hugs! See stackoverflow.com/faq#reputation – rbento Mar 10 '13 at 18:29