对于文章的字母、单词、短语,(无用词表)的检索Java代码实现
日期:2019.5.9
博客期:073
星期四
今天软件工程课上,又做了测试,老师说我们的速度太慢了,实际上我也觉得自己很慢。老师说了这是我们的上一届的大二上半学期学习中的速度,所以呢?意思就是说我们和上一届的学长学姐们相比差的是天与地的距离啊!emmmm......唉~我也承认了!以下是我提交的源码,请各位欣赏,有疑问的话,评论区里见!
源码:
1 package basic; 2 3 public class Chara { 4 public char str; 5 public int times; 6 }
char.Chara.java
1 package basic; 2 3 import java.io.File; 4 import java.util.ArrayList; 5 import java.util.List; 6 import java.util.Scanner; 7 8 public class Justice { 9 public List <Chara> list = new ArrayList<Chara>(); 10 public int length = 0; 11 public boolean isAccess(char c){ 12 return ((c>='a'&&c<='z')||(c>='A'&&c<='Z')); 13 } 14 public double p(char c){ 15 int seat = (changeToSmall(c)-'a'); 16 int time = list.get(seat).times; 17 return (double)((double)time/(double)length); 18 } 19 public char changeToSmall(char c){ 20 if(c>='a'&&c<='z') 21 return c; 22 else 23 return (char)(c-'A'+'a'); 24 } 25 public void dealFile(String filename) throws Exception{ 26 File f = new File(filename); 27 if(!f.exists()) 28 { 29 f.createNewFile(); 30 } 31 Scanner sc = new Scanner(f); 32 while(sc.hasNext()) 33 { 34 String s = sc.next(); 35 for(int t=0;t<s.length();++t) 36 { 37 char c = s.charAt(t); 38 if(this.isAccess(c)) 39 { 40 int seat = (changeToSmall(c)-'a'); 41 Chara ch = list.get(seat); 42 ch.times++; 43 list.set(seat, ch); 44 ++length; 45 } 46 } 47 } 48 49 sc.close(); 50 } 51 public void changeChannel(){ 52 int size = list.size(); 53 for(int i=0;i<size;++i) 54 { 55 for(int j=0;j<size-1;++j) 56 { 57 if(list.get(j).times<list.get(j+1).times) 58 { 59 Chara temp = list.get(j); 60 list.set(j,list.get(j+1)); 61 list.set(j+1, temp); 62 } 63 } 64 } 65 } 66 public void display(){ 67 int leng = list.size(); 68 for(int i=0;i<leng;++i) 69 { 70 char c = (char)(i+'a'); 71 System.out.println((char)(i+'a')+"出现了"+(list.get(i).times)+"次,它的频率是"+p(c)+"\t"); 72 } 73 } 74 public Justice(){ 75 for(char i='a';i<='z';++i) 76 { 77 Chara c = new Chara(); 78 c.str = i; 79 c.times = 0; 80 list.add(c); 81 } 82 } 83 }
Justice.java
1 package basic; 2 3 public class Main { 4 public static void main(String[] args) throws Exception { 5 Justice justice = new Justice(); 6 justice.dealFile("txt/piao.txt"); 7 justice.changeChannel(); 8 justice.display(); 9 } 10 }
Main.java
1 package more; 2 3 public class Chara { 4 public String str = ""; 5 public int times = 0; 6 public Chara(){ 7 8 } 9 public Chara(String str,int times){ 10 this.str = str; 11 this.times = times; 12 } 13 }
word.Chara.java
1 package more; 2 3 import java.io.File; 4 import java.io.FileNotFoundException; 5 import java.util.ArrayList; 6 import java.util.List; 7 import java.util.Scanner; 8 9 public class EditTable{ 10 11 public List<Chara> list = new ArrayList<Chara>(); 12 public List<String> listString = new ArrayList<String>(); 13 public int length = 0; 14 public boolean isAddmitted = false; 15 //单词处理 16 public String changeWord(String str){ 17 String newStr = str.replace("“",""); 18 newStr = newStr.replace(",",""); 19 newStr = newStr.replace("”",""); 20 newStr = newStr.replace(".",""); 21 newStr = newStr.replace("?",""); 22 newStr = newStr.replace("!",""); 23 newStr = newStr.replace(":",""); 24 return newStr.toLowerCase(); 25 } 26 public boolean isInUnUseTable(String str){ 27 int leng = listString.size(); 28 for(int i=0;i<leng;++i) 29 { 30 if(str.toLowerCase().compareTo(listString.get(i))==0) 31 return true; 32 } 33 return false; 34 } 35 //判断是否可以通过 36 public boolean isAccess(String str){ 37 int leng = str.length(); 38 str = this.changeWord(str); 39 40 if(isAddmitted) 41 { 42 if(isInUnUseTable(str)) 43 return false; 44 } 45 46 47 for(int i=0;i<leng;++i) 48 { 49 char c = str.charAt(i); 50 if(!((c>='a'&&c<='z')||(c>='A'&&c<='Z'))) 51 return false; 52 } 53 return true; 54 } 55 public boolean isAddmitted() { 56 return isAddmitted; 57 } 58 public void setAddmitted(boolean isAddmitted) { 59 this.isAddmitted = isAddmitted; 60 } 61 //判断位置 62 public int seatAt(String c){ 63 int leng = list.size(); 64 for(int i=0;i<leng;++i) 65 { 66 String trp = list.get(i).str; 67 if(trp.compareTo(c)==0) 68 return i; 69 } 70 return -1; 71 } 72 //判断并添加 73 public void AddString(String str){ 74 if(isAccess(changeWord(str))) 75 { 76 int seat = seatAt(changeWord(str)); 77 if(seat==-1) 78 { 79 Chara chara = new Chara(changeWord(str),1); 80 list.add(chara); 81 } 82 else 83 { 84 Chara chara = list.get(seat); 85 chara.times++; 86 list.set(seat, chara); 87 } 88 ++length; 89 } 90 } 91 //排序 92 public void changeChannel(){ 93 int size = list.size(); 94 for(int i=0;i<size;++i) 95 { 96 for(int j=0;j<size-1;++j) 97 { 98 if(list.get(j).times<list.get(j+1).times) 99 { 100 Chara temp = list.get(j); 101 list.set(j,list.get(j+1)); 102 list.set(j+1, temp); 103 } 104 } 105 } 106 } 107 //概率 108 public double p(String str){ 109 int seat = seatAt(changeWord(str)); 110 if(seat==-1) 111 return 0; 112 int time = list.get(seat).times; 113 return ((double)time/(double)length); 114 } 115 //展示 116 public void display(int seat){ 117 Chara cha = list.get(seat); 118 System.out.println("单词:"+cha.str+(cha.str.length()<=2?"\t\t":"\t")+"出现次数:"+cha.times+"\t"+"概率为:"+((double) Math.round(p(cha.str) * 10000) / 100)+"%\t"); 119 } 120 public void Display(int N){ 121 for(int i=0;i<N&&i<list.size();++i) 122 display(i); 123 } 124 public EditTable() throws FileNotFoundException{ 125 126 Scanner sc = new Scanner(new File("txt/stopword.txt")); 127 while(sc.hasNext()) 128 { 129 String str = sc.next(); 130 listString.add(str); 131 } 132 } 133 public EditTable(boolean isAd) throws FileNotFoundException{ 134 this.isAddmitted = isAd; 135 Scanner sc = new Scanner(new File("txt/stopword.txt")); 136 while(sc.hasNext()) 137 { 138 String str = sc.next(); 139 listString.add(str); 140 } 141 } 142 //处理文件 143 public void DealFile(String fileName) throws Exception{ 144 File f = new File(fileName); 145 Scanner sc = new Scanner(f); 146 while(sc.hasNext()) 147 { 148 String str = sc.next(); 149 AddString(str); 150 } 151 sc.close(); 152 } 153 154 }
EditTable.java
1 package more; 2 3 import java.io.File; 4 import java.io.FileNotFoundException; 5 import java.util.ArrayList; 6 import java.util.List; 7 import java.util.Scanner; 8 9 import more.Chara; 10 11 public class Table { 12 public List<Chara> list = new ArrayList<Chara>(); 13 public List<String> listString = new ArrayList<String>(); 14 public int length = 0; 15 public boolean isAddmitted = false; 16 //单词处理 17 public String changeWord(String str){ 18 String newStr = str.replace("“",""); 19 newStr = newStr.replace(",",""); 20 newStr = newStr.replace("”",""); 21 newStr = newStr.replace(".",""); 22 newStr = newStr.replace("?",""); 23 newStr = newStr.replace("!",""); 24 newStr = newStr.replace(":",""); 25 return newStr.toLowerCase(); 26 } 27 public static boolean isContainSpecifical(String str){ 28 if(str.contains("“")) 29 return false; 30 if(str.contains("”")) 31 return false; 32 if(str.contains(",")) 33 return false; 34 if(str.contains(".")) 35 return false; 36 if(str.contains("?")) 37 return false; 38 if(str.contains("!")) 39 return false; 40 if(str.contains(":")) 41 return false; 42 return true; 43 } 44 public boolean isInUnUseTable(String str){ 45 int leng = listString.size(); 46 for(int i=0;i<leng;++i) 47 { 48 if(str.toLowerCase().compareTo(listString.get(i))==0) 49 return true; 50 } 51 return false; 52 } 53 //判断是否可以通过 54 public boolean isAccess(String str){ 55 int leng = str.length(); 56 str = this.changeWord(str); 57 58 if(isAddmitted) 59 { 60 if(isInUnUseTable(str)) 61 return false; 62 } 63 64 65 for(int i=0;i<leng;++i) 66 { 67 char c = str.charAt(i); 68 if(!((c>='a'&&c<='z')||(c>='A'&&c<='Z'))) 69 return false; 70 } 71 return true; 72 } 73 public boolean isAddmitted() { 74 return isAddmitted; 75 } 76 public void setAddmitted(boolean isAddmitted) { 77 this.isAddmitted = isAddmitted; 78 } 79 //判断位置 80 public int seatAt(String c){ 81 int leng = list.size(); 82 for(int i=0;i<leng;++i) 83 { 84 String trp = list.get(i).str; 85 if(trp.compareTo(c)==0) 86 return i; 87 } 88 return -1; 89 } 90 //判断并添加 91 public void AddString(String str){ 92 if(isAccess(changeWord(str))) 93 { 94 int seat = seatAt(changeWord(str)); 95 if(seat==-1) 96 { 97 Chara chara = new Chara(changeWord(str),1); 98 list.add(chara); 99 } 100 else 101 { 102 Chara chara = list.get(seat); 103 chara.times++; 104 list.set(seat, chara); 105 } 106 ++length; 107 } 108 } 109 //排序 110 public void changeChannel(){ 111 int size = list.size(); 112 for(int i=0;i<size;++i) 113 { 114 for(int j=0;j<size-1;++j) 115 { 116 if(list.get(j).times<list.get(j+1).times) 117 { 118 Chara temp = list.get(j); 119 list.set(j,list.get(j+1)); 120 list.set(j+1, temp); 121 } 122 } 123 } 124 } 125 //概率 126 public double p(String str){ 127 int seat = seatAt(changeWord(str)); 128 if(seat==-1) 129 return 0; 130 int time = list.get(seat).times; 131 return ((double)time/(double)length); 132 } 133 //展示 134 public void display(int seat){ 135 Chara cha = list.get(seat); 136 System.out.println("单词:"+cha.str+(cha.str.length()<=2?"\t\t":"\t")+"出现次数:"+cha.times+"\t"+"概率为:"+((double) Math.round(p(cha.str) * 10000) / 100)+"%\t"); 137 } 138 public void Display(int N){ 139 for(int i=0;i<N&&i<list.size();++i) 140 display(i); 141 } 142 public Table() throws FileNotFoundException{ 143 144 Scanner sc = new Scanner(new File("txt/stopword.txt")); 145 while(sc.hasNext()) 146 { 147 String str = sc.next(); 148 listString.add(str); 149 } 150 } 151 public Table(boolean isAd) throws FileNotFoundException{ 152 this.isAddmitted = isAd; 153 Scanner sc = new Scanner(new File("txt/stopword.txt")); 154 while(sc.hasNext()) 155 { 156 String str = sc.next(); 157 listString.add(str); 158 } 159 } 160 //读取 161 public void AddMenu(List <String> sqlList){ 162 int length = sqlList.size(); 163 String ss = ""; 164 if(length!=0) 165 ss = sqlList.get(length-1); 166 for(int i=length-2;i>=0;--i) 167 { 168 ss = sqlList.get(i) + " " + ss; 169 AddString(ss); 170 } 171 } 172 //处理文件 173 public void DealFile(String fileName) throws Exception{ 174 File f = new File(fileName); 175 Scanner sc = new Scanner(f); 176 boolean isT = true; 177 while(sc.hasNext()) 178 { 179 while(isT&&sc.hasNext()) 180 { 181 List <String> strSql = new ArrayList<String>(); 182 String str = sc.next(); 183 if(isInUnUseTable(str)) 184 break; 185 strSql.add(str); 186 AddMenu(strSql); 187 if(Table.isContainSpecifical(str)) 188 break; 189 } 190 } 191 sc.close(); 192 } 193 }
Table.java
1 package running; 2 3 import java.io.File; 4 import java.io.FileWriter; 5 import java.io.PrintWriter; 6 import java.util.Scanner; 7 8 import more.EditTable; 9 import more.Table; 10 11 import basic.Justice; 12 13 public class TestMain { 14 //第0步 15 public static void process0(String file) throws Exception{ 16 Justice justice = new Justice(); 17 justice.dealFile(file); 18 justice.changeChannel(); 19 justice.display(); 20 } 21 //第1步 22 public static void process1(String file) throws Exception{ 23 EditTable table = new EditTable(); 24 table.DealFile(file); 25 table.changeChannel(); 26 table.Display(99999999); 27 } 28 //第2步 29 public static void process2(String file,int num) throws Exception{ 30 EditTable table = new EditTable(); 31 table.DealFile(file); 32 table.changeChannel(); 33 table.Display(num); 34 } 35 //第3步 36 public static void process3(String file,int num) throws Exception{ 37 EditTable table = new EditTable(true); 38 table.DealFile(file); 39 table.changeChannel(); 40 table.Display(num); 41 } 42 //第4步 43 public static void process4(String file,int num) throws Exception{ 44 Table table = new Table(); 45 table.DealFile(file); 46 table.changeChannel(); 47 table.Display(999999); 48 } 49 //停用词 50 public static void stopWord(String file) throws Exception{ 51 File f = new File("txt/stopword.txt"); 52 PrintWriter pw = new PrintWriter(new FileWriter(f,true)); 53 pw.println(); 54 pw.println(file); 55 pw.close(); 56 } 57 //主 58 public static void main(String[] args) throws Exception { 59 Scanner sc = new Scanner(System.in); 60 boolean isAccess = true; 61 while(isAccess) 62 { 63 System.out.println("==============================================="); 64 System.out.println(" p0 + 文件路径 -----------> 分析字母"); 65 System.out.println(" p1 + 文件路径 -----------> 分析单词"); 66 System.out.println(" p2 + 文件路径 + 前几项 -----------> 分析频率最高的单词"); 67 System.out.println(" p3 + 文件路径 + 前几项 -----------> 分析频率最高的单词(过滤)"); 68 System.out.println(" sw + 单词 -----------> 添加停用词"); 69 System.out.println(" p4 + 文件路径 + 前几项 -----------> 分析短语"); 70 System.out.println(" q -----------> 退出"); 71 System.out.println(); 72 String order = sc.next(); 73 String file = sc.next(); 74 if(order.compareTo("p0")==0) 75 { 76 TestMain.process0(file); 77 } 78 else if(order.compareTo("p1")==0) 79 { 80 TestMain.process1(file); 81 } 82 else if(order.compareTo("p2")==0) 83 { 84 int num = sc.nextInt(); 85 TestMain.process2(file,num); 86 } 87 else if(order.compareTo("p3")==0) 88 { 89 int num = sc.nextInt(); 90 TestMain.process3(file,num); 91 } 92 else if(order.compareTo("sw")==0) 93 { 94 TestMain.stopWord(file); 95 } 96 else if(order.compareTo("p4")==0) 97 { 98 int num = sc.nextInt(); 99 TestMain.process4(file,num); 100 } 101 else if(order.compareTo("q")==0) 102 { 103 System.out.println("欢迎下次使用!"); 104 break; 105 } 106 System.out.println("==============================================="); 107 } 108 sc.close(); 109 } 110 }
TestMain.java
1 package more; 2 3 import java.io.File; 4 5 public class Test { 6 public static void TestForPath(String path) throws Exception{ 7 File file = new File(path); 8 if(file.isDirectory()) 9 { 10 File []fl = file.listFiles(); 11 int leng = fl.length; 12 for(int i=0;i<leng;++i) 13 { 14 String fileName = path+"/"+fl[i].getName(); 15 EditTable table = new EditTable(); 16 System.out.println("===================================================="); 17 System.out.println("文件名称:"+fl[i].getName()); 18 table.DealFile(fileName); 19 table.changeChannel(); 20 table.Display(40); 21 } 22 } 23 } 24 public static void main(String[] args) throws Exception { 25 TestForPath("txt"); 26 } 27 }
Test.java
1 package running; 2 3 import java.io.File; 4 import java.io.FileWriter; 5 import java.io.PrintWriter; 6 import java.util.Scanner; 7 8 import more.EditTable; 9 import more.Table; 10 11 import basic.Justice; 12 13 public class TestMain { 14 //第0步 15 public static void process0(String file) throws Exception{ 16 Justice justice = new Justice(); 17 justice.dealFile(file); 18 justice.changeChannel(); 19 justice.display(); 20 } 21 //第1步 22 public static void process1(String file) throws Exception{ 23 EditTable table = new EditTable(); 24 table.DealFile(file); 25 table.changeChannel(); 26 table.Display(99999999); 27 } 28 //第2步 29 public static void process2(String file,int num) throws Exception{ 30 EditTable table = new EditTable(); 31 table.DealFile(file); 32 table.changeChannel(); 33 table.Display(num); 34 } 35 //第3步 36 public static void process3(String file,int num) throws Exception{ 37 EditTable table = new EditTable(true); 38 table.DealFile(file); 39 table.changeChannel(); 40 table.Display(num); 41 } 42 //第4步 43 public static void process4(String file,int num) throws Exception{ 44 Table table = new Table(); 45 table.DealFile(file); 46 table.changeChannel(); 47 table.Display(999999); 48 } 49 //停用词 50 public static void stopWord(String file) throws Exception{ 51 File f = new File("txt/stopword.txt"); 52 PrintWriter pw = new PrintWriter(new FileWriter(f,true)); 53 pw.println(); 54 pw.println(file); 55 pw.close(); 56 } 57 //主 58 public static void main(String[] args) throws Exception { 59 Scanner sc = new Scanner(System.in); 60 boolean isAccess = true; 61 while(isAccess) 62 { 63 System.out.println("==============================================="); 64 System.out.println(" p0 + 文件路径 -----------> 分析字母"); 65 System.out.println(" p1 + 文件路径 -----------> 分析单词"); 66 System.out.println(" p2 + 文件路径 + 前几项 -----------> 分析频率最高的单词"); 67 System.out.println(" p3 + 文件路径 + 前几项 -----------> 分析频率最高的单词(过滤)"); 68 System.out.println(" sw + 单词 -----------> 添加停用词"); 69 System.out.println(" p4 + 文件路径 + 前几项 -----------> 分析短语"); 70 System.out.println(" q -----------> 退出"); 71 System.out.println(); 72 String order = sc.next(); 73 String file = sc.next(); 74 if(order.compareTo("p0")==0) 75 { 76 TestMain.process0(file); 77 } 78 else if(order.compareTo("p1")==0) 79 { 80 TestMain.process1(file); 81 } 82 else if(order.compareTo("p2")==0) 83 { 84 int num = sc.nextInt(); 85 TestMain.process2(file,num); 86 } 87 else if(order.compareTo("p3")==0) 88 { 89 int num = sc.nextInt(); 90 TestMain.process3(file,num); 91 } 92 else if(order.compareTo("sw")==0) 93 { 94 TestMain.stopWord(file); 95 } 96 else if(order.compareTo("p4")==0) 97 { 98 int num = sc.nextInt(); 99 TestMain.process4(file,num); 100 } 101 else if(order.compareTo("q")==0) 102 { 103 System.out.println("欢迎下次使用!"); 104 break; 105 } 106 System.out.println("==============================================="); 107 } 108 sc.close(); 109 } 110 }
TestMain.java
附上截图:
转载于:https://www.cnblogs.com/onepersonwholive/p/10872391.html
对于文章的字母、单词、短语,(无用词表)的检索Java代码实现相关推荐
- 面试题:选择一篇英语文章,统计文章中所有单词出现的次数,单词的长度以及每个单词中每个字母出现的次数
选择一篇英语文章,统计文章中所有单词出现的次数,单词的长度以及每个单词中每个字母出现的次数 这里我们把这个题目可以分为三个步骤也就是三个方法: 统计单词中每个字符出现的次数 统计每个单词在文章中出现的 ...
- 由文章中英文字母出现频率分析破解密文
原题: 使用频率分析法,尝试破译密文: WB WI KJB MK RMIT BMIQ BJ RASHMWK RMVP YJERYRKB MKD WBI IWOKWXWVMKVR MKDIJYR YNI ...
- 英语教材提取单词制作生词表
文章目录 英语教材提取单词制作生词表 教材分解成单词 查英语字典生成单词表 英语教材提取单词制作生词表 教材分解成单词 vim下: 以非英语字符为界,切割所有文本 :%s/\W/\r/g 删除重复单词 ...
- Leetcode69场双周赛-第三题5962. 连接两字母单词得到的最长回文串
5962. 连接两字母单词得到的最长回文串 题目描述 解题思路和解题代码 定义map和sameMap.如果两个字母一样,就看sameMap里面有没有记录,有的话把该记录取出来,结果总数+4,没有的话, ...
- python 统计一篇英语文章中每个单词出现的次数
""" 统计一篇英语文章中每个单词出现的次数 """ import stringdef get_dict_word_times(file): ...
- android拼单词游戏源码,Android拼字母单词文字游戏源码Fives
Android拼字母单词文字游戏源码Fives,玩家需要从系统给出的5个字母中尽可能多的拼成不同的单词,每拼成一个单词计时器时间就会减少,使用Android Studio 2.3.x开发,支持Andr ...
- 论文写作课堂总结3:慎用的单词短语
论文写作课堂总结3:慎用的单词短语 在这堂课上,闵老师讲解了在学术论文写作钟需要注意的单词短语使用.在总结课堂上的单词短语之外,联系了<国际论文写作>的课程后,对这节课有了更深的记忆.中外 ...
- java 统计文章中每个单词出现的次数
java 统计文章中每个单词出现的次数 思路:扫描文章,使用正则表达式分割出一个个单词, 然后把这个单词放到map<String,Integer>集合中作为key,同时它的value置1, ...
- python如何删除代码_Python如何删除除字母和数字之外的所有字符?(代码示例)
字符串操作是日常编码和Web开发中非常重要的任务:例如:HTTP查询中的大多数请求和响应都是字符串形式,有时我们需要删除一些无用的数据.下面本篇文章就来给大家介绍一些Python方法来将指定字符串中除 ...
- java 计算i 出现的次数_JAVA算法:按照给定的段落统计单词出现次数(JAVA代码)...
https://blog.csdn.net/seagal890/article/details/92067644 JAVA算法:按照给定的段落统计单词出现次数(JAVA代码) 写一个 JAVA程序以统 ...
最新文章
- 在ApacheHTTPD服务器中使用DSO完全分析
- mysql 允许远程登录
- 认识计算机硬件观评课,观课听课评课评语
- Vue入门 ---- 仿百度搜索
- Javascript数组操作
- 5.MySQL优化---索引优化专题
- Mac磁盘项目管理工具DiskCatalogMaker
- 人工智能你必须掌握的32个算法(二)归并排序算法
- 零基础怎么学计算机编程!看完的你,应该会恍然大悟!
- 3D 世界的钥匙「GitHub 热点速览 v.22.08」
- Bex5文档服务器,不通过登录直接打开BeX5的首页和功能页的url是什么?
- BAT、360、网易等大公司开源项目
- 原来这就是公文写作领导讲话稿模板(3)
- 【贪玩巴斯】带你一起攻克英语语法长难句—— 第六章——英语的特殊结构 ——2022年3月19日-20日
- 蚁群算法画图java_[转载]简单蚁群算法 + JAVA实现蚁群算法
- Windows应急响应
- 笔记本安装固态涉及到的注意事项
- ad如何绘制拼版_Altium Design PCB拼板完整教程,这样讲就明白了!
- python爬取证券数据并存入数据库
- net start mysql 提示:发生系统错误 2。 系统找不到指定的文件。