gpt4 book ai didi

java - 使用 Java 反转索引

转载 作者:太空宇宙 更新时间:2023-11-04 07:28:39 25 4
gpt4 key购买 nike

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

}

最佳答案

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

  • 设置
  • ST

而不是内置的java类:

  • 设置
  • map

这里需要哪些?

对于您的问题,您的代码根本不应该编译,因为您在使用变量 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;

关于java - 使用 Java 反转索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18128153/

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