-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArithmeticCodingFloatCompress.java
143 lines (129 loc) · 6.16 KB
/
ArithmeticCodingFloatCompress.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class ArithmeticCodingFloatCompress {
public static HashMap<Character, Integer> FrequencyForCharInString(String string){
HashMap<Character, Integer> map = new HashMap<>();
for(int i = 0; i < string.length(); i++)
{
if(map.containsKey(string.charAt(i))) {
map.put(string.charAt(i), map.get(string.charAt(i)) + 1);
}
else {
map.put(string.charAt(i), 1);
}
}
return map;
}
public static HashMap<Character, Float> ProbablityForCharInString(String string){
HashMap<Character, Integer> map = new HashMap<>();
map=FrequencyForCharInString(string);
HashMap<Character, Float> ProbMap = new HashMap<>();
for(Map.Entry<Character, Integer> counter: map.entrySet()){
ProbMap.put( counter.getKey() , ((float)counter.getValue()/(float)string.length()));
}
return ProbMap;
}
public static ArrayList<Float> prob(HashMap<Character, Float> map){
ArrayList<Float> Low=new ArrayList<>();
int i=0;
for(Map.Entry<Character, Float> counter: map.entrySet()){
Low.add(counter.getValue());
}
return Low;
}
public static HashMap<Character, Float> LoweMap( HashMap<Character, Float> map){
HashMap<Character, Float> mapLow = new HashMap<>();
ArrayList<Float> basicBrobablity=prob( map);
ArrayList<Float> lowRange =new ArrayList<>();
ArrayList<Float> highRange=new ArrayList<>();
int i=0;
for(Map.Entry<Character, Float> counter: map.entrySet()){
if(i==0) {
lowRange.add((float) 0);
highRange.add(counter.getValue());
mapLow.put( counter.getKey() , (float) i);
}
else{
lowRange.add(highRange.get(i-1));
highRange.add(counter.getValue()+lowRange.get(i));
mapLow.put( counter.getKey() , lowRange.get(i));
}
i++;
}
return mapLow;
}
public static HashMap<Character, Float> HigheMap(HashMap<Character, Float> map){
HashMap<Character, Float> mapHigh = new HashMap<>();
ArrayList<Float> highRange=new ArrayList<>();
int i=0;
for(Map.Entry<Character, Float> counter: map.entrySet()){
if(i==0) {
highRange.add(counter.getValue());
mapHigh.put( counter.getKey() , highRange.get(i));
}
else{
highRange.add(counter.getValue()+highRange.get(i-1));
}
i++;
}
int j=0;
for(Map.Entry<Character, Float> counter: map.entrySet()){
highRange.add(counter.getValue());
mapHigh.put( counter.getKey() , highRange.get(j));
j++;
}
return mapHigh;
}
public static HashMap<Character, Float> RangeMap( HashMap<Character, Float> map){
HashMap<Character, Float> mapRangeLow = new HashMap<>();
HashMap<Character, Float> mapRangeUpper = new HashMap<>();
HashMap<Character, Float> mapRange = new HashMap<>();
mapRangeLow=LoweMap(map);
mapRangeUpper=HigheMap(map);
int j=0;
for(Map.Entry<Character, Float> counter: map.entrySet()){
mapRange.put( counter.getKey() ,counter.setValue(mapRangeUpper.get(counter.getKey())-mapRangeLow.get(counter.getKey())));
j++;
}
return mapRange;
}
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
String inputTextWillCompress=input.nextLine();
HashMap<Character, Float> mapCharAndProbab = new HashMap<>();
HashMap<Character, Float> mapRangeLow = new HashMap<>();
HashMap<Character, Float> mapRangeUpper = new HashMap<>();
HashMap<Character, Float> mapRange = new HashMap<>();
mapCharAndProbab=ProbablityForCharInString(inputTextWillCompress);
mapRangeLow=LoweMap(mapCharAndProbab);
mapRangeUpper=HigheMap(mapCharAndProbab);
mapRange=RangeMap(mapCharAndProbab);
System.out.println("High Range : "+mapRangeUpper);
System.out.println("Low Range : "+ mapRangeLow);
System.out.println("Range : "+ mapRange);
Character tempraryChar=inputTextWillCompress.charAt(0);
for(int i=1;i<inputTextWillCompress.length();i++){
/*
* Lower(symbol)=lower(الحرف اللي قبلي)+Range(الحرف اللي قبلي )*low(symbol)
* Upper(symbol)=upper(الحرف اللي قبلي)+Range(الحرف اللي قبلي )*upper(symbol)
* */
mapRangeLow.put(inputTextWillCompress.charAt(i),
(mapRangeLow.get(tempraryChar)+(mapRangeUpper.get(tempraryChar)-mapRangeLow.get(tempraryChar))*mapRangeLow.get(inputTextWillCompress.charAt(i))));
mapRangeUpper.put(inputTextWillCompress.charAt(i),
(mapRangeLow.get(tempraryChar)+(mapRangeUpper.get(tempraryChar)-mapRangeLow.get(tempraryChar))*mapRangeUpper.get(inputTextWillCompress.charAt(i))));
tempraryChar=inputTextWillCompress.charAt(i);
/*
System.out.println(" Character is : "+tempraryChar);
System.out.println("the lower range of Charcter is : "+mapRangeLow.get(tempraryChar));
System.out.println("the Upper range of Charcter is : "+mapRangeUpper.get(tempraryChar));
System.out.println("------------------------------------------------------------------------ ");
System.out.println("------------------------------------------------------------------------ ");
System.out.println("------------------------------------------------------------------------ ");
*/
}
System.out.println("Float for compress data is : "+((mapRangeLow.get(tempraryChar)+mapRangeUpper.get(tempraryChar))/2));
}
}