Skip to main content
Share Your Experience: Take the 2024 Developer Survey
20 votes
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 ...
Billal Begueradj's user avatar
15 votes

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}\$...
vnp's user avatar
  • 56.5k
15 votes

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 <...
muru's user avatar
  • 941
11 votes
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 ...
Veedrac's user avatar
  • 9,613
7 votes

Hidden Markov Model with Viterbi

1. Bugs No use is made of the last observation. For example, if we have this model: ...
Gareth Rees's user avatar
  • 49.6k
6 votes
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 ...
Deduplicator's user avatar
  • 19.3k
6 votes

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 ...
RootTwo's user avatar
  • 10k
4 votes
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 ...
Greg Nisbet's user avatar
4 votes
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 ...
Gareth Rees's user avatar
  • 49.6k
4 votes

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 ...
Delya Erricson's user avatar
4 votes

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 ...
Graipher's user avatar
  • 41k
4 votes

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 ...
qwr's user avatar
  • 1,055
3 votes

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 ...
pacmaninbw's user avatar
  • 23.8k
3 votes
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 ...
Gareth Rees's user avatar
  • 49.6k
3 votes

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 ...
MarianD's user avatar
  • 1,876
3 votes

Sentence generation using Markov Chains

generate_sentence calls _iterate_through_word_list that seems to perform all sorts of initialization, including reading the ...
Janne Karila's user avatar
  • 10.3k
3 votes
Accepted

Cumulative transition matrix Markov Chain simulation

I see nothing wrong with your implementation, in the sense it does exactly what you described in plain English, and somewhat efficiently. Here are however a few pieces of advice, mostly about ...
flodel's user avatar
  • 3,525
3 votes
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 ...
QuasiStellar's user avatar
  • 2,239
2 votes
Accepted

Generic Template Markov Chain

On your general coding style: First the little things: ...
ab.o2c's user avatar
  • 306
2 votes

Song lyric generator using Markov Chains - Python

In this part of your code ...
MarianD's user avatar
  • 1,876
2 votes
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 ...
BenC's user avatar
  • 2,738
2 votes

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 ...
chux - Reinstate Monica's user avatar
2 votes

Viterbi algorithm implementation in C

Too large allocations I noticed that you allocated several arrays of length n here: ...
JS1's user avatar
  • 28.5k
2 votes
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: <...
200_success's user avatar
2 votes

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 ...
Gareth Rees's user avatar
  • 49.6k
2 votes

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: ...
yuri's user avatar
  • 4,458
2 votes
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: ...
G. Sliepen's user avatar
2 votes

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 ...
RootTwo's user avatar
  • 10k
1 vote
Accepted

Generating text from a Markov chain in Java

Your concat method can be simplified with JDK 8: ...
Marvin's user avatar
  • 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 ...
301_Moved_Permanently's user avatar

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