用bfs搜索

发布于 2024-11-29 05:49:21 字数 1647 浏览 2 评论 0原文

我已经从 wordnet 检索同义词并将其作为数组返回。这是我的代码的一部分,

<pre>
RiWordnet wordnet = new RiWordnet();
String word = lineTF.getText();
// Get synsets
String[] synsets = wordnet.getAllSynsets(word, "n");
String outputSynset = "Word: " + word;

    GUIsynonymTA.append("\n");
    GUIsynonymTA.append(outputSynset);
    GUIsynonymTA.append("\n");

    if (synsets != null) 
    {

    for (int i = 0; i < synsets.length; i++) 
    {
    GUIsynonymTA.append("\n");
    GUIsynonymTA.append("Synsets " + i + ": " + (synsets[i]));                        
    GUIsynonymTA.append("\n");

    //implement BFS here
<code>

直到这一行,我已成功检索同义词集。我要做的是在搜索 WordNet 同义词集时实现广度优先搜索。我正在调用 RiWordnet 库中的 getAllSynsets 方法,该方法将所有同义词存储在 wordnet 中。我尝试使用循环(if..else),但我不确定在哪里停止搜索。使用 BFS 期望知道搜索的范围,其中搜索同义词被标记为已访问的节点。这是我想在同义词搜索中使用 BFS 实现的一个概念。

例如:

student = {pupil, educatee, scholar, bookman} 

pupil = {student, educatee, schoolchild}

educatee = {student, pupil} --> has been search, so go to the next synonym.

schoolchild = {pupil} --> has been search, so go to the next synonym.

scholar = {bookman, student, learner, assimilator}

bookman = {scholar, student} --> has been search, so go to the next synonym.

learner = {scholar, assimilator, apprentice, prentice}

assimilator = {learner, scholar} --> has been search, so go to the next synonym.

apprentice = {learner} --> has been search, so go to the next synonym.

prentice = {apprentice, learner} --> has been search, so go to the next synonym.

ALL SYNONYM HAS BEEN SEARCH, SO STOP. 

也有人建议我应用HashSet而不是BFS。谁能帮助我吗?先感谢您..

i have retrieve synsets from wordnet an return it as an array. This is part of my code

<pre>
RiWordnet wordnet = new RiWordnet();
String word = lineTF.getText();
// Get synsets
String[] synsets = wordnet.getAllSynsets(word, "n");
String outputSynset = "Word: " + word;

    GUIsynonymTA.append("\n");
    GUIsynonymTA.append(outputSynset);
    GUIsynonymTA.append("\n");

    if (synsets != null) 
    {

    for (int i = 0; i < synsets.length; i++) 
    {
    GUIsynonymTA.append("\n");
    GUIsynonymTA.append("Synsets " + i + ": " + (synsets[i]));                        
    GUIsynonymTA.append("\n");

    //implement BFS here
<code>

until this line, i have retrieved the synsets successfully. What i'm going to do is to implement the Breadth First Search in searching WordNet synsets. I'm calling the method getAllSynsets from RiWordnet library which stores all synonym in wordnet. I try using looping (if..else), but i'm not sure where to stop my search. Using BFS is expected to know the scope of the search, where the search synonym be marked as the nodes that were visited. Here is a concept that i would like to implement using BFS in searching synonym.

For example:

student = {pupil, educatee, scholar, bookman} 

pupil = {student, educatee, schoolchild}

educatee = {student, pupil} --> has been search, so go to the next synonym.

schoolchild = {pupil} --> has been search, so go to the next synonym.

scholar = {bookman, student, learner, assimilator}

bookman = {scholar, student} --> has been search, so go to the next synonym.

learner = {scholar, assimilator, apprentice, prentice}

assimilator = {learner, scholar} --> has been search, so go to the next synonym.

apprentice = {learner} --> has been search, so go to the next synonym.

prentice = {apprentice, learner} --> has been search, so go to the next synonym.

ALL SYNONYM HAS BEEN SEARCH, SO STOP. 

Some people also suggested me to apply HashSet instead of BFS. Can anyone help me? Thank you in advance..

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

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

发布评论

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

评论(1

半边脸i 2024-12-06 05:49:21

听起来你想要这样的东西:

Queue<String> pending = ...
HashSet<String> processed = ...

pending.add("startWord"); 
processed.add("startWord");

while (!pending.isEmpty()) {
   String word = pending.remove();

   String[] possibilities = wordnet.getAllSynsets(word, "n");
   for (String p : possibilities) {
     // Make sure we haven't already processed the new word 
     if (!processed.contains(p)) {
        pending.add(p);
        processed.contains(p);
      }
   }
}

//  At this point, processed contains all synonyms found

它或多或少是同义词的递归扩展(这基本上就是 BFS)。

Sounds like you want something like this:

Queue<String> pending = ...
HashSet<String> processed = ...

pending.add("startWord"); 
processed.add("startWord");

while (!pending.isEmpty()) {
   String word = pending.remove();

   String[] possibilities = wordnet.getAllSynsets(word, "n");
   for (String p : possibilities) {
     // Make sure we haven't already processed the new word 
     if (!processed.contains(p)) {
        pending.add(p);
        processed.contains(p);
      }
   }
}

//  At this point, processed contains all synonyms found

It's more or less a recursive expansion of the synonyms (which is basically what BFS is).

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