字符串处理中如何处理缩略词 本帖最后由 cobasta 于 2012-03-10 21:34:51 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public void read(){ try{ Map< String, Integer> map=new HashMap<String, Integer>(); int all=0; for (File file : new File("e:\\newgroups\\alt.atheism").listFiles()) { //顺次统计文件夹下的每个文件 InputStreamReader in=new InputStreamReader(new FileInputStream(file)); int b; String str=""; while((b=in.read())!=-1){ char ch=(char)b; if(!Character.isLetter(ch)) { //读取字符,如果不是字母,则把之前积累的字符串加到MAP结构中 if(str!="") { Integer n=map.get(str); //区分原有字符串和新增字符串 map.put(str,n==null?1:n+1); all++; str=""; } continue; } else {//字符是字母,添加到字符串后部 if(Character.isUpperCase(ch)) ch=Character.toLowerCase(ch);//大写字母转换为小写 str+=ch; } } System.out.println(all); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } 这不是MapReduce的wordcount算法嘛不过那个是分布式的,当我没说好了 = =我觉得缩略词应该跟原词没啥关系,算两种不同的单词就好了。 菜鸟关于插入包的问题 多线程 synchronize关键字用法 以及虚拟机对不同线程的调度 用正则表达式如何提取绝对路径下的文件名? 如何让String输出一个空字符 输出main函数参数失败。。 急救,SWT中,如何为窗体添加一个背景图片? 长型向短型数据转换问题 新手上路,如何把.class搞成可执行文件? java消息系统的问题 ??JNDI 和支付网关(在线等待,参与也有分啊)?? 这算不算网上银行可能的安全漏洞??? 菜鸟求教高手关于打包的问题
try{
Map< String, Integer> map=new HashMap<String, Integer>();
int all=0;
for (File file : new File("e:\\newgroups\\alt.atheism").listFiles()) {
//顺次统计文件夹下的每个文件
InputStreamReader in=new InputStreamReader(new FileInputStream(file));
int b;
String str="";
while((b=in.read())!=-1){
char ch=(char)b;
if(!Character.isLetter(ch))
{
//读取字符,如果不是字母,则把之前积累的字符串加到MAP结构中
if(str!="") {
Integer n=map.get(str);
//区分原有字符串和新增字符串
map.put(str,n==null?1:n+1);
all++;
str="";
}
continue;
}
else
{//字符是字母,添加到字符串后部
if(Character.isUpperCase(ch))
ch=Character.toLowerCase(ch);//大写字母转换为小写
str+=ch;
}
}
System.out.println(all);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}