Exemplo n.º 1
0
 def __init__(self, data, count):
     self.bigram_measures = nltk.collocations.BigramAssocMeasures()
     with open(data, 'r') as dataDelim:
         self.lst = [line.rstrip().split('-') for line in dataDelim]
     with open(count, 'r') as noLines:
         self.totallst = noLines.read().split('-')
     self.oChain = pykov.Matrix()
Exemplo n.º 2
0
    def __init__(self,
                 messages: List[Tuple[Any, str]],
                 finite=False,
                 max_walk_length: int = 100,
                 enhance: bool = True):
        """Create a Chat object based on historical messages.

        Args:
            messages: List of tuples of sender and message. Sender may be of any type and may not be None. Messages should be in chronological order; otherwise results may be worse.
            max_walk_length: Limit on number of words in a generated message
            enhance: If set to true, repeated message generation for the same user may be faster at the cost of space. When dealing with a large amount of users on a system without much space this should be set to False

        Examples:
            >>> messages = [('Alice', 'Hey there!'),
                        ('Bob', 'Hey, what's up?'),
                        ('Charlie', 'Shuf is fantastic. I tried it on a 78 billion line text file.')]

            >>> cc = chainchat.Chat(messages, max_walk_length = 1000, enhance = True)

        """
        self.MAX_WALK_LENGTH = max_walk_length
        self.messages = messages
        self.Musers = pykov.Matrix()
        self.enhance_speed = enhance
        for mes, nxt in zip(messages[:-1], messages[1:]):
            assert (mes is not None)
            assert (nxt is not None)
            if (mes[0], nxt[0]) not in self.Musers:
                self.Musers[(mes[0], nxt[0])] = 0
            self.Musers[(mes[0], nxt[0])] = self.Musers[(mes[0], nxt[0])] + 1
        if finite: self.Musers[(messages[-1][0], None)] = 1
        elif self.Musers.succ(key=messages[-1][0]).sum() == 0:
            self.Musers[(messages[-1][0], messages[0][0])] = 1
        self.Musers.stochastic()
Exemplo n.º 3
0
    def generate(self,
                 head: Any = None) -> Generator[Tuple[Any, str], None, None]:
        """Return a generator that yields a new message on every next call.

        Args:
            head: User to start the interaction with. If no user supplied, the sender of the earliest message is used. Note, this user will not send a message but will be the user the first generated message responds to.

        Examples:
            # Generate 2 messages
            >>> gen = cc.generate('Alice')
            >>> for _ in range(2):
                    tpl = next(gen)
                    print(": ".join(tpl))

            Bob: Hey, what's up?
            Charlie: Shuf is fantastic. I tried it on a 78 billion line text file.

            # Generates an infinite amount of messages
            >>> for tpl in cc.generate('Alice')():
                    print(": ".join(tpl))

            Bob: Hey, what's up?
            Charlie: Shuf is fantastic. I tried it on a 78 billion line text file.
            ...
        """
        user = pykov.Vector({self.messages[0][0] if head is None else head:
                             1})  # starting point for markov chain
        while True:
            choice = (
                user *
                self.Musers).choose()  # choose next user to write a message
            if choice is None: return
            user = pykov.Vector(
                {choice: 1})  # create pykov vector for future user calculation
            if choice in self.word_matrices:
                Mword = self.word_matrices[choice]
            else:  # create word matrix
                Mword = pykov.Matrix({(1, 1):
                                      1})  # create matrix for word chain
                for mes in [m for m in self.messages
                            if m[0] == choice]:  # add every word to matrix
                    splt = mes[1].split(' ')
                    if (0, splt[0]) not in Mword: Mword[(0, splt[0])] = 0
                    Mword[(0, splt[0])] += 1
                    for word, nxt in zip(splt[:-1], splt[1:]):
                        if (word, nxt) not in Mword: Mword[(word, nxt)] = 0
                        Mword[(word, nxt)] += 1
                    if (splt[-1], 1) not in Mword: Mword[(splt[-1], 1)] = 0
                    Mword[(splt[-1], 1)] += 1
                Mword.stochastic()
                if not self.enhance_speed: self.word_matrices[choice] = Mword

            C = pykov.Chain(Mword)
            yield (choice, " ".join(C.walk(self.MAX_WALK_LENGTH, 0, 1)[1:-1]))
Exemplo n.º 4
0
    def get_symmetric_pykov_matrix(self):
        states = self.get_state_names()
        edge_dict = collections.OrderedDict()
        edges = self.language_construct.get_edgelist()
        vertex_degrees = self.language_construct.vs.degree()
        transition_probabilities = [
            1 / degree for degree in vertex_degrees if degree > 0
        ]

        for edge in edges:
            state1 = states[edge[0]]
            state2 = states[edge[1]]
            edge_dict[(state1, state2)] = transition_probabilities[edge[0]]
            edge_dict[(state2, state1)] = transition_probabilities[edge[1]]

        return pykov.Matrix(edge_dict)
