有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用Markovmethod hashmap组装字符串列表

我得到了一个hashmap,其中包含两个字符串的字符串列表键和不同数量字符串的字符串列表值。我必须汇编并返回hashmap值的字符串列表,除非该值包含多个字符串,在这种情况下,元素应该是列表中的随机字符串

换句话说,<[a, b], b> | <[b, c], d> | <[c, d], [e, f, g]> | <[d, e], f>的映射(其中每个字母都是一个字符串)将返回元素的字符串列表[b, d, x, f],其中x是随机选择的e、f或g也可以假设映射中的第一个键是长度为2的字符串列表,其中两个元素都是“”,最终值是长度为1的字符串列表,其中包含“”(参见底部)。下面是代码

  static ArrayList<String> generateText(Map<List<String>, List<String>> whatComesNext, Random r) {
    List<List<String>> listKey = new ArrayList<List<String>>();
    ArrayList<String> ret = new ArrayList<String>();
    int x;
    ret.add("<START>");
    ret.add("<START>");
    for (List<String> key : whatComesNext.keySet())
        listKey.add(key);
    for (int i=0; i<whatComesNext.size()-1; i++)
      if (listKey.get(i).size()>1) {
        x=r.nextInt(listKey.get(i).size());
        ret.add(listKey.get(i).get(x)); }
      else ret.add(listKey.get(i).get(0));
    return ret;
  }

这里的问题是,它应该通过AssertEquals等独立程序的测试。问题是我得到了一个断言错误,说“与aa不匹配”。我不明白它期望的是什么,我也不明白我的程序为什么不返回我上面指定的内容。相关线路如下:

Pattern pattern = Pattern.compile("(ab)+c");
    int [] histogram = new int[10];
    for(int i = 0; i < 128; i++) {
      List<String> result = MarkovText.generateText(whatComesNext, new Random(197*i));
      result = MarkovText.excludeStartAndEndMarkers(result);
      assertEquals(1, result.size() % 2);
      int repeats = (result.size()-1)/2;
      if (repeats < histogram.length)
        histogram[repeats] = histogram[repeats]+1;
      StringBuffer together = new StringBuffer();
      for(String w : result) 
        together.append(w);
      assertTrue("Doesn't match " + together, pattern.matcher(together).matches());

而剩下的测试看起来是这样的

public void testSimpleGeneration() {
    List<String> text =
        Arrays.asList("<START> <START> a b a b c <END>".split(" "));
    Map<List<String>, List<String>> whatComesNext = new LinkedHashMap<>();
    MarkovText.learnFromText(whatComesNext, text);
    System.out.println(whatComesNext);
    assertEquals(5, whatComesNext.size());
    assertEquals(Arrays.asList("a", "c"), whatComesNext.get(Arrays.asList("a", "b")));
    Pattern pattern = Pattern.compile("(ab)+c");
    int [] histogram = new int[10];
    for(int i = 0; i < 128; i++) {
      List<String> result = MarkovText.generateText(whatComesNext, new Random(197*i));
      result = MarkovText.excludeStartAndEndMarkers(result);
      assertEquals(1, result.size() % 2);
      int repeats = (result.size()-1)/2;
      if (repeats < histogram.length)
        histogram[repeats] = histogram[repeats]+1;
      StringBuffer together = new StringBuffer();
      for(String w : result) 
        together.append(w);
      assertTrue("Doesn't match " + together, pattern.matcher(together).matches());
    }
    assertEquals(0, histogram[0]);
    assertTrue("expected distribution of results", 32 <= histogram[1] && histogram[1] <= 128 );
    assertTrue("expected distribution of results", 16 <= histogram[2] && histogram[2] <= 64 );
    assertTrue("expected distribution of results", 8 <= histogram[3] && histogram[3] <= 32 );
  }

编辑:该映射是在程序的早期创建的,来自一个单独的给定字符串列表。对于程序的这一部分,列表中有以下元素:[“[START]”、“[START]”、“a”、“b”、“a”、“b”、“c”和“[END]”,其中每个键是两个连续元素的一对,值是该对后面的元素


共 (0) 个答案