简体   繁体   English

如何从Java中的.FIC文件提取数据?

[英]How do you pull data from a .FIC file in java?

So I am writing a scrabble word suggestion program that I decided to do because I wanted to learn sets (don't worry, I at least got that part) and referencing info/data not created within the program. 因此,我正在写一个我决定做的拼字游戏建议程序,因为我想学习集合(不用担心,至少我有一部分),并引用程序中未创建的信息/数据。 Im pretty new to Java (and programming in general), but I was wondering how to pull words from a word list .FIC file in order to check them against words generated from the letters inputted. 我对Java(以及一般而言的编程)来说还很陌生,但是我想知道如何从单词列表.FIC文件中提取单词,以便根据输入的字母生成的单词检查它们。

To clarify, I have written a program which takes a series of letters and returns a set of every possible word created from those letters. 为了澄清,我编写了一个程序,该程序接受一系列字母,并返回从这些字母创建的每个可能单词的集合。 for example: input: 例如:输入:

abc

would give a set containing the "words": 将给出一个包含“单词”的集合:

a, ab, ac, abc, acb, b, ba, bc, bac, bca, c, ca, cb, cab, cba

What I am asking, really, is how to check those to find the ones contained in the .FIC file. 我真正要问的是如何检查这些文件以查找.FIC文件中包含的文件。

The file is the "official crosswords" file from the Moby project word list and I am still (very) shaky on parsing and other file dealing-with methods. 该文件是Moby项目单词列表中的“官方填字游戏”文件,我在分析和其他文件处理方法上仍然(非常)动摇。 I am continuing to research so I dont have any prototype code for that. 我正在继续研究,所以我没有任何原型代码。

Sorry if the question isn't entirely clear. 很抱歉,这个问题还不清楚。

edit: here is the method that makes the "words" to make it easier to understand the idea. 编辑:这是使“单词”更容易理解的方法。 The part I don't understand is specifically how to pull a word(as a string) from the .FIC file. 我不明白的部分是如何从.FIC文件中提取单词(作为字符串)。

private static Set<String> Words(String s)
{
    Set<String> tempwords = new TreeSet<String>();
    if (s.length() == 1)
    {   // base case, last letter
        tempwords.add(s);
    //  System.out.println(s);                                            uncomment when debugging
    }
    else
    {
        //set up to add each letter in s
        for (int i = 0; i < s.length(); i++)
        {   //cut the i letter out of the string
            String remaining = s.substring(0, i) + s.substring(i+1);
            //recursion to add all combinations of letters onto the current letter/"word"
            for (String permutation : Words(remaining))
            {
            //  System.out.println(s.substring(i, i+1) + permutation);    uncomment when debugging

                //add the full length words
                tempwords.add(s.substring(i, i+1) + permutation);
            //  System.out.println(permutation);                          uncomment when debugging

                //add the not-full-length words
                tempwords.add(permutation);
            }
        }
    }
//  System.out.println(tempwords);                                        uncomment when debugging
    return tempwords;
}

I dont know if it is the best solution, but i figured it out (hobbs the line thing helped a lot, thank you). 我不知道这是否是最好的解决方案,但是我想通了(霍布斯这个东西很有帮助,谢谢)。 I found that this works: 我发现这可行:

public static void main(String[] args) throws FileNotFoundException
{
    Scanner s = new Scanner(new FileReader("C:/Users/Sean/workspace/Imbored/bin/113809of.fic"));
    while(true)
    {
        words.clear();
        String letters = enterLetters();
        words.addAll(Words(letters));
        while(s.hasNextLine()) {
             String line = s.nextLine();
             String finalword = checkWords(line, words);
             if (finalword != null) finalwordset.add(finalword);
        }
        s.reset();
        System.out.println(finalwordset);
        System.out.println();
        System.out.println("_________________________________________________________________________");
    }
}

A few things: 一些东西:

  1. The checkWords method checks if the current word from the file is in the generated list of "words" checkWords方法检查文件中的当前单词是否在生成的“单词”列表中
  2. The enterletters method takes user inputted letters and returns them in a string enterletters方法采用用户输入的字母并将其返回为字符串
  3. The Words method returns a set of strings of all of the possible combinations of the characters in the given string, with each character used up to as many times as it appears in the string and no repeated "words" in the returned set. Words方法返回一组字符串,其中包含给定字符串中所有字符的所有可能组合,每个字符使用的次数与字符串中出现的次数一样多,并且在返回的集合中没有重复的“单词”。
  4. finalwordset and words are arraylists of strings defined as instance variables(i would put them in the main method but I'm lazy and it doesn't matter for this case) finalwordset和word是定义为实例变量的字符串的数组列表(我会将它们放在main方法中,但是我很懒,在这种情况下这无关紧要)
  5. I am very sure there is a better/more efficient way to do this, but this at least works. 我非常确定有更好/更有效的方法可以做到这一点,但这至少是可行的。

  6. Finally: I decided to answer rather than delete because I didn't see this answered anywhere else, so if it is feel free to delete the question or link to the other answer or whatever, at this point it is to help other people. 最后:我决定回答而不是删除,因为我在其他任何地方都没有看到这个答案,因此,如果可以删除问题或链接到其他答案或其他内容,这时可以帮助其他人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM