使用Java反转一致性

时间:2013-08-08 14:05:18

标签: java set symbol-table

今天我正在与一个客户端合作,该客户端使用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);
}

}

1 个答案:

答案 0 :(得分:1)

首先 - 你为什么要使用一些奇怪的类,如:

  • SET
  • ST

而不是内置的java类:

  • 地图

哪些人在这里结婚?

至于你的问题,你的代码根本不应该编译,因为你在使用它后声明变量inv

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;
}

如果我理解你的想法,那么索引就会创建包含索引的单词和集合列表。如果这是正确的解释,那么逆操作将是:

public static String[] invertConcordance (ST<String, SET<Integer>> st) {

//First - figure out the length of the document, which is simply the maximum index in the concordancer
int document_length = 0;
for(String key : st.keys()){
  for(Integer i : st.get(key)){
    if(i>document_length){
      document_length=i;
    }
  }
}    

//Create the document
String[] document = new String[document_length+1];

//Reconstruct
for(String key : st.keys()){
  for(Integer i : st.get(key)){
    document[i] = key;
  }
}

return document;
}

我假设,索引的编号从0到文档的长度为1,如果实际存储从1到文档的长度,则应修改行:

String[] document = new String[document_length+1];

String[] document = new String[document_length];

    document[i] = key;

    document[i-1] = key;