gpt4 book ai didi

java - 将多个字符翻译成一个

转载 作者:行者123 更新时间:2023-11-29 03:43:29 24 4
gpt4 key购买 nike

我需要帮助将摩尔斯电码转换为英语。我已经编写并测试了将英语翻译成莫尔斯码的代码。我的主要问题是将摩尔斯电码中的所有字符放在一起,然后再将其翻译成英文字符(如果有意义的话)。

例如:“。”转换为 E 并...转换为 S,但我不希望翻译在到达...之前开始

几条规则-空格用于分隔摩尔斯字母-|用作分隔单词的分隔符-不能使用 HashMap :(

这是我的代码

import java.util.Arrays;
import javax.swing.JOptionPane;

public class test3
{
public static void main ( String [] args )
{

String s1 = "Morse";

// Decide whether Morse code or English
String decide = JOptionPane.showInputDialog("Enter 'English' for Morse to English code translation and 'Morse' for English to Morse code translation. Pay attention to Caps.");

// Enter String & decide whether to convert to Morse or English
String phrase = JOptionPane.showInputDialog("Enter the words you wish to translate.");

if ( decide.equals( s1 ))
toMorse( phrase );

else
toEnglish( phrase );
}

// Translate to Morse
public static void toMorse( String preTranslation )
{

String[] english = {"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","1","2","3","4","5","6","7","8","9","0"};

String[] morse = {".-","-...","-.-.","-..",".","..-.","--.","....","..", ".---",
"-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-",
"...-",".--","-..-","-.--","--..",".----","..---","...--","....-",".....","-....","--...","---..","----.","-----"};
// Remove uppercase
String preTrans = preTranslation.toLowerCase();

// Deletes spaces
String phraseWithDelimiter = preTrans.replace( " ", "|");

String[] translation = new String[phraseWithDelimiter.length()];
String[] delimiter = {"|"};

// Translate
for ( int arrayVar = 0, transArrayVar = 0, subStringVarB = 0, subStringVarE = 1; transArrayVar < phraseWithDelimiter.length();)
{
if( phraseWithDelimiter.substring(subStringVarB, subStringVarE).equals( delimiter[0] ) )
{
translation[transArrayVar] = delimiter[0];
transArrayVar++;
subStringVarB++;
subStringVarE++;
}
else if ( phraseWithDelimiter.substring(subStringVarB, subStringVarE).equals( english[arrayVar] ) )
{
translation[transArrayVar] = morse[arrayVar];
transArrayVar++;
subStringVarB++;
subStringVarE++;
arrayVar = 0;
}
else
{
arrayVar++;
if ( arrayVar == 35 )
{
arrayVar = 0;
subStringVarB++;
subStringVarE++;
transArrayVar++;
}
}
}
String morseSeparator = new String( " " );
arrayToString ( translation, morseSeparator );

}


//Convert array to string and print translation
public static void arrayToString(String[] trans, String separator)
{
String result = "";
if (trans.length > 0)
{
result = trans[0]; // start with the first element
for (int i = 1; i < trans.length; i++)
result = result + separator + trans[i];
}
System.out.println( result );
}

// unfinished
public static void toEnglish( String preTranslation)
{
}

我正在考虑制作一个 for 语句并将摩尔斯电码的每个字符分配给一个新字符串,直到我到达一个空白区域,然后使用它进行测试,但我不确定该怎么做。

感谢任何帮助,谢谢!

最佳答案

按从长到短的顺序创建一个包含所有摩尔斯电码序列的正则表达式。

 Pattern.compile("----\\.|-----|---\\.\\.|...")

然后将其与字符串重复匹配以获得最长的有效摩尔斯电码序列。


或者,您可以使用像JavaCC 这样的解析器编译器。 .

SKIP : { " " }
TOKEN : { < A : "-." > }
TOKEN : { < B : "-..." > }
TOKEN : { < C : "-.-." > }
...

String translate() :
{ StringBuilder sb; }
{
{ sb = new StringBuilder(); }

(
letter(sb)
)*
<EOF>

{ return sb.toString(); }
}

void letter(StringBuilder sb) :
{}
{
( <A>
{ sb.append('a'); }
| <B>
{ sb.append('b'); }
| <C>
{ sb.append('c'); }
...
)
}

关于java - 将多个字符翻译成一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12096264/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com