【问题标题】:using stringtokenizer to count word issue使用 stringtokenizer 计算单词问题
【发布时间】:2013-06-05 23:48:20
【问题描述】:

我无法让我的程序正常运行。差不多完成了,除了这部分我已经工作了一个星期,但无法完成。程序应该计算每个单词出现的次数。

输入:

This is my file, yes my file My file.. ? ! , : ; / \ |" ^ * + = _( ) { } [ ] < >

输出应如下所示:

    file *3
    is *1
    my *3
    this *1
    yes *1

这是我的代码

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collections;

public class cleanup3 {

public cleanup3() {}

public static void main(String[] args) {   
  try{
     ArrayList myArraylist = new ArrayList();
     System.out.println("Please Enter file");

     InputStreamReader istream = new InputStreamReader(System.in) ;

     BufferedReader bufRead = new BufferedReader(istream) ;
     String fileName = bufRead.readLine();

     BufferedReader file = new BufferedReader(new FileReader(fileName));

     String s = null;
     while((s = file.readLine()) != null) {                      
         String updated2 = s.replaceAll("[\\.\\,\\?\\!\\:\\;\\/\\|\\\\\\^\\*\\+\\=\\_\\(\\)\\{\\}\\[\\]\\<\\>\"]+"," ");  

         //note to self: missing Single quotes (only if the LAST character of a token)
         StringTokenizer st = new StringTokenizer(updated2.toLowerCase());
         while (st.hasMoreTokens()) {
              String nextToken = st.nextToken();

              String myKeyValue = (String)myMap.get(nextToken);
              if(myKeyValue == null){
                  myMap.put(nextToken, "1");
              }
              else{
                  int mycount = Integer.parseInt(myKeyValue) + 1;
                  myMap.put(nextToken, String.valueOf(mycount));
              }
              System.out.println(nextToken);                           
           }   
        }
            System.out.println( updated2.toLowerCase());
            myArraylist.add(updated2.toLowerCase());                     
    }           
    Collections.sort(myArraylist);
    String outPutFileName =  fileName + "sorted.txt";         
    PrintStream ps = new PrintStream( outPutFileName );
    ps.print(myArraylist.toString());
    ps.flush();
    ps.close();        
  }
  catch (Exception e){
      System.out.println(e.toString());
  }
 }

【问题讨论】:

  • 您的输入文件是什么样的?你的程序现在做什么?
  • 替换所有的斜线太多了。大多数这些字符实际上不需要在字符类中转义。
  • 我的输入文件是这是我的文件,是的,我的文件是我的文件..? ! , : ; / \ |" ^ * + = _( ) { } [ ]
  • 所以你想统计单词的出现次数?
  • 在我尝试输入计数之前,我能够逃脱所有字符但没有计数器

标签: java


【解决方案1】:

你的代码太复杂了——你只需要几行代码。

这是一种优雅的方法:

Map<String, Integer> map = new TreeMap<String, Integer>();
for (String word : input.toLowerCase().replaceAll("[^a-z ]", "").trim().split(" +"))
    map.put(word, map.containsKey(word) ? map.get(word) + 1 : 1);
for (Map.Entry<String, Integer> entry : map.entrySet())
    System.out.println(entry.getKey() + " *" + entry.getValue());

输入:

  • 折叠成小写,处理大小写问题
  • 已删除所有非字母/空格,负责清理输入
  • 被修剪,使输入准备好进行拆分
  • 按 1-n 个空格分割
  • 被添加到累积总数的映射中,使用三元组来初始化单词 total

然后迭代映射条目以输出总数。

使用TreeMap 可以免费按字母顺序排序。

【讨论】:

    【解决方案2】:

    试试 BufferedReader 和 Regex,如下所示:

        Map<String, Integer> map = new HashMap<String, Integer>();
        String line;
        try (BufferedReader r = new BufferedReader(new FileReader(myFile))) {
                Pattern pattern = Pattern.compile("[a-zA-Z]+");
                while ((line = r.readLine())!=null) {
                    Matcher matcher = pattern.matcher(line);
                    while (matcher.find()) {
                        String word = matcher.group();
                        map.put(word, map.get(word) == null ? 1 : map.get(word)+1);
                    }
                }
        }
        System.out.println(map.toString());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-06
      相关资源
      最近更新 更多