Exemplo n.º 1
0
 def __init__(self, teamName, game):
     self.teamName = teamName
     self.game = game
     self.playerList = ([])
     self.queue = ([])
     self.tree = Tree()
     
     self.setUpGameboard()
Exemplo n.º 2
0
class Team:
    """ Builds the default Team                                       """
    """ teamName: Holds a string for the Team's name                  """
    """ playerList: Holds the Player objects associated with the Team """
    """ queue: Holds the Teams sequence of numbers that must be added """
    """   and sorted into the tree                                    """
    """ tree: Holds the Team's Tree object                            """
    def __init__(self, teamName, game):
        self.teamName = teamName
        self.game = game
        self.playerList = ([])
        self.queue = ([])
        self.tree = Tree()
        
        self.setUpGameboard()
    
    """ Adds a Player to the team """
    def addPlayer(self, player):
        self.playerList.append(player)
    
    """ Removes all Players from the team """
    def clearPlayers(self):
        self.playerList = ([])
        
    def getPlayerByRole(self, inRole):
        for player in self.playerList :
            if player.getName() == inRole :
                return player
        
        return None
        
    """ Builds the Team's queue with random numbers from 0-25                           """
    """ The getRandomNumber function ensures these numbers have not been used earlier   """
    def setUpGameboard(self):
        for x in range(5) :
            x = self.getRandomNumber()
            self.queue.append(x)
            
    """ Generates a random Number that a Team hasn't used before """
    """   (that's not in the queue or the root of the tree)      """  
    def getRandomNumber(self):
        picked = False
        while not picked :
            picked = True
            x = random()*MAX_NUMBER
            x = int(x)
            
            if x == self.tree.root.value :
                picked = False
                continue
            for val in self.queue :
                if x == val :
                    picked = False
                    break
            if picked:
                return x
    
    """ Allows sorter to swap numbers in the 'queue' """
    def swapQueue(self, current, direction):
        temp = self.queue[current]
        self.queue[current] = self.queue[current + direction]
        self.queue[current + direction] = temp
        
    """ Gets the next number out of the list """
    def getNextNumber(self):
        # Check if the Game is over
        if len(self.queue) == 0:
            self.game.gameOver(self)
            return
        
        # Get the value
        value = self.queue[0]
        self.queue.remove(value)
        
        # Move Player's currents to make sure they still point at something
        for team in self.game.teams :
            if team != self :
                break
        
        player = team.getPlayerByRole("Sorter")
        player.moveCurrent()
        
        # Return the value
        return value
  
    """ Called when players want to affect the tree                      """
    """ This allows the Team to check if the tree is balanced or not     """
    """   after the alteration                                           """
    """ If the tree is balanced, it pops the first number from its queue """
    """   and sends it to the Team's Inserter                            """
    """ If the tree is not balanced, it sends None to the Inserter       """
    def affectTree(self, action, params):
        result = action(params)
        
        balanced = self.tree.checkBalanced(self.tree.root, 0)
        
        if balanced is not "Unbalanced":
            theNum = self.getNextNumber()
            self.game.itsBalanced(self)
        else:
            theNum = None
            self.getPlayerByRole("Inserter").center = self.tree.root
        
        for x in self.playerList :
            x.getNext(theNum)
        
        return result
    
    """ Prints the Team's queue """ 
    def printQueue(self):
        print "Team", self.teamName
        print "Queue:",
        for x in self.queue:
            print x,
        print ""
        
    """ Prints the Team's tree """
    def printTree(self):
        self.tree.printTree(self.tree.root)
Exemplo n.º 3
0
            line = line.strip()
            words = line.split(" ")
            if words[0] == '(S':
                if flag:
                    f2.write("\n" + "(S ")
                else:
                    f2.write("(S ")
                    flag = 1
            else:
                f2.write(' '.join(words) + " ")

with open('corpus_processed.txt', 'r') as f2:
    with open('PCFG_train_results.txt', 'w') as f3:
        for line in f2:
            line = line.strip()
            line = Tree.parse(line)
            rules = line.getProductions()
            for (lhs, rhs) in rules:
                counts[(lhs, rhs)] += 1
                base[lhs] += 1
        for (lhs, rhs), count in counts.items():
            prob = count / base[lhs]
            RandSentence_revised.add_prod(lhs, rhs)
            RandSentence.add_prod(lhs, rhs)
            str = "%s -> %s # %.4f" % (lhs, rhs, prob)
            f3.write(str + '\n')

with open('PCFG_generate_results.txt', 'w') as f4:
    for i in range(10):
        f4.write(RandSentence.gen_random('S') + '\n')
Exemplo n.º 4
0
import sys
from graphviz import Digraph
from NodeClass import Node
from TreeClass import Tree

avl = Digraph(comment="AVL tree",format="png")
binary = Digraph(comment="Binary tree",format="png")
myTree = Tree()
dummy = Tree()
print "1-Insert\t2-inorder\t3-height\t4-draw tree"
#insertionOrder = []
while(1):

    ch = int(raw_input("Enter choice:"))
    if(ch == 1):
        data = int(raw_input("Enter new node data"))
        #insertionOrder.append(data)
        myTree.root = myTree.insert(myTree.root,Node(data))
        dummy.root = dummy.normalInsert(dummy.root,Node(data))
    elif ch == 2:
        myTree.inorder(myTree.root)
    elif ch == 3:
        print myTree.height(myTree.root)
    elif ch == 4:
        if myTree.root == None:
            print "Tree is empty"
        else:
            z = 0
            avl.name = "AVL_Tree"
            avl.filename = "avlTree.gv"
            avl.node(str(myTree.root.data))