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 ...
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 <...
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}\$...
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
...
7
votes
Hidden Markov Model with Viterbi
1. Bugs
No use is made of the last observation. For example, if we have this model:
...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
2
votes
Accepted
2
votes
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 ...
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 ...
2
votes
Viterbi algorithm implementation in C
Too large allocations
I noticed that you allocated several arrays of length n here:
...
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:
<...
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 ...
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:
...
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:
...
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 ...
1
vote
Markov text generator program in Python
In the following, I assume that you use Python3 rather than Python2, though I can't say for sure that it makes a difference.
First, when you parse your file, you could use a context manager:
...
1
vote
Accepted
Generating text from a Markov chain in Java
Your concat method can be simplified with JDK 8:
...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
markov-chain × 37python × 22
python-3.x × 8
algorithm × 8
natural-language-processing × 6
performance × 5
beginner × 5
statistics × 5
random × 4
java × 3
c# × 2
c++ × 2
ruby × 2
haskell × 2
numpy × 2
bioinformatics × 2
physics × 2
c × 1
programming-challenge × 1
parsing × 1
regex × 1
hash-map × 1
graph × 1
mathematics × 1
r × 1