繁体   English   中英

马尔可夫链的比较器和HashMap

[英]Comparators and HashMaps for Markov Chain

我正在尝试在Java / Processing中建立一个markov链,该链将读一本书,然后能够以概率方式将其分解。 编程是一种爱好...

我的想法是,方法是使用HashMap,并在其中存储Word对象。 我可以很容易地用String做到这一点,但是在每个唯一的Word中,都需要有另一个HashMap,该HashMap将为后面的Word存储更多的Word对象,依此类推,直到我们制作了具有足够复杂度的模型。

问题在于我似乎无法通过String name来检查Word对象是否已经在Map中。

通过四处查看,我可以发现我可能需要一个Comparator,但是当我认为我需要更像equals东西时,我所看到的所有示例都使用comparecompareTo 我根本不需要排序,排序将在程序的第二部分中进行。

下面的代码非常可怕-多年来,我一直在努力解决这个问题,但我找不到足够愚蠢的解释让我理解它。

在伪:
read book If the Word is not in the Map, put it in there If the Word is in the Map, iterate the key Check the Words that follow this Word, and check in the same way if they are within the first Word's Map, adding as necessary… repeat…完成后
Using the Integer values as probabilities, pick a word from that Word's Map, find a Word that is probable to follow it repeat until desired length is achieved

到目前为止的代码:

///markovs

import java.util.HashSet;
import java.util.Comparator;

HashMap<Word, Integer> book;

void setup()
{

  book = new HashMap<Word, Integer>();
  String[] rows = loadStrings("crash.txt");
  for (int i = 0; i < rows.length; i++)
  {
    if (trim(rows[i]).length() == 0)
    {
      continue;
    }

    String[] pieces = split(rows[i], " ");

    for (int j = 0; j<pieces.length; j++)
    {

      Word temp = new Word(pieces[j]);

      //c++;
      if (book.compare(temp)) {
        println("this worked for once");
        //iterate here
      } else {
        book.put(temp, 1);
        println("didn’t work");
        //book.add(temp);
        book.put(temp, 1);
      }
    }
  }
  println(book.size());
  //println(c);
  //println(book);
}

class WordComparator implements Comparator<Word> {
  @Override
  public int compare(Word w1, Word w2) {
    String w1name = w1.name;
    String w2name = w2.name;

    if (w1name.equals(w2name)) {
      return 1;
    } else {
      return 0;
    }
  }
}

class Word
{
  String name;
  int value=1; 
  int depth;

  HashMap<String, Integer> list;

  Word(String name_)
  {
    this.name = name_;
  }

  int compareTo(Word w) {
    if (w.name.equals(this.name)) {
      return 0;
    } else {
      return -1;
    }
  }

  Word(Word w)
  {
    this.depth = w.depth+1;
  }

  void nextWord(String word)
  {
  }
  void count() {
    value++;
  }
  void makeHash()
  {
    list = new HashMap<String, Integer>();
  }
}

要将对象用作HashMap中的键,您需要重写两个方法: equals()hashCode() 我不确定您要做什么,但是一个仅使用name变量的简单示例如下所示:

public boolean equals(Object other){
   if(other instanceof Word){
      return this.name.equals(((Word)other).name);
   }
   return false;
}

public int hashCode(){
   return name.hashCode();
}

但是,如果仍然使用name变量,则可能正在寻找multimap ,它只是一个Map,其中包含的Map包含...

HashMap<String, HashMap<String, Integer>> bookMap;

此外,虽然HashMap不使用compareTo函数,但是您实现它的方式似乎不对。 首先,您需要在您的课程上实现Comparable:

class Word implements Comparable<Word>{

其次, compareTo函数应返回以下三个值之一:负,零或正。 现在,您只返回零或负数,这没有任何意义。

我认为您最好退后一步来描述您实际要执行的操作,因为您的代码现在包含许多令人困惑的逻辑。

至于比较,您可以重写Object的继承的equals方法,例如:

  @ Override 
   boolean equals(Object o) {
    return o instanceof Word
      ? o.name.equals(name) : false;
  }

注意使用您自己的类型作为HashMap键,在本例中为Word 仅当您在Word上提供.hashCode().equals()的明智实现时, .hashCode() .equals()

在这里,您似乎可以只使用String来代替。 String已经具有必需的方法实现。 如果您确实想使用Word ,则可以使用String中的那些方法。 例如

class Word {
    String letters;

    public int hashCode() {
        return letters.hashCode();
    }

    public boolean equals(Object o) {
         if (o == null || o.getClass() != getClass()) return false;
         return letters.equals(((Word) o).letters);
    }
}

您不需要comparecompareTo ,只需这两个。

暂无
暂无

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

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