diff --git a/dictionary/character.java b/dictionary/character.java new file mode 100644 index 0000000000000000000000000000000000000000..23647c0ca91abf20795bfa16a5dfe6252ec53356 --- /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 0000000000000000000000000000000000000000..febf109b8d0b145ca4c3a21a3d05211c545c6f06 --- /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 0000000000000000000000000000000000000000..728bb96623e31263f4d51300aa54c9223081e88a --- /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 0000000000000000000000000000000000000000..0d4408dc944410f1b509f156acbafd181c660105 --- /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 0000000000000000000000000000000000000000..0ffb7c4709185658e9564075c264dbd0ea255f4e --- /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.class b/dictionary/one.class new file mode 100644 index 0000000000000000000000000000000000000000..e0dad91f3d728063b52142c893abe5bbe8f6885c Binary files /dev/null and b/dictionary/one.class differ diff --git a/dictionary/one.java b/dictionary/one.java new file mode 100644 index 0000000000000000000000000000000000000000..a804d026d3c09976bf1c682ac0760f99a3558fc1 --- /dev/null +++ b/dictionary/one.java @@ -0,0 +1,133 @@ +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); + Object[] key = map.keySet().toArray(); + Arrays.sort(key); + for(Iterator> it=list.iterator();it.hasNext();){ + System.out.println(it.next()); + } + }catch(Exception e){ + e.printStackTrace(); + }finally{ + try{ + //关闭流,释放资源 + fis.close(); + }catch(Exception e){} + } + } +} + diff --git a/dictionary/open.java b/dictionary/open.java new file mode 100644 index 0000000000000000000000000000000000000000..6f3c249dcc2bcc71d50aaba866a42c06182e5c49 --- /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 0000000000000000000000000000000000000000..00cccb5b07de72638f7040f72cce12be75dcbc70 --- /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 0000000000000000000000000000000000000000..a859093533abbb0966f63840c357ec3b092ca7c5 --- /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 0000000000000000000000000000000000000000..4997dcc70fa9d3a1393a07c506027e8aa258b3fb --- /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 0000000000000000000000000000000000000000..115d1baf22f5a1cd23006cae75f8536cf42bf1a1 --- /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 0000000000000000000000000000000000000000..2747506a2ae94a6d0eb073dc40a84dd17ca8f91b --- /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); + } + +}