私は、2つの文字列が与えられると、文字を「ジャグリング」して同義語を形成するプログラムを作成しようとしています。その例を示すWebサイトは次のとおりです。
http://www.braingle.com/brainteasers/46611/letter-juggle.html
私の仕事は、「同義語のペアを含むファイルと、辞書からの単語のシーケンスを含む2番目のファイルを指定して、パズルを設定するために使用できる辞書からできるだけ多くの単語のペアを生成するプログラムを作成することです。同義語のペアごとに。」
これらはファイルです-dictionary.txtとsynonyms.txt。
単語をジャグリングするときは、辞書をチェックして、それが有効かどうかを確認します。つまり、「自慢」と「ヒップ」という言葉を使って、それらをジャグリングすると、「ボート」と「船」(同義語)を取得できます。
今、私は2つの文字列(ネイルとピン)を取り、それらをchar配列に分割しましたが、それらが有効な単語であるかどうかを確認するためにそれらをジャグリングする方法がわかりません。
「nail」から「pin」に文字「n」を追加して「pinn」を取得できるようにしたいので、「pinn」のすべての組み合わせを調べて、それが有効な単語であるかどうかを確認します。 、次に、「ail」が単語になり得るかどうかを確認します。そうでない場合は、「nail」の次の文字に移動します。pinn-> pinn、pnin、pnni、pnin ..... ..
public class LetterJuggle {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("Dictionary.txt"); //Dictionary.txt //Synonyms.txt
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
int size =0;
while ((strLine = br.readLine()) != null){
size++;
}
String [] dictionary = new String [size];
fstream = new FileInputStream("Dictionary.txt");
in = new DataInputStream(fstream);
br = new BufferedReader(new InputStreamReader(in));
size = 0;
//Read File Line By Line
while ((strLine = br.readLine()) != null){
// Print the content on the console
dictionary[size] = strLine;
size++;
}
fstream = new FileInputStream("Synonyms.txt");
in = new DataInputStream(fstream);
br = new BufferedReader(new InputStreamReader(in));
while ((strLine = br.readLine()) != null){
//System.out.println(strLine);
String [] words = strLine.split("\\s+");
for(int i =0; i < words.length; i++){
//System.out.println(words[i]);
}
char[] ch_array_1 = words[0].toCharArray();
char[] ch_array_2 = words[1].toCharArray();
for(int i =0; i < ch_array_1.length; i++){
System.out.print(ch_array_1[i] + " ");
}
System.out.println();
for(int i =0; i < ch_array_2.length; i++){
System.out.print(ch_array_2[i] + " ");
}
System.out.println();
}
//Close the input stream
in.close();
}catch(Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}