LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Small matlab code to convert Morse code to plaintext using regular expressions (http://www.linuxquestions.org/questions/programming-9/small-matlab-code-to-convert-morse-code-to-plaintext-using-regular-expressions-799098/)

gregorian 03-31-2010 06:49 AM

Small matlab code to convert Morse code to plaintext using regular expressions
 
Code:

stri='.---- -. ..--- .. .--- .-.. --... ....- .- .' % Input string
stri = strcat(' ', stri); % Make every morse 'character' begin with a space
morse = {'\.-' '-\.\.\.' '-\.-\.' '-\.\.' '\.' '\.\.-\.' '--\.' '\.\.\.\.' '\.\.' '\.---' '-\.-' '\.-\.\.' '--' '-\.' '---' '\.--\.' '--\.-' '\.-\.' '\.\.\.' '-' '\.\.-' '\.\.\.-' '\.--' '-\.\.-' '-\.--' '--\.\.' '-----' '\.----' '\.\.---' '\.\.\.--' '\.\.\.\.-' '\.\.\.\.\.' '-\.\.\.\.' '--\.\.\.' '---\.\.' '----\.'};
code='ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
for i=1:size(morse,2)      % For loop till end of the morse array
 str = strcat(' ',morse(i));   
 stri = regexprep(stri,str,code(i))     % if any morse character is found, replace it by the corresponding alphabet
end

stri is the Morse string I want to translate to plaintext. Every element in the array morse is mapped to the corresponding character in the string code by index i.e. the first element in morse is the 'A' and so on.

The algorithm is simple: if any element in the morse array is present in stri, replace it by the corresponding alphabet. But this code doesn't work and produces lots of wrong characters.

Can you find the error?

Sergei Steshenko 03-31-2010 07:00 AM

Quote:

Originally Posted by gregorian (Post 3919015)
Code:

stri='.---- -. ..--- .. .--- .-.. --... ....- .- .'
stri = strcat(' ', stri);
morse = {'\.-' '-\.\.\.' '-\.-\.' '-\.\.' '\.' '\.\.-\.' '--\.' '\.\.\.\.' '\.\.' '\.---' '-\.-' '\.-\.\.' '--' '-\.' '---' '\.--\.' '--\.-' '\.-\.' '\.\.\.' '-' '\.\.-' '\.\.\.-' '\.--' '-\.\.-' '-\.--' '--\.\.' '-----' '\.----' '\.\.---' '\.\.\.--' '\.\.\.\.-' '\.\.\.\.\.' '-\.\.\.\.' '--\.\.\.' '---\.\.' '----\.'};
code='ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
for i=1:size(morse,2)
 str = strcat(' ',morse(i));   
 stri = regexprep(stri,str,code(i))
end

stri is the Morse string I want to translate to plaintext. Every element in the array morse is mapped to the corresponding character in the string code by index i.e. the first element in morse is the 'A' and so on.

The algorithm is simple: if any element in the morse array is present in stri, replace it by the corresponding alphabet. But this code doesn't work and produces lots of wrong characters.

Can you find the error?


What did you do to find the error ? What did you verify ?

dsmyth 03-31-2010 10:31 AM

hello, i wouldn't do it like this... it's about as readable as the fourth edition of NIST_JANAF thermochemical tables.

create a file that maps morse code to letter one per line seperated by comma...
then read this into a dictionary and then it's real simple to parse the morse code.

In fact I'm going to code it up in ... emmmmm .... Ruby... post it in a minute.

*EDIT: Ok the file will look like this and is called codes.txt
A|.-
B|-...
C|-.-.
... and so on

this is then read in to a dictionary that looks something like this....

{ '.-'=>'A', '-...'=>'B', ... and so on }

then it becomes real easy to parse a message, split the message into individual morse code letters and use the dictionary to look up the corresponding alphabetic letter.

Here is the Ruby code, the idea is the same no matter what language you write it in. If you don't want to have an external file then create the dictionary in the code; just hardcode it as it's very unlikely to change....

Code:

codes = {}

file = File.open("codes.txt", "r")
file.each do |line|
        values = line.split("|")
        codes[values[1].chomp()] = values[0]
end
file.close()

received = ".- -.-. -...".split()

message = ""
received.each {|letter| message += codes[letter] }
puts message


gregorian 03-31-2010 12:34 PM

Quote:

Originally Posted by Sergei Steshenko (Post 3919030)
What did you do to find the error ? What did you verify ?

The expected result is 10 characters since it has 10 morse characters. But it produces several characters. I can verify the answer with an online Morse converter, and I have verified the expression with an online regex tester: http://www.regular-expressions.info/...ptexample.html

dsmyth, I thought of using a hash myself but I couldn't find an easy way of using it in matlab. I know that using a hash is probably the best method, but I need to do it this way. Besides, I want to know what went wrong in the code.

EDIT: I found the problem: Matlab sucks.
If you try to concatenate spaces to the beginning or end of a string, it will ignore it. Because of this every substring was getting matched. Solution? Use some other delimiter.
Final code:
Code:

stri=strrep(stri, ' ',',')
stri = strcat(',', stri, ',')

morse = {'\.-' '-\.\.\.' '-\.-\.' '-\.\.' '\.' '\.\.-\.' '--\.' '\.\.\.\.' '\.\.' '\.---' '-\.-' '\.-\.\.' '--' '-\.' '---' '\.--\.' '--\.-' '\.-\.' '\.\.\.' '-' '\.\.-' '\.\.\.-' '\.--' '-\.\.-' '-\.--' '--\.\.' '-----' '\.----' '\.\.---' '\.\.\.--' '\.\.\.\.-' '\.\.\.\.\.' '-\.\.\.\.' '--\.\.\.' '---\.\.' '----\.'};
code='ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';

  for i=1:size(morse,2)
  str = strcat(',',morse(i),',');
  ch = strcat(',', code(i), ',');
  i
  stri = regexprep(stri,str,ch)
  end
stri=strrep(stri, ',','')

Thanks for trying to help me!


All times are GMT -5. The time now is 11:34 PM.