文本分类实验中用java实现取名词和去除停用词

来源:互联网 发布:淘宝可以说好评返现吗 编辑:程序博客网 时间:2024/06/06 00:06

这点小程序我调了一整天啊啊啊啊啊

本来打算用C语言写一个  可是弄了半天总是出错  上午几乎就在用C语言写了 

写的是:

#include<stdio.h>#include<string.h>#include<stdlib.h>void get_n(FILE* word_result,FILE* n_result);int enterchange(FILE *fp);void main(){for(int i=0;i<=9;i++){for(int j=0;j<=1999;j++){FILE *word_result,*n_result;char name_i[20],name_j[20];sprintf(name_i,"%d",i);sprintf(name_j,"%d",j);char file_path[100];char file_path_n[100];//file_path=strcat(strcat(strcat(strcat("E:\\datamining\\ICTCLAS50_Windows_32_JNI\\Sample\\Windows_32_jni_Demo\\file"\\,name_i),"\\"),"result_"),name_j);sprintf(file_path,"%s%d%s%d%s","E:\\datamining\\ICTCLAS50_Windows_32_JNI\\Sample\\Windows_32_jni_Demo\\file",i,"\\",j,".txt");sprintf(file_path_n,"%s%d%s%d%s","E:\\datamining\\ICTCLAS50_Windows_32_JNI\\Sample\\Windows_32_jni_Demo\\file",i,"\\",j,"result_n.txt");word_result = fopen(file_path,"r");n_result = fopen(file_path_n,"w");get_n(word_result, n_result);}//j}//i}void get_n(FILE* word_result,FILE* n_result){enterchange(word_result);char line[1024];while(fgets(line,1024,word_result)){for(int i=0;i<(int)strlen(line);i++){if(line[i]=='n'){fprintf(n_result,"%s",line);}}}//while}//get_nint enterchange(FILE *fp){int c;if(fp==NULL)return 1; while((c=fgetc(fp))!=EOF)if(c==' '){fseek(fp,-1L,1);fputc('\n',fp);fseek(fp,0L,1); }fclose(fp);return 0;}
也不知道是哪里出错了 

我的想法是把要检察的表中空格都换成回车 然后可以用fgets函数 获取一行 然后查找是不是存在n这个字符 存在就说明是名词 就存到一个新文件中 如果没有 就不存到新文件中

总之没达到效果 希望大神帮我看看怎么回事

然后同学给我传过来他的一段java代码:

//*************************取名词和去除停用词****************************************public static void refineNoun() throws IOException{String filestopwords = "E:/datamining/ICTCLAS50_Windows_32_JNI/Sample/Windows_32_jni_Demo/file/stop_words_ch.txt";//停用詞表File fsw = new File(filestopwords);BufferedReader stopreader = new BufferedReader(new FileReader(fsw));Set <String> setor = new HashSet <String>();String tempt = null;while((tempt = stopreader.readLine()) != null){setor.add(tempt);}System.out.println("set over!");for(int j=0;j<=9;j++){            for(int i=10;i<=1999;i++){String Outputfilename = "E:/datamining/ICTCLAS50_Windows_32_JNI/Sample/Windows_32_jni_Demo/file/"+j+"/"+"result_"+i+".txt";File file = new File(Outputfilename);if(!file.exists()) continue;BufferedReader reader = new BufferedReader(new FileReader(file));String filenewname = "E:/datamining/ICTCLAS50_Windows_32_JNI/Sample/Windows_32_jni_Demo/file/"+j+"/"+"result_data_"+i+".txt";File fileout = new File(filenewname);FileWriter fw = new FileWriter(fileout);   String tmp = null;try {while((tmp = reader.readLine()) !=null){String t = tmp.trim();if(t.equals("") ){continue;}else{tmp = " "+tmp;}//System.out.println(tmpString);    String[] str = tmp.split(" ");    for(int k=0;k<str.length;k++){    int  m = str[k].indexOf("/");        if(m != -1){    String x = str[k].substring(m, str[k].length());    String o = str[k].substring(0, m).replaceAll("[a-zA-Z0-9\\pP‘’“”]*", "");    if(x.indexOf('n') != -1 && !(setor.equals(o))){    fw.write(o+" ");    fw.flush();    }else{    k++;    }    }    }}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}fw.close();//System.out.println(i+"once over!");            }}}//******************************取名词和去除停用词***********************************

然后我出现的错误是  泛型什么的  使用了未经检察或不安全的操作

然后我加了一个<String>

就搞定啦

后来又出现的错误是我本来有10-19个文件 但是我的循环写的是i=10;i<=199;i++

总是出错 因为我根本不存在这么多文件 这属于异常

然后我就加了一个判断语句

判断文件是不是存在,如果不存在就continue

哇咔咔 好机智的我

0 0
原创粉丝点击