Osobní nástroje
Nacházíte se zde: Úvod Výuka TM LS 2009/2010 16.3.2010 - Informace, kódování, data Morse translator

Morse translator

MorseTranslator.java — Java source code, 3Kb

Obsah souboru

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * Translate Morse code into text and vice versa
 */
public class MorseTranslator {

	/**
	 * Translation definition
	 */
	class Translation {
		Character text;
		String code;

		Translation(Character text, String code) {
			this.text = text;
			this.code = code;
		}
	}

	/**
	 * Definition of dictionary
	 */
	Translation[] dictionary = new Translation[] { new Translation('A', ".-"),
			new Translation('B', "-..."), new Translation('C', "-.-."),
			new Translation('D', "-.."), new Translation('E', "."),
			new Translation('F', "..-."), new Translation('G', "--."),
			new Translation('H', "...."), new Translation('I', ".."),
			new Translation('J', ".---"), new Translation('K', "-.-"),
			new Translation('L', ".-.."), new Translation('M', "--"),
			new Translation('N', "-."), new Translation('O', "---"),
			new Translation('P', ".--."), new Translation('Q', "--.-"),
			new Translation('R', ".-."), new Translation('S', "..."),
			new Translation('T', "-"), new Translation('U', "..-"),
			new Translation('V', "...-"), new Translation('W', ".--"),
			new Translation('X', "-..-"), new Translation('Y', "-.--"),
			new Translation('Z', "--.."),

			new Translation('1', ".----"), new Translation('2', "..---"),
			new Translation('3', "...--"), new Translation('4', "....-"),
			new Translation('5', "....."), new Translation('6', "-...."),
			new Translation('7', "--..."), new Translation('8', "---.."),
			new Translation('9', "----."), new Translation('0', "-----"),

			new Translation(' ', "/"),

	};

	//lookup map for faster translation
	private Map<Character, String> translationLookup = new HashMap<Character, String>();

	/**
	 * @throws IllegalArgumentException
	 */
	public void translateToMorseCode(BufferedReader reader, PrintWriter writer) throws IllegalArgumentException, IOException {
		String line;
		
		while((line = reader.readLine()) != null) {
			// remove untranslatable characters
			line = line.toUpperCase().replaceAll("[:\\.\\t\r\n,'\"\\();\\[\\]-]"," ").replaceAll(" +", " ").trim();

			if(line.length() == 0) continue;
			
			for (int i = 0; i < line.length(); i++) {
				Character c = line.charAt(i);
				String s = lookupTranslation(c);

				writer.print(s);
				if(c != ' ')
					writer.print("/");
			}
			writer.print("/");
		}
		writer.append("/");
	}

	/**
	 * Lookup for translation
	 */
	private String lookupTranslation(Character c) throws IllegalArgumentException {
		String s = translationLookup.get(c);

		if (s == null) {
			for (Translation t : dictionary) {
				if (t.text.equals(c)) {
					s = t.code;
					break;
				}
			}
			if (s == null) {
				throw new IllegalArgumentException(String.valueOf(c));
			}
			translationLookup.put(c, s);
		}

		return s;
	}

	public static void main(String[] args) throws Exception {
		PrintWriter w = new PrintWriter(new File("c:\\java\\morse-code.txt"));
		BufferedReader r = new BufferedReader(new FileReader("c:\\java\\morse-text.txt"));
		try {
			new MorseTranslator().translateToMorseCode(r,w);
		} finally {
			r.close();
			w.close();
		}
	}

}
Akce dokumentů
« květen 2025 »
květen
PoÚtStČtSoNe
1234
567891011
12131415161718
19202122232425
262728293031