2

我正在制作一个单词定义测验应用程序,我的单词列表及其定义是 JSON 格式,结构如下:

"words": [
        {
            "word": "rescind",
            "type": "verb",
            "definition": "to take back, repeal as in a rule or policy"
        },
        {
            "word": "curtail",
            "type": "verb",
            "definition": "to reduce or lessen"
        },

等等等等

在测验中,你会得到一个随机挑选的单词和五个可供选择的定义。就像一个标准化的多项选择测试。

现在,我开始看到的一个问题是我的单词具有非常相似的含义。我目前从列表中随机选择了 4 个错误定义,但为避免混淆,我想避免选择与正确选择相似的定义。

我应该如何创建这个“相似性”地图?我想到的一种解决方案是:

        {
            "word": "avid",
            "type": "adjective",
            "definition": "extremely excited about, enthusiastic about",
            "similar to": [
                "ardent",
                "fervent"
            ]
        },

但我意识到这个解决方案有点糟糕,因为我必须找到彼此的单词并实现相同的列表,当我最终添加大量单词时,它会变得非常庞大。

那么你们认为最好的解决方案是什么?

4

1 回答 1

3

一个简单的第一种方法是创建一个Word带有字段的类。

确保覆盖equals()hashCode()使用“word”字段(我称它为“值”以将其与类名区分开来)(见下文):

public class Word {
    private final String value;
    private final String type;
    private final String definition;
    private final List<Word> synonymns = new ArrayList<Word>();

    public Word(String value, String type, String definition) {
        this.value = value;
        this.type = type;
        this.definition = definition;
    }

    // equals() and hashCode() based on the value field
    @Override
    public int hashCode() {
        return value.hashCode();
    }
    @Override
    public boolean equals(Object obj) {
         return obj instanceof Word && ((Word)obj).value.equals(value);
    }

    public String getValue() {
        return value;
    }
    public String getType() {
        return type;
    }
    public String getDefinition() {
        return definition;
    }
    public List<Word> getSynonymns() {
        return synonymns;
    }
}

实现equals()hashCode()基于 value 字段意味着您可以通过使用 Set 来防止重复:

Set<Word> words = new HashSet<Word>(); // wont allow two Word objects with the same value

您可以使用 from 的返回值(Set.add()如果集合尚未包含指定元素,则返回 true)来检查所添加的单词实际上是唯一的:

Word word = new Word("duplicate", "adjective", "another copy");
if (!words.add(word)) {
    // oops! set already contained that word
}

如果您想添加特殊的酱汁,请创建type一个枚举:

public enum PartOfSpeach {
    NOUN,
    VERB, // in USA "verb" includes all nouns, because any noun can be "verbed"
    ADJECTIVE,
    ADVERB
}

您可以考虑允许单词属于多种类型:

  • 树皮:动词:狗做什么
  • 树皮:名词:覆盖一棵树的东西

事实上,您可以考虑每个单词有多种含义:

public class Meaning {
    PartOfSpeach p;
    String definition;
    List<Word> synonyms; // synonyms belong to a particular *meaning* of a Word.
}
于 2013-07-19T02:56:49.083 回答