首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Eclipse开发 >

怎么去掉英文txt中的stopword?

2012-09-18 
如何去掉英文txt中的stopword???现有一个待处理的txt的文件(test.txt),还有一个是停用词的文件(stopword.t

如何去掉英文txt中的stopword???
现有一个待处理的txt的文件(test.txt),还有一个是停用词的文件(stopword.txt)。。。现想去掉停用词,不知各位大神有什么好的方法??? 我的初步思路是:依次读取字符串,每个单词与文件(stopword.txt)对比,相同则去掉该词、、、、但感觉该方法比较麻烦。 谁能提供程序???语言最好是java 不过其他能处理也可

[解决办法]

Java code
public static void main(String[] args) throws IOException  {                        String test  = readFile("E:/test.txt");        String stopword  = readFile("E:/stopword.txt");        stopword="("+(stopword.replace("\n", "|"))+")";        System.out.println(test.replaceAll(stopword, ""));    }        public static String readFile(String fileName) throws IOException{        BufferedReader read = new BufferedReader(new InputStreamReader(new FileInputStream(new File(fileName))));        String content = null;        StringBuffer sbu = new StringBuffer();        while((content=read.readLine())!=null){            sbu.append(content).append("\n");        }        if(sbu.length()!=0)sbu.deleteCharAt(sbu.length()-1);        return sbu.toString();    }
[解决办法]
楼主看起来对编程实在是不熟悉?

五楼的方案不太妥,应该是按词过滤而不是直接用replace来处理。

建议分成2步:
1、将stopword装载到内存中备查;这个就是我4楼代码做的事情;
2、逐个单词读取test.txt,然后检查其是否为stopword,如果是就忽略,不是就将其输出。

第二步也很简单,类似如下:
Java code
Scanner scTest = new Scanner(new File("test.txt"));while (scTest.hasNext()) {   String word = scTest.next();   if (!stops.contains(word)) { // 检查是否为敏感词      System.out.print(word); // 输出该单词      System.out.print(" "); // 输出空格   } }scTest.close(); 

热点排行