# Tag Info

## Hot answers tagged markov-chain

Accepted

### Markov country name generator

It is better to follow PEP8 which says that import statements, such as in your case, should use multiple lines: import re import random Whatever the programming ...
• 3,412

### Markov country name generator

Looping over lines of a file Probably a minor nitpick, but when using a file object returned by open(), you can just iterate over the object, instead of calling <...
• 941

### Markov-chain sentence generator in Python

chooseNextWord distorts the probabilities. For example, consider a list of 3 words with the inherent probabilities $\frac{1}{3}$, $\frac{1}{3}$, $\frac{1}{3}$...
• 56.5k
Accepted

### Solve the phase state between two haplotype blocks using markov transition probabilities

Before we get to the interesting stuff, we should handle some stylistic niggles. Note that PEP 8 is a de-facto style for Python code. First, imports should be sorted ...
• 9,613

### Hidden Markov Model with Viterbi

1. Bugs No use is made of the last observation. For example, if we have this model: ...
• 49.6k
Accepted

### Viterbi algorithm implementation in C

Well, you are right that you rediscovered the megamoth, and it should be divided into easier to understand and maybe reusable parts. But let's first look at your 2 functions, which are both ...
• 19.3k

### Markov-chain sentence generator in Python

endTerms When a word ends with an endTerm, think you need to include an START or END symbol in adjList. Most words can appear anywhere in a sentence. So it is unlikely that you can end a sentence ...
• 10k

### Markov-chain sentence generator in Python

For long blocks of informational text at the beginning of a class or method definition, you should use docstrings instead of comments, as per PEP 8. This makes your descriptions automatically ...

### Markov-chain sentence generator in Python

Let's look at this one line, which shows quite a few areas where your code could be improved: if (word in self.adjList[prev]): As noted elsewhere, Python has an ...
• 41k

### Markov-chain sentence generator in Python

Style The long comment at the beginning of the class is good class documentation and should be made into a docstring. The same applies for functions. Avoid variables in all caps, like ...
• 1,085
Accepted

### Vanilla Markov chain in 18 lines

At a high level, I think you're overusing regexes. You can split stuff into words earlier and then clean them, and you don't need to treat newlines as an explicit token. If you do want to split them ...
• 439
Accepted

### Markov Chain in Python

Code is easier to understand, test, and reuse, if you divide it into functions with well-documented inputs and outputs, for example you might choose functions ...
• 49.6k

### Sentence generation using Markov Chains

generate_sentence calls _iterate_through_word_list that seems to perform all sorts of initialization, including reading the ...
• 10.3k

### Viterbi algorithm implementation in C

Welcome to Code Review, very nice first question for Code Review! You're quite correct about the program structure and you may want to do some research on software design principles such as the ...
• 24k

### Song lyric generator using Markov Chains - Python

Some suggestions: def unique(s): u = [] for x in s: if x not in u: u.append(x) else: pass return u may become ...
• 1,876
Accepted

### N state probability in an M/M/2 queue

Surely this is a trick question? The formulas only apply once the system has been running long enough that its behaviour approximates the stationary distribution. But the question explicitly says that ...
• 49.6k
Accepted

### Calculating the energy of the harmonic oscillator using a Monte Carlo method

You will probably get feedback more specific to the physics problem you are solving at physics.stackexchange.com [Thanks to Reinderien in the comments for clarification.] As for the code itself, there ...
• 2,249
Accepted

### Basic Markov Chain Algorithm

Use std::random_device instead of time() Stay within C++, and use a real random device to seed the generator, like so: ...
• 58.3k
Accepted

### Simulation of an alien population

There are some obvious possible simplifications for elegance, if not necessarily for speed. The while condition should be written as a double-ended inequality: <...
• 144k

### Mark V. Shaney: a script to produce gibberish

The name pairwise is fine when n=2 but for the general case I would prefer a name like ...
• 49.6k

### Markov chain-based random text generator

The only thing that comes to mind is the way you open the file. You could do this instead: ...
• 4,458
Accepted

### Generic Template Markov Chain

On your general coding style: First the little things: ...
• 306

### Song lyric generator using Markov Chains - Python

In this part of your code ...
• 1,876
Accepted

### Picking a random state from a set based on probabilities

There could potentially be millions of states, since each distinct word in the text I scan will be become a state. And they'll never change once calculated. I'm going to scan over a book, calculate ...
• 2,738

### Viterbi algorithm implementation in C

OP's code is largely well reviewed. Just adding a bit The "%*[\r\n]" in fscanf(statef, "%c %*[\r\n]", &ch); serves no ...

### Viterbi algorithm implementation in C

Too large allocations I noticed that you allocated several arrays of length n here: ...
• 28.5k

### Markov text generator program in Python

Here's a kinda stream-of-consciousness review: reading the file input() takes an argument, typically a prompt or question, so the user knows enter something. You ...
• 10k
1 vote
Accepted

### Generating text from a Markov chain in Java

Your concat method can be simplified with JDK 8: ...
• 560
1 vote

### Vanilla Markov chain in 18 lines

Use functions As it is, your code performs 3 tasks: getting a list of words out of a text, building the chain out of this words, and testing the chain by building a sentence; but it is rather ...
1 vote

### Entity resolution with NLTK

Style There is an official python style guide, PEP8. One of the things it recommends is using lower_case for variable and function names. Another recommendation is ...
• 41k

Only top scored, non community-wiki answers of a minimum length are eligible