Exemplo n.º 5
0
def buildMarkovChain(phrasesFile):
    phrases = construct_phrases_list(phrasesFile)
    print(phrases)
    wordCount = buildWordCount(phrases)
    connectionsCount = buildConnectionsCount(phrases)

    vector = pykov.Matrix()

    for connection in connectionsCount:
        initialState = connection[0]
        #print("(" + str(connectionsCount[connection]) + ", " + str(wordCount[initialState]) + ")")
        probability = (float(connectionsCount[connection]) /
                       float(wordCount[initialState]))
        #print(probability)
        vector[connection] = probability

    return pykov.Chain(vector)
Exemplo n.º 6
0
def markov_chain(states):
    T = pykov.Matrix()
    markov_states = []
    for i in xrange(0, len(states) - 1):
        markov_states.append((
            states[i],
            states[i + 1],
        ))

    for i in xrange(len(markov_states)):
        total = len([j for j in markov_states if j[0] == markov_states[i][0]]),
        amount = markov_states.count(markov_states[i])
        markov_states[i] += (amount / float(total[0]), )
    for i in markov_states:
        if (i[0], i[1]) not in T:
            T[(i[0], i[1])] = i[2]

    return pykov.Chain(T)
Exemplo n.º 7
0
def generate():
    import pykov

    phrasesData = ''
    with open('sayings.txt', "rt") as f:
        phrasesData = f.read()
    phrasesLines = phrasesData.split("\n")
    phrases = phrasesLines

    wordCount = dict()
    for phrase in phrases:
        words = phrase.split(" ")
        for index in range(len(words)):
            word = words[index]
            if word not in wordCount:
                wordCount[word] = 0
            wordCount[word] = wordCount.get(word) + 1

    connectionsCount = dict()
    for phrase in phrases:
        words = phrase.split(" ")
        for index in range(len(words) - 1):
            currState = (words[index], words[index + 1])
            if currState not in connectionsCount:
                connectionsCount[currState] = 0
            connectionsCount[currState] = connectionsCount.get(currState) + 1

    vector = pykov.Matrix()

    for connection in connectionsCount:
        initialState = connection[0]
        probability = (float(connectionsCount[connection]) /
                       float(wordCount[initialState]))
        vector[connection] = probability
    mc = pykov.Chain(vector)

    newPhrases = mc.walk(100, '+', '+\r')
    newPhrases = newPhrases[1:len(newPhrases) - 1]

    newPhrases = " ".join(newPhrases)
    newPhrase = newPhrases.split('+')[0].strip()
    return newPhrase.replace("í", "'")
Exemplo n.º 8
0
import pykov

d = pykov.Matrix({
    ('coal', 'WW'): 0.3483,
    ('coal', 'COD'): 0.2034,
    ('coal', 'NHN'): 0.2658,
    ('oil', 'WW'): 0.1399,
    ('oil', 'COD'): 0.1820,
    ('oil', 'NHN'): 0.2666,
    ('chemistry', 'WW'): 0.1397,
    ('chemistry', 'COD'): 0.1826,
    ('chemistry', 'NHN'): 0.1916,
    ('smelt', 'WW'): 0.0759,
    ('smelt', 'COD'): 0.0515,
    ('smelt', 'NHN'): 0.0417,
    ('farm', 'WW'): 0.0458,
    ('farm', 'COD'): 0.1116,
    ('farm', 'NHN'): 0.0355,
})

T = pykov.Chain(d)

import matplotlib.pyplot as plt
import numpy as np

# create some data to use for the plot
dt = 0.001
t = np.arange(0.0, 10.0, dt)
r = np.exp(-t[:1000] / 0.05)  # impulse response
x = np.random.randn(len(t))
s = np.convolve(x, r)[:len(x)] * dt  # colored noise
import pykov
import math

s = [0.99, 0.01, 0.00, 0.00, 0.00, 0.00]
i = [0.00, 0.80, 0.04, 0.01, 0.00, 0.15]
h = [0.00, 0.15, 0.80, 0.05, 0.00, 0.00]
u = [0.00, 0.00, 0.05, 0.80, 0.15, 0.00]
o = [0.00, 0.00, 0.00, 0.00, 1.00, 0.00]
r = [0.00, 0.00, 0.00, 0.00, 0.00, 1.00]

a = np.array([s, i, h, u, o, r])
str_states = ['S', 'I', 'H', 'U', 'O', 'R']
states = dict(zip(list(range(1, len(a) + 1)), str_states))
dic = {(states[i + 1], states[j + 1]): a[i][j]
       for i in range(len(a)) for j in range(len(a[i]))}
