[SOLVED] Small matlab code to convert Morse code to plaintext using regular expressions
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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 ?
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....
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:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.