Hey guys I have got a question...we have to do an assignment where we have to translate normal text code into morse code in Processing (Java). We also have to calculate entropy and redundancy. The translation is working, but we can't figure out how we can calculate the probability for all the chars at ones to calculate the entropy.
import controlP5.*;
import java.util.Map;
// dot = one unit
// dash = three units
// space between parts of the same letter = one unit
// space between letter = three units
// space between words is seven units
float x= 50;
float y= 450;
float w= 500;
float h = 80;
ControlP5 cp5;
String input;
String convert_string;
String output;
String temp;
Textarea myTextarea;
Textarea addTextarea;
String[] alphabet = {
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", " "
};
String[] morse = {
"._", "_...", "_._.", "_..", ".", ".._.", "__.", "....", "..", ".___", "_._", "._..", "__",
"_.", "___", ".__.", "__._", "._.", "...", "_", ".._", "..._", ".__", "_.._", "_.__", "__..", "/"
};
int countLetter(String str, char zeichen) {
str = str.toLowerCase();
int count = 0;
for (int i = 0; i < str.length(); i++) {
char currentLetter = str.charAt(i);
if (currentLetter == zeichen)
count++;
}
return count;
}
double getAverageCharLength(String str) {
String words[] = str.split("/");
int numWords = words.length;
int totalCharacters = 0;
for (int i = 0; i < numWords; i++) {
totalCharacters = totalCharacters + words[i].length();
}
return totalCharacters/numWords;
}
void setup() {
size(600, 600);
convert_string= "";
temp = "";
input = "";
output = "";
cp5 = new ControlP5(this);
cp5.addTextfield("Input").setPosition(50, 100).setSize(100, 50).setAutoClear(false);
cp5.addBang("Submit").setPosition(50, 200).setSize(100, 50);
myTextarea = cp5.addTextarea("Output")
.setPosition(200, 100)
.setSize(300, 300)
.setFont(createFont("arial", 20))
.setLineHeight(25)
.setColor(color(255))
.setColorBackground(color(255, 100))
.setColorForeground(color(255, 100));
;
addTextarea = cp5.addTextarea("AddOutput")
.setPosition(200, 450)
.setSize(300,100)
.setFont(createFont("arial", 14))
.setLineHeight(14)
.setColor(color(255))
.setColorBackground(color(255, 100))
.setColorForeground(color(255, 100));
;
}
void draw() {
background(0);
}
public void Submit() {
input = cp5.get(Textfield.class, "Input").getText();
convert_string = input.toLowerCase();
float otput = output.length();
float oput =0;
for (int i=0; i<convert_string.length(); i++) {
for (int j=0; j<alphabet.length; j++) {
if (String.valueOf(convert_string.charAt(i)).equals(alphabet[j])) {
output += morse[j]+' ';
otput= otput-1;
}
}
}
double entropie = 0;
double wortlaenge = 0;
double charProbability=0;
float curCharCount= 0;
float inputLength = input.length();
float entcounter=0;
// Anzahl der einzelnen Zeichen im Eingabestring
HashMap<Character,Integer> map = new HashMap<Character,Integer>();
for(int i = 0; i < input.length(); i++){
char c = input.toLowerCase().charAt(i);
Integer val = map.get(c);
if(val != null){
map.put(c, new Integer(val + 1));
}else{
map.put(c,1);
}
// iteriert über Zeichen der Eingabe und berechnet Entropie und Wortl?nge
for (HashMap.Entry<Character, Integer> me : map.entrySet()) {
curCharCount = me.getValue();
charProbability = curCharCount / input.length();
// Wortl?nge für aktuelles Zeichen aus Morsekode-Map holen
// if (convert_string.get(me.getKey())) != null) {
// wortlaenge += charProbability * get(me.getKey()).length();
}
}
//entropie=entropie/entcounter;
oput= output.length()+otput;
wortlaenge += charProbability * oput;
entropie = charProbability * (Math.log(charProbability) / Math.log(2));
println(wortlaenge);
println(charProbability);
println(entropie);
println(inputLength);
}
//entropie = -entropie;
// double redundanz = wortlaenge - entropie;
/* println(output);
temp= output;
myTextarea.setText(output);
output = "";
addTextarea.setText("Durchschnittliche Wortl?nge : "+ getAverageCharLength(temp) +"
");
println(countLetter(temp, '_'));
println(countLetter(temp, '.'));
println(countLetter(temp, '/'));
*/
//}
question from:https://stackoverflow.com/questions/65940680/entropielist-for-morsecode