2

I have this code which is meant to generate text via Markov chains/processes. It compiles fine with no errors and runs on terminal with no errors but doesn't generate any response/return?

I do this by going into the directory were the Markov.py file is held, and running Python3 Markov.py on terminal, as shown below enter image description here

I know I'm doing something incorrect but not sure what it is, do I need to call the functions also?

import random

class Markov (object):
    def __init__(self,order):
        self.order = order
        self.group_size = self.order + 1 
        self.text = "testFile.txt"  #The training text
        self.graph = {}  #Holds the information learnt
        return

    def train (self,filename):
        self.text = file (testFile.txt).read () .split()    
        self.text = self.text + self.text [ : self.order]   
        for i in range (0, len (self.text) - self.group_size):
            key = tuple (self.text [i : i + self.order]) 
            value = self.text[i + self.order] 

            if key in self.graph:
                self.graph [key].append (value)
            else:
                self.graph [key] = [value]

    def generate (self, length):
        index = random.randint (0, len(self.text) - self.order)
        result = self.text[index : index + self.order]
        for i in range (length):
            state = tuple(result[len(result) - self.order:])
            next_word = random.choice(self.graph[state])
            result.append(next_word)

        return " ".join (result[self.order : ])

x = Markov(2)
files = open("testFile.txt", "r")
filename = files

x.train(filename)
print(x.generate(10))
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
MD9
  • 105
  • 2
  • 14
  • 2
    Yes, you need to call the functions. Right now there is no 'main' program. But this you would really learn in a crash course Python. It is probably good to invest some time... – Tom de Geus Jul 14 '18 at 12:14
  • 1
    of course it does not print anything because you have only defined some classes. You need to create an instance of the class in the end of the code – seralouk Jul 14 '18 at 12:15
  • 2
    Your code here has only an import, a class definition, and two function definitions. But no statement is executed because the functions are never called and the class is never instantiated. Add some lines that actually execute some commands and/or use the class. – Rory Daulton Jul 14 '18 at 12:15

2 Answers2

3

Assuming that your code snippet was copied over correctly, it looks like you forgot a level of indentation. The generate() and train() methods are part of the Markov() object, so they need an extra level of indentation.

Try this:

import random

class Markov (object):
    def __init__(self,order):
        self.order = order
        self.group_size = self.order + 1 
        self.text = "testFile.txt"  #The training text
        self.graph = {}  #Holds the information learnt
        return

    def train (self,filename):
        self.text = file (testFile.txt).read () .split()    
        self.text = self.text + self.text [ : self.order]   
        for i in range (0, len (self.text) - self.group_size):
            key = tuple (self.text [i : i + self.order]) 
            value = self.text[i + self.order] 

            if key in self.graph:
                self.graph [key].append (value)
            else:
                self.graph [key] = [value]

    def generate (self, length):
        index = random.randint (0, len(self.text) - self.order)
        result = self.text[index : index + self.order]
        for i in range (length):
            state = tuple(result[len(result) - self.order:])
            next_word = random.choice(self.graph[state])
            result.append(next_word)

        return " ".join (result[self.order : ])
Ryan
  • 169
  • 2
  • 6
  • @usr2564301 OP clarified in another comment that this code snippet is followed by some declarations. – Ryan Jul 14 '18 at 17:40
  • Okay, apologies for jumping to that conclusion then. It's OP's fault (of course!) for not including all information in the question itself... On that same topic: surely the indentation must be correct in OP's *original* source! Because else Python surely would have to say *something* about that bad indentation. – Jongware Jul 14 '18 at 17:44
1

Yes, you must call the functions within your code or they will not be called and will not run. You just call a class you must instantiate the class, try and instantiate your class and call the functions appropriately. More information on functions can be found here.

Silk
  • 33
  • 7
  • Ok, what is the input it generates. – Silk Jul 14 '18 at 12:56
  • However, I'm getting invalid syntax for print(x.generate(10)) ^ – MD9 Jul 14 '18 at 12:58
  • In the code I see two parameters for the generate() function. However you only entered one. – Silk Jul 14 '18 at 13:01
  • Surely since the first parameter is 'self', I wouldn't need to re-enter it again. – MD9 Jul 15 '18 at 13:01
  • Fixed it! `x = Markov(2) files = open("testFile.txt", "r") filename = files x.train(filename) print(x.generate(10))`. The error msg was in the wrong place, but the problem was occurring due to 'file' already being used under the Train function, re-naming the declaration to 'files' sort that out, thanks for the help! – MD9 Jul 15 '18 at 13:01