LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-31-2010, 06:49 AM   #1
gregorian
Member
 
Registered: Apr 2006
Posts: 509

Rep: Reputation: 34
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?

Last edited by gregorian; 03-31-2010 at 12:44 PM.
 
Old 03-31-2010, 07:00 AM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by gregorian View Post
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 ?
 
Old 03-31-2010, 10:31 AM   #3
dsmyth
LQ Newbie
 
Registered: Mar 2010
Location: Glasgow, Scotland
Distribution: Fedora 12
Posts: 26
Blog Entries: 6

Rep: Reputation: 17
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

Last edited by dsmyth; 03-31-2010 at 10:59 AM.
 
1 members found this post helpful.
Old 03-31-2010, 12:34 PM   #4
gregorian
Member
 
Registered: Apr 2006
Posts: 509

Original Poster
Rep: Reputation: 34
Quote:
Originally Posted by Sergei Steshenko View Post
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!

Last edited by gregorian; 03-31-2010 at 01:44 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
pls convert this windows code into linux code nagendrar Programming 6 06-03-2008 07:00 AM
small syntax problem with C code (implemented in Code Composer Studio) illiniguy3043 Programming 6 01-07-2008 02:14 AM
How to convert Assembly code to "C" source code ssg14j Programming 2 08-01-2005 12:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration