简体   繁体   English

用随机字母实现马尔可夫算法,直到字母组成字符串中的一个单词?

[英]Implement Markov's algorithm using random letters till letters makeup one word in the string?

I am trying to use the Markov chain to display random letters till the letters make up one of the words found in the string. 我试图使用马尔可夫链显示随机字母,直到字母组成字符串中找到的单词之一。 I think I'm on the right track, but instead of stopping when it reaches one word it just prints the whole string of random. 我认为我走在正确的轨道上,但是它不会在到达一个单词时停下来,而是会打印整个随机字符串。

I believe there will be a check to see if characters match with the print out, but not sure… 我相信将会检查字符是否与打印件匹配,但不确定...

What am I doing wrong here? 我在这里做错了什么?

public class Main {

    public static void main(String[] args) {

        final int NUMBER_OF_CHARS = 100000;
        String[] words = { "at", "is", "he", "we", "up", "on" };

        for (int i = 0; i < NUMBER_OF_CHARS; i++) {
            char ch = main.getRandomLowerCaseLetter();
            if ((i + 1) % 40 == 0)
                System.out.println(ch);
            else
                System.out.print(ch);
        }
    }

    public static char getRandomCharacter(char ch1, char ch2) {
        return (char)(ch1 + Math.random() * (ch2 - ch1 + 1));
    }

    public static char getRandomLowerCaseLetter() {
        return getRandomCharacter('a', 'z');
    }
}

I don't see any if statements checking whether the characters have formed a word yet. 我没有看到任何if语句来检查字符是否已经组成单词。 In other words, the loop will never exit, because you haven't written logic for it to do so. 换句话说,循环永远不会退出,因为您还没有为此编写逻辑。

You probably want to store each character you generate in a String as you go, like this: 您可能想随即将生成的每个字符存储在String中,如下所示:

String chars = "";
...
for(...){
    ...
    chars.append(ch);
    ...
}

You should also write something like this to break out of the loop: 您还应该编写如下代码来打破循环:

for(String word : words){
    if(chars.contains(word){
        break outerLoop;
    }
}

Label the outer for loop like this so the break statement breaks out of both loops, not just the inner for loop: 像这样标记外部的for循环,以便break语句可以脱离两个循环,而不仅仅是内部的for循环:

outerLoop: while(...){}

There are probably better methods out there for doing this but here is something quick that solves your problem, as suggested by Keara you need to store each character as you go, then loop through and check if you have found something that you are looking for. 可能有更好的方法可以执行此操作,但是可以通过一些快速的方法解决您的问题,正如Keara所建议的那样,您需要随身存储每个字符,然后遍历并检查是否找到了所要查找的东西。 As (in the example you gave anyway) the words you are looking for are a maximum of 2 characters long it makes it simple to reset the string every time you have found an invalid combination. 因为(无论如何,在您给出的示例中)您要查找的单词的最大长度为2个字符,因此每次找到无效组合时都可以轻松重置字符串。

import java.util.ArrayList;
import java.util.List;

public class Test {

public static void main(String[] args) {

    final int NUMBER_OF_CHARS = 100000;
    String[] words = {"at", "is", "he", "we", "up", "on"};
    List<Character> characters = new ArrayList<>();
    for (int i = 0; i < NUMBER_OF_CHARS; i++) {
        char ch = getRandomLowerCaseLetter();
        characters.add(ch);

        if ((i + 1) % 40 == 0) {
            System.out.println(ch);
            if (contains(characters, words)) {
                System.exit(0);
            }
        } else {
            System.out.print(ch);
            if (contains(characters, words)) {
                System.exit(0);
            }
        }
    }
}

public static boolean contains(List<Character> characters, String[] words) {
    String st = "";
    int count = 0;
    for (Character c : characters) {
        st += c;
        count++;
        if (count == 2) {
            for (String s : words) {
                if (st.equals(s)) {
                    System.out.println(" Found: " + s);
                    return true;
                }
            }
            count = 0;
            st = "";
        }
    }
    return false;
}

public static char getRandomCharacter(char ch1, char ch2) {
    return (char) (ch1 + Math.random() * (ch2 - ch1 + 1));
}

public static char getRandomLowerCaseLetter() {
    return getRandomCharacter('a', 'z');
}
}

Kind Regards and happy programming! 亲切的问候和快乐的编程!

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

相关问题 如何检查由7个随机字母组成的字符串是否可以形成文件中存在的单词 - How to check if a string of 7 random letters can form a word that exists in a file 您如何使用一组随机给定的字母来检查是否仅使用给定字母创建一个单词 - How do you, using a set of random given letters, check if a word is created with only those given letters 如何在Java中实现马尔可夫算法? - How to implement Markov's algorithm in Java? 用另一组字母检查单词中的字母的算法和数据结构 - Algorithm and Data Structure for Checking letters in a word with another set of letters 为单词搜索游戏选择随机字母的算法,允许拼写多个单词 - Algorithm to choose random letters for word search game that allows many words to be spelled 将数组的随机字母另存为字符串 - saving random letters of an array as a String 计算字符串的小写字母 - Counting a String's Lowercase Letters 在随机字母字符串中插入字符,但保持成为单词的可能性,没有庞大的数据结构是否可能? - Insert character inside a string of random letters but maintain possibility to become word, is it possible without immense data-structure? 用字符串中的随机字符替换字母和数字 - Substitute letters and numbers with random characters in a string 查看单词的字母是否按升序排列 - View if a word's letters are in ascending order
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM