www.xbdev.net
xbdev - software development
Friday February 7, 2025
Home | Contact | Support | Programming.. More than just code .... | Data Mining and Machine Learning... It's all about data ..
     
 

Data Mining and Machine Learning...

It's all about data ..

 


Data Mining and Machine Learning > Learning Markov Chains using Python, Words and Examples


Generating text using Markov Chains. This example uses a text example ('textexample.txt') which is a single text file with lots and lots of example sentences; we use this to build the Markov Chain library; once the chain is built, we can create our out 'new' text based on the probablistic factor (e.g., what word usually follows ...).

import numpy as np

# Trump's speeches here: https://github.com/ryanmcdermott/trump-speeches
trump open('textexample.txt'encoding='utf8').read()

corpus trump.split()

def make_pairs(corpus):
    for 
i in range(len(corpus)-1):
        yield (
corpus[i], corpus[i+1])
        
pairs make_pairs(corpus)

word_dict = {}

for 
word_1word_2 in pairs:
    if 
word_1 in word_dict.keys():
        
word_dict[word_1].append(word_2)
    else:
        
word_dict[word_1] = [word_2]
 
first_word np.random.choice(corpus)

while 
first_word.islower():
    
first_word np.random.choice(corpus)

chain = [first_word]

n_words 50

for i in range(n_words):
    
chain.append(np.random.choice(word_dict[chain[-1]]))

' '.join(chain)



Resources


• Gutenberg Library Free Online Book Collection [LINK]
• Pixel Markov Chains [LINK]











 
Advert (Support Website)

 
 Visitor:
Copyright (c) 2002-2025 xbdev.net - All rights reserved.
Designated articles, tutorials and software are the property of their respective owners.