查找“连接的组件”; 在图中

发布于 2024-07-22 07:06:20 字数 1304 浏览 8 评论 0 原文

我正在使用 HashMap > 构建同义词库来保存单词及其同义词(需要此数据结构)。

出于分配的目的,同义关系被认为是传递的。 (我们可以将同义词库想象成一张图表)。 我想要完成的是将这个图形打印在文本文件中,每行都有一个连接的组件。 换句话说,所有可以作为同义词汇集在一起​​的单词都应该放在一行上。

public void save() {
    try {
        FileWriter fw = new FileWriter(defaultDefinitionFile);
        BufferedWriter out = new BufferedWriter(fw);
        Set<String> keys = thesaurus.keySet();
        Iterator<String> ite = keys.iterator();
        while (ite.hasNext()) {
            String key = ite.next();
            out.write(key);
            ArrayList<String> synonyms = thesaurus.get(key);
            Iterator<String> i = synonyms.iterator();
            while (i.hasNext()) {
                String syn = i.next();
                out.write(","+syn);
                keys.remove(syn);
            }
            out.write("\r\n");
        }
        out.close();
        fw.close();
    }
    catch (Exception e) {
        System.out.println("Error writing to file");
        e.printStackTrace();
    }
}

这就是我想象的情况:

打印一个单词及其每个同义词,然后从数据结构中删除这些同义词,这样我们就不会出现重复的行。

问题当然是,当我迭代哈希图的内容时,我无法删除任何内容。

我缺少任何替代方法吗?

PS:我自始至终都使用“图表”比喻,只是因为我需要标题雄辩且简洁。 我知道这个比喻的用处是有限的。

I'm building a thesaurus using a HashMap <String,ArrayList<String>> to hold words and their synonyms (this data structure is required).

For the purpose of the assignment, the synonymity relation is considered transitive. (We can imagine the thesaurus as a graph).
What I'm trying to accomplish is to print this graph in a text file, with a connected component on each line. In other words, all the words that can be pooled together as synonyms should go on a single line.

public void save() {
    try {
        FileWriter fw = new FileWriter(defaultDefinitionFile);
        BufferedWriter out = new BufferedWriter(fw);
        Set<String> keys = thesaurus.keySet();
        Iterator<String> ite = keys.iterator();
        while (ite.hasNext()) {
            String key = ite.next();
            out.write(key);
            ArrayList<String> synonyms = thesaurus.get(key);
            Iterator<String> i = synonyms.iterator();
            while (i.hasNext()) {
                String syn = i.next();
                out.write(","+syn);
                keys.remove(syn);
            }
            out.write("\r\n");
        }
        out.close();
        fw.close();
    }
    catch (Exception e) {
        System.out.println("Error writing to file");
        e.printStackTrace();
    }
}

This is how I pictured it to happen:

Print a word along with each of its synonyms, then remove those synonyms from the data structure so we don't have duplicate lines.

Problem is of course that I can't delete anything while i'm iterating over the content of the hashmap.

Any alternative approaches I'm missing?

P.S. I'm keeping the 'graph' metaphor throughout only because i needed the title to be eloquent and succint. I understand that this metaphor is limited in usefulness.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

似最初 2024-07-29 07:06:20

您可以将打印的单词存储在 Set< /a>,然后仅处理尚未包含在集合中的单词。

旁注:虽然人们确实可以将其视为图形问题,但您的代码不会将其视为这样。 如果我们将其视为图问题,那么我们就不会假设每个单词的所有同义词都列在相应的 ArrayList 中,从而需要计算对称和传递闭包。 只有这样我们才能提取等价类。

(实际上,我知道同义词关系不是传递性的。)

You can store the words that were printed in a Set, and then only handle words that are not yet in the set.

Side remark: though it's true that one can think about this as a graph problem, your code doesn't treat this as such. If we were to treat this as a graph problem, then we wouldn't make the assumption that each word has all its synonyms listed in the corresponding ArrayList, thus calling for the calculation of the symmetric and transitive closure. Only then would we extract the equivalence classes.

(In reality the synonym relation is not transitive, I know.)

海夕 2024-07-29 07:06:20

我不认为这个(你的总体想法)会起作用,因为“同义性”不是传递属性。

有很多单词的同义词本身并不是同义词。

I don't this this (your general idea) will work as "synonimity" is not a transitive property.

There are plenty of words that have synonyms that are not themselves synonyms.

一抹淡然 2024-07-29 07:06:20

不要删除该项目,而是将其添加到要忽略的项目列表中。

Instead of removing the item, add it to a list of items to ignore.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文