From b9c6cb53db8cd65bb34a2f0616a62a817b9c2a69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BD=97=E4=BD=B3?= <768071758@qq.com> Date: Sat, 15 Sep 2018 22:01:51 +0800 Subject: [PATCH 1/2] first --- dictionary/one.class | Bin 0 -> 924 bytes dictionary/one.java | 131 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 dictionary/one.class create mode 100644 dictionary/one.java diff --git a/dictionary/one.class b/dictionary/one.class new file mode 100644 index 0000000000000000000000000000000000000000..e0dad91f3d728063b52142c893abe5bbe8f6885c GIT binary patch literal 924 zcmZuv?QRlL5IuJnxUeqv3)(7DTdm4hi`HtjRI0R^U}!PHm=Jy~aNBH!-3=^8U&9yh zr@!?FBw~C3-^16a&fU_6+HSHlyK~RXoH=uUAOH9TU>&;}rm@Je#Bqz`wgw*-gIJdS zjx={QsJN#=K|-2j2q~mFG8)2I(XfiF4BzK?z_G><*fK5C%`>QpAUdL1w49zxJ%rI_m^(d{ z$(Njr5i0b|R#S9nrrph~jz@T`V*^iQo2Sxj>d0Y>pGbn9bG*<|kQqCY zol0!aLCf_c(Qp}*T7hBalwGlWHZqB8IF7KI3@eFCRJ#xw@y?Mnu6?2{LnM*B03<~8 zvWm2a`B9u?923b~k&lEVF8)NVkSv}XrShrk3es4$do)n=q#spyL+c$afLd}&EEeUm z&!!y%P+>>H$v0YdS6~TKbW`P{fKm`48>PQEc}Cu!8i0MH54u%k1746LggMMpiiCa! zryC8Fhfu0RsPy`(1Nf%{eejgJMz)XO0HFc2(v%NBgL`T|o$2FBHLrZZ?04uxgsT}f zKETyJCcggtlkQ_uHdVZ)3)BG6uuSYy)G|ZS4I=OwG3?_8uF+{2$}ut=*EwP=PC*bS L=8du76}tHk>Nw2j literal 0 HcmV?d00001 diff --git a/dictionary/one.java b/dictionary/one.java new file mode 100644 index 0000000..0b06097 --- /dev/null +++ b/dictionary/one.java @@ -0,0 +1,131 @@ +package dictionary; + +import java.util.*; +import java.util.Map.Entry; +import java.util.concurrent.ConcurrentHashMap; +import java.io.FileInputStream; + +/** + * 使用FileInputStream读取文件 + */ +public class one { + private static class ValueComparator implements Comparator>{ + public int compare(Map.Entry mp1, Map.Entry mp2){ + return mp2.getValue() - mp1.getValue(); + } + } + + public static void main(String[] args) { + //声明流对象 + FileInputStream fis = null; + + try{ + //创建流对象 + fis = new FileInputStream("D:/"+args[0]); + //读取数据,并将读取到的数据存储到数组中 + byte[] data = new byte[1024]; //数据存储的数组 + int kong=0; + int t=0; + int n=0; + byte[] rold = new byte[10]; + int flag=0; + Map map = new ConcurrentHashMap(); + //List> list=new ArrayList<>(); + + int i = fis.read(data); + + //解析数据 + String s = new String(data,0,i); + //输出字符串 + System.out.println(s.toLowerCase()); + for(int x=0;x<=i;x++) { + switch(data[x]) { + case 32: + kong++;break; + case 9: + t++;break; + case 10: + n++;break; + } + + if(data[x]>=65&&data[x]<=90) { + rold[flag]=data[x]; + flag++; + } + else if(data[x]>=97&&data[x]<=122) { + rold[flag]=data[x]; + flag++; + } + + else if(data[x]==32||data[x]==10||data[x]==13||data[x]==0){ + if(flag>3) { + String str = new String(rold).trim(); + Iterator> it = map.entrySet().iterator(); + if(it.hasNext()) { + while(it.hasNext()) { + Entry entry = it.next(); + + if(entry.getKey().equals(str)) { + + map.put(str, entry.getValue()+1); + flag=0; + rold = new byte[10]; + break; + } + else { + + map.put(str, 1); + flag=0; + rold = new byte[10]; + } + + } + + } + else { + map.put(str, 1); + flag=0; + rold = new byte[10]; + + } + } + else { + flag=0; + rold = new byte[10]; + } + } + else if(data[x]>=48&&data[x]<=57) { + + if(data[x]!=32&&flag>3) { + rold[flag]=data[x]; + flag++; + } + else { + flag=0; + rold = new byte[10]; + } + } + } + System.out.println("空格数为:"+kong); + System.out.println("水平制表符数为:"+t); + System.out.println("换行符数为:"+n); + + + List> list=new ArrayList<>(); + list.addAll(map.entrySet()); + one.ValueComparator vc=new ValueComparator(); + Collections.sort(list,vc); + for(Iterator> it=list.iterator();it.hasNext();){ + System.out.println(it.next()); + } + }catch(Exception e){ + e.printStackTrace(); + }finally{ + try{ + //关闭流,释放资源 + fis.close(); + }catch(Exception e){} + } + } +} + -- Gitee From 14c12a16909c7262ca182f892e27b92f82ea4d9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BD=97=E4=BD=B3?= <768071758@qq.com> Date: Wed, 19 Sep 2018 16:02:17 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E7=AC=AC=E4=BA=8C=E6=AC=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dictionary/character.java | 23 ++++++ dictionary/characterTest.java | 24 +++++++ dictionary/comper.java | 18 +++++ dictionary/comperTest.java | 52 ++++++++++++++ dictionary/main.java | 19 +++++ dictionary/one.java | 2 + dictionary/open.java | 40 +++++++++++ dictionary/openTest.java | 39 ++++++++++ dictionary/word.java | 127 +++++++++++++++++++++++++++++++++ dictionary/wordTest.java | 43 +++++++++++ dictionary/wordsimple.java | 47 ++++++++++++ dictionary/wordsimpleTest.java | 22 ++++++ 12 files changed, 456 insertions(+) create mode 100644 dictionary/character.java create mode 100644 dictionary/characterTest.java create mode 100644 dictionary/comper.java create mode 100644 dictionary/comperTest.java create mode 100644 dictionary/main.java create mode 100644 dictionary/open.java create mode 100644 dictionary/openTest.java create mode 100644 dictionary/word.java create mode 100644 dictionary/wordTest.java create mode 100644 dictionary/wordsimple.java create mode 100644 dictionary/wordsimpleTest.java diff --git a/dictionary/character.java b/dictionary/character.java new file mode 100644 index 0000000..23647c0 --- /dev/null +++ b/dictionary/character.java @@ -0,0 +1,23 @@ +package dictionary; + +public class character{ + int kong=0; + int t=0; + int n=0; + public character(byte[] data,int i) { + + for(int x=0;x<=i;x++) { + switch(data[x]) { + case 32: + kong++;break; + case 9: + t++;break; + case 10: + n++;break; + } + } + System.out.println("空格数为:"+kong); + System.out.println("水平制表符数为:"+t); + System.out.println("换行符数为:"+n); + } +} diff --git a/dictionary/characterTest.java b/dictionary/characterTest.java new file mode 100644 index 0000000..febf109 --- /dev/null +++ b/dictionary/characterTest.java @@ -0,0 +1,24 @@ +package dictionary; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +public class characterTest { + + @Before + public void setUp() throws Exception { + } + + @Test + public void testCharacter() { + + String str = "Hello201621123036 hello \r\n"; + byte[] data = str.getBytes(); + character c=new character(data,26); + assertEquals(2,c.kong); + assertEquals(1,c.t); + assertEquals(1,c.n); + } +} diff --git a/dictionary/comper.java b/dictionary/comper.java new file mode 100644 index 0000000..728bb96 --- /dev/null +++ b/dictionary/comper.java @@ -0,0 +1,18 @@ +package dictionary; + +import java.util.Comparator; +import java.util.Map; + +public class comper { + public static class ValueComparator implements Comparator>{ + public int compare(Map.Entry mp1, Map.Entry mp2){ + return mp2.getValue() - mp1.getValue(); + } + } + public static class keycomparator implements Comparator>{ + public int compare(Map.Entry o1, + Map.Entry o2) { + return (o1.getKey()).toString().compareTo(o2.getKey().toString()); + } + } +} diff --git a/dictionary/comperTest.java b/dictionary/comperTest.java new file mode 100644 index 0000000..0d4408d --- /dev/null +++ b/dictionary/comperTest.java @@ -0,0 +1,52 @@ +package dictionary; + +import static org.junit.Assert.*; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.concurrent.ConcurrentHashMap; + +import org.junit.Before; +import org.junit.Test; + +public class comperTest { + + @Before + public void setUp() throws Exception { + } + + @Test + public void test() { + Map map = new ConcurrentHashMap(); + map.put("temp", 2); + map.put("apple", 2); + map.put("bettle", 1); + map.put("counting", 1); + List> infoIds = new ArrayList>(map.entrySet()); + comper.keycomparator kc=new dictionary.comper.keycomparator(); + Collections.sort(infoIds, kc); + + comper.ValueComparator vc=new dictionary.comper.ValueComparator(); + Collections.sort(infoIds,vc); + Iterator> it=infoIds.iterator(); + + Entry entry= it.next(); + assertEquals("apple",entry.getKey()); + assertEquals(2,entry.getValue()); + entry= it.next(); + assertEquals("temp",entry.getKey()); + assertEquals(2,entry.getValue()); + entry= it.next(); + assertEquals("bettle",entry.getKey()); + assertEquals(1,entry.getValue()); + entry= it.next(); + assertEquals("counting",entry.getKey()); + assertEquals(1,entry.getValue()); + + } + +} diff --git a/dictionary/main.java b/dictionary/main.java new file mode 100644 index 0000000..0ffb7c4 --- /dev/null +++ b/dictionary/main.java @@ -0,0 +1,19 @@ +package dictionary; + +public class main { + + public main(String[] str) { + // TODO Auto-generated constructor stub + } + + public static void main(String[] args) { + // TODO Auto-generated method stub + open f=new open(); + byte[] data=f.open(args); + int i=f.geti(); + new character(data,i); + new wordsimple(data,i); + word w=new word(); + w.word(data,i); + } + } diff --git a/dictionary/one.java b/dictionary/one.java index 0b06097..a804d02 100644 --- a/dictionary/one.java +++ b/dictionary/one.java @@ -115,6 +115,8 @@ public class one { list.addAll(map.entrySet()); one.ValueComparator vc=new ValueComparator(); Collections.sort(list,vc); + Object[] key = map.keySet().toArray(); + Arrays.sort(key); for(Iterator> it=list.iterator();it.hasNext();){ System.out.println(it.next()); } diff --git a/dictionary/open.java b/dictionary/open.java new file mode 100644 index 0000000..6f3c249 --- /dev/null +++ b/dictionary/open.java @@ -0,0 +1,40 @@ +package dictionary; + +import java.io.FileInputStream; + +public class open { + + int i; + public byte[] open(String[] args){ + //声明流对象 + FileInputStream fis = null; + byte[] data = new byte[2048]; //数据存储的数组 + + try{ + + //创建流对象 + fis = new FileInputStream("D:/"+args[0]); + //读取数据,并将读取到的数据存储到数组中 + + i = fis.read(data); + + //解析数据 + String s = new String(data,0,i); + //输出字符串 + System.out.println(s.toLowerCase()); + + }catch(Exception e){ + //e.printStackTrace(); + System.out.println("系统找不到指定的文件。"); + }finally{ + try{ + //关闭流,释放资源 + fis.close(); + }catch(Exception e){} + } + return data; + } + public int geti() { + return i; + } +} diff --git a/dictionary/openTest.java b/dictionary/openTest.java new file mode 100644 index 0000000..00cccb5 --- /dev/null +++ b/dictionary/openTest.java @@ -0,0 +1,39 @@ +package dictionary; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +public class openTest { + + @Before + public void setUp() throws Exception { + } + + @Test + public void testOpen() { + open f=new open(); + String[] str= {"test.txt"}; + byte[] data=f.open(str); + String s = new String(data).trim(); + assertEquals("java201621123036 sdsd",s); + } + + @Test + public void testOpen1() { + open f=new open(); + String[] str= {"app.txt"}; + byte[] data=f.open(str); + String s = new String(data).trim(); + + } + + @Test + public void testGeti() { + open f=new open(); + String[] str= {"test.txt"}; + byte[] data=f.open(str); + assertEquals(22,f.geti()); + } +} diff --git a/dictionary/word.java b/dictionary/word.java new file mode 100644 index 0000000..a859093 --- /dev/null +++ b/dictionary/word.java @@ -0,0 +1,127 @@ +package dictionary; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.concurrent.ConcurrentHashMap; + +public class word { + public Iterator> word(byte[] data,int i){ + byte[] rold = new byte[20]; + int flag=0; + Map map = new ConcurrentHashMap(); + int rf=0; + for(int x=0;x<=i;x++) { //遍历byte数组 + if(data[x]>=65&&data[x]<=90) { //如果为大写改为小写 + data[x]=(byte) (data[x]+32); + rold[flag]=data[x]; //是字母计入rold数组 + flag++; //统计单词长度 + } + else if(data[x]>=97&&data[x]<=122) {//是小写字母计入数组 + rold[flag]=data[x]; + flag++; //放入数组单词长度加一 + } + + else if(data[x]==32||data[x]==10||data[x]==13||data[x]==0||data[x]==9){//如是空格回车TAB分隔符一类的进入判断 + if(flag>3) { //字母数长度超过3可以算作一个单词 + String str = new String(rold).trim(); //使rold数组转为string并取掉多余的空格 + Iterator> it = map.entrySet().iterator();//用迭代器遍历判断是否有相同的单词已经存入 + if(it.hasNext()) { + while(it.hasNext()) { + Entry entry = it.next(); + if(entry.getKey().equals(str)) {//如有相同的单词,value+1 + map.put(str, entry.getValue()+1); + flag=0; //单词长度清空 + rold = new byte[20]; //数组清空 + rf=1; //记录是否放入了map + break; + } + } + if(rf!=1) { //循环完了,没放入需要放入 + map.put(str, 1); + flag=0; + rold = new byte[20]; + } + rf=0; //置0再次使用 + } + else { //如果map里没值直接put + map.put(str, 1); + flag=0; + rold = new byte[20]; + } + } + else { //如果不满足4个字母数,清空 + flag=0; + rold = new byte[20]; + } + } + else if(data[x]>=48&&data[x]<=57) { //如碰到特殊字符判断下一个字符是不是空并且字母数超过3个可以计入单词 + if(data[x]!=32&&flag>3) { + rold[flag]=data[x]; + flag++; + } + else { //如是空格或者不到4个字母清空 + flag=0; + rold = new byte[20]; + while(data[x+1]!=32) { //以特殊字符和数字开头的都不算单词 + x++; + if(x==i) + break; + } + } + } + } //遍历完之后最后一个单词可能大于3个字母数需要计入数组运行如上 + if(flag>3) { + String str = new String(rold).trim(); + Iterator> it = map.entrySet().iterator(); + if(it.hasNext()) { + while(it.hasNext()) { + Entry entry = it.next(); + + if(entry.getKey().equals(str)) { + + map.put(str, entry.getValue()+1); + flag=0; + rold = new byte[20]; + break; + } + else { + + map.put(str, 1); + flag=0; + rold = new byte[20]; + break; + } + + } + + } + else { + map.put(str, 1); + flag=0; + rold = new byte[20]; + + } + } + + + List> infoIds = new ArrayList>(map.entrySet()); + comper.keycomparator kc=new dictionary.comper.keycomparator(); + Collections.sort(infoIds, kc); + + comper.ValueComparator vc=new dictionary.comper.ValueComparator(); + Collections.sort(infoIds,vc); + Iterator> it=infoIds.iterator(); + + for(int num=0;num<10;num++){ + if(it.hasNext()) { + Entry entry= it.next(); + System.out.println(entry.getKey()+":"+entry.getValue()); // 获取key + } + } + return it; + } +} diff --git a/dictionary/wordTest.java b/dictionary/wordTest.java new file mode 100644 index 0000000..4997dcc --- /dev/null +++ b/dictionary/wordTest.java @@ -0,0 +1,43 @@ +package dictionary; + +import static org.junit.Assert.*; + +import java.util.Iterator; +import java.util.Map; +import java.util.Map.Entry; + +import org.junit.Before; +import org.junit.Test; + +public class wordTest { + + @Before + public void setUp() throws Exception { + } + + @Test + public void testWord() { + String str = "hello hello2 Hello /r/n hEllo java201621123036 1file apple"; + byte[] data = str.getBytes(); + word w=new word(); + Iterator> it=w.word(data, str.length()-1); + + + Entry entry= it.next(); + assertEquals("hello",entry.getKey()); + assertEquals("3",entry.getValue()); + entry= it.next(); + assertEquals("apple",entry.getKey()); + assertEquals("1",entry.getValue()); + entry= it.next(); + assertEquals("hello2",entry.getKey()); + assertEquals("1",entry.getValue()); + entry= it.next(); + assertEquals("hello201621123036",entry.getKey()); + assertEquals("1",entry.getValue()); + + + + + } +} diff --git a/dictionary/wordsimple.java b/dictionary/wordsimple.java new file mode 100644 index 0000000..115d1ba --- /dev/null +++ b/dictionary/wordsimple.java @@ -0,0 +1,47 @@ +package dictionary; + +public class wordsimple { + public int num=0; + public wordsimple(byte[] data,int i){ + byte[] rold = new byte[20]; + int flag=0; + + for(int x=0;x<=i;x++) { + + if(data[x]>=65&&data[x]<=90) { + data[x]=(byte) (data[x]+32); + rold[flag]=data[x]; + flag++; + } + else if(data[x]>=97&&data[x]<=122) { + rold[flag]=data[x]; + flag++; + } + + else if(data[x]==32||data[x]==10||data[x]==13||data[x]==0){ + if(flag>3) { + num++; + flag=0; + rold = new byte[20]; + + } + else { + flag=0; + rold = new byte[20]; + } + } + else if(data[x]>=48&&data[x]<=57) { + + if(data[x]!=32&&flag>3) { + rold[flag]=data[x]; + flag++; + } + else { + flag=0; + rold = new byte[20]; + } + } + } + System.out.println("单词数为:"+num); + } +} diff --git a/dictionary/wordsimpleTest.java b/dictionary/wordsimpleTest.java new file mode 100644 index 0000000..2747506 --- /dev/null +++ b/dictionary/wordsimpleTest.java @@ -0,0 +1,22 @@ +package dictionary; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +public class wordsimpleTest { + + @Before + public void setUp() throws Exception { + } + + @Test + public void testWordsimple() { + String str = "Hello201621123036 hello Hello \r\n heLLo be cass+ cas- 1file 2file"; + byte[] data = str.getBytes(); + wordsimple w=new wordsimple(data,str.length()-1); + assertEquals(5,w.num); + } + +} -- Gitee