M = pykov.Matrix(dic)
print(M, end='\n\n')

# Todos os estados
print(M.states(), end='\n\n')
# Antecessores de cada estado
print(M.pred(), end='\n\n')
# Sucessores de cada estado
print(M.succ(), end='\n\n')

C = pykov.Chain(dic)
state = pykov.Vector(S=1)
# Distribuição de prob após N dias, começando no estado state
n_inicio = 282  # dias, entre o dia do primeiro infetado em Portugal e o dia da submissão do projeto
dia274 = C.pow(state, n_inicio)
n_fim = 365 - (31 + 28 + 1)
Exemplo n.º 10
0
    for st in json_data['statesArray']:
        states_dict[st['id']] = st['text']  # 'text' is the name of the state
        try:
            states_mirdef[st['text']] = st['mir'][0]
        except:
            states_mirdef[st['text']] = {
                "sfx.duration": "* TO 3",
                "sfx.inharmonicity.mean": "0.1"
            }  #default value
        try:
            states_dur[st['text']] = st['duration']  #default value
        except:
            states_dur[st['text']] = 1.  # default duration

    sd = states_dict
    T = pykov.Matrix()
    for st in json_data['linkArray']:
        # print( float(st['text']) )
        T[sd[st['from']], sd[st['to']]] = float(st['text'])

    try:
        T.stochastic()  #check
    except Exception, e:
        print(e)
        exit(1)

#########################
#FIXME: Time
# duration = 1 #FIXME: hardcoded (default duration)
# time_bt_states = 1 # (delay within states...)
#########################
Exemplo n.º 11
0
def main():

    if len(sys.argv) < 2:
        print(("\nBad amount of input arguments\n\t", Usage, "\n"))
        sys.exit(1)

    # JSON config file
    config = ""
    try:
        config = json.load(open(".apicultor_config.json", 'r'))
    except Exception as e:
        print(e)
        print("No json config file or error.")
        # sys.exit(2)

    api_key = config["Freesound.org"][0]["API_KEY"]

    #JSON composition file
    json_data = ""
    try:
        json_comp_file = sys.argv[1]
        # with open(json_file,'r') as file:
        #     json_data = json.load( file )
        json_data = json.load(open(json_comp_file, 'r'))
    except Exception as e:
        print(e)
        print("JSON composition file error.")
        sys.exit(2)

    print("Starting MIR state machine")

    states_dict = dict()  # id to name conversion
    states_dur = dict()  #states duration
    start_state = json_data['statesArray'][0][
        'text']  #TODO: add as property (start: True)
    for st in json_data['statesArray']:
        states_dict[st['id']] = st['text']
        try:
            states_dur[st['text']] = st['duration']
        except:
            states_dur[st['text']] = 1.  # default duration

    sd = states_dict
    T = pykov.Matrix()
    for st in json_data['linkArray']:
        # print( float(st['text']) )
        T[sd[st['from']], sd[st['to']]] = float(st['text'])

    try:
        T.stochastic()  #check
    except Exception as e:
        print(e)
        exit(1)


#########################
#FIXME: Time
# duration = 1 #FIXME: hardcoded (default duration)
# time_bt_states = 1 # (delay within states...)
#########################
#########################

# Init conditions
#state = 'idle' #start state
# state = "A" #start state
    state = start_state
    previous_state = "H"

    #Fixed amount or infinite with while(1 ) ()
    # events = 10 # or loop with while(1)
    # for i in range(events):
    while (1):
        print(("State: %s" %
               state))  # TODO: call the right method for the state here
        #(optional) change sound in the same state or not (add as json config file)
        if state != previous_state:
            #retrieve new sound
            call = '/list/samples'  #gets only wav files because SuperCollider
            response = urllib.request.urlopen(URL_BASE + call).read()
            audioFiles = list()
            for file in response.split('\n'):
                if len(file) > 0:  #avoid null paths
                    audioFiles.append(file)
                    # print file
            file_chosen = audioFiles[random.randint(0, len(audioFiles) - 1)]
            print(file_chosen)

            # Hardcoded sound for each MIR state
            # file_chosen = snd_dict[state]

            pyo_synth(file_chosen, dry_val)
            # granular_synth(file_chosen)
            # external_synth(file_chosen)

        time_bt_states = states_dur[state]
        # time_between_notes = random.uniform(0.,2.) #in seconds
        #time.sleep(time_between_notes)

        # MIDI
        # notes = Notein(poly=10, scale=1, mul=.5)
        # p = Port(notes['velocity'], .001, .5)

        # # Add inputs to the mixer
        # mm.addInput(voice=new_voice, input=sfplay)
        #mm.addInput(voice=new_voice, input=pvs)

        # Delay within states
        time.sleep(time_bt_states)

        #next state
        previous_state = state
        state = T.succ(state).choose()  #new state