1

今日、Java を使用してテキスト ファイルからコンコーダンスを作成するクライアントと仕事をしています。私がする必要があるのは、コンコーダンスを反転して、本質的にテキストを最初から最後まで再作成することだけです。今、私が抱えているように見える問題は、どこから始めて、各ステップをどのように行うかです。今のところ、単語の配列を作成し、記号テーブルを反復処理して、各キーを配列に割り当てようとしました。次に、コンコーダンスから単語のリストだけを取得します。なんらかの理由で、この問題は単純な解決策であるように思われるため、私は非常にばかげているように感じます。物語を再現するための有効なアイデアが思い浮かびません。ここにソースを含めました:

public class InvertedConcordance {

public static ST<String, SET<Integer>> createConcordance (String[] words) {
    ST<String, SET<Integer>> st = new ST<String, SET<Integer>>();
    for (int i = 0; i < words.length; i++) {
        String s = words[i];
        if (!st.contains(s)) {
            st.put(s, new SET<Integer>());
        }
        SET<Integer> set = st.get(s);
        set.add(i);
    }
    return st;
}
public static String[] invertConcordance (ST<String, SET<Integer>> st) {

 //This is what I have so far
//Here is what I have that doesnt work
for(String key : st.keys())
{
inv[i++] = key;
}
for(int z = 0; z< inv.length; z++)
{
System.out.println(inv[z]);
}


 String[]inv = new String[st.size()];

    return inv;
}
private static void saveWords (String fileName, String[] words) {
    int MAX_LENGTH = 70;
    Out out = new Out (fileName);
    int length = 0;
    for (String word : words) {
        length += word.length ();
        if (length > MAX_LENGTH) {
            out.println ();
            length = word.length ();
        }
        out.print (word);
        out.print (" ");
        length++;
    }
    out.close ();
}
public static void main(String[] args) {
    String fileName = "data/tale.txt";
    In in = new In (fileName);
    String[] words = in.readAll().split("\\s+");

    ST<String, SET<Integer>> st = createConcordance (words);
    StdOut.println("Finished building concordance");

    // write to a file and read back in (to check that serialization works)
    //serialize ("data/concordance-tale.txt", st);
    //st = deserialize ("data/concordance-tale.txt");

    words = invertConcordance (st);
    saveWords ("data/reconstructed-tale.txt", words);
}

}

4

1 に答える 1