Пример #1
0
 def createMarkovChains(self, increment=False):
     """ Generate Markov Chains and store it in the database """
     mt = self._markovify()
     for entry in mt:
         (first, second, third) = [x.id for x in entry]
         #Frist the markov chain
         mw = Markov.select(
             AND(Markov.q.first_wordID == first,
                 Markov.q.second_wordID == second,
                 Markov.q.third_wordID == third))
         markov = list(mw)
         if markov and increment == True:
             markov[0].increment()
         else:
             Markov(first_word=first, second_word=second, third_word=third)
         #Next the Markov Lex chain
         (first, second, third) = [x.main_type.id for x in entry]
         ml = MarkovLex.select(
             AND(MarkovLex.q.first_lexID == first,
                 MarkovLex.q.second_lexID == second,
                 MarkovLex.q.third_lexID == third))
         markovlex = list(ml)
         if markovlex and increment == True:
             markovlex[0].increment()
         else:
             MarkovLex(first_lex=first, second_lex=second, third_lex=third)
Пример #2
0
def main():
    api_key = input('API key for the NYT article search API')
    NYT_scraper = NewYorkTimes(api_key)
    subject = input('Enter the subject to generate headlines for')
    data = NYT_scraper.get_text(subject)
    x = Markov(2)
    count = input('Enter the number of headlines to generate')
    for _ in range(count):
        print(x.generate_text(10).strip('*'))
Пример #3
0
def makeWordModel(filename):
    # creates a Marko model from words in  filename
    infile = open(filename)
    model = Markov()
    for line in infile:
        words = line.split()
        for w in words:
            model.add(w)
    infile.close()

    # Add a sentinel at the end of the text
    model.add(None)
    model.reset()
    return model
Пример #4
0
def writeToFile(sourcefolderName, outputFileName, sequenceLength):
    markov = Markov()
    folderName = os.path.abspath(sourcefolderName)
    files = getFiles(folderName)
    print str(len(files)) + " files found"

    #parse each file and push to markovDS
    for fileName in files:
        parseFile(fileName, markov, sequenceLength)

    print "Files parsed"

    #write as json to file
    markov.writeToFile(outputFileName)
    print "Markov written to file"
Пример #5
0
def makeWordModel(filename):
    '''creates a Morkov model from the words in the file with filename.
    pre: The file with name filename exists.
    post: A Markov chain from the text in the file is returned. '''
    # creates a Markov model from words in filename
    infile = open(filename)
    tmpmodel = Markov()
    for line in infile:
        words = line.split()
        for w in words:
            tmpmodel.add(w)
    infile.close()
    # Add a sentinel at the end of the text
    tmpmodel.add(None)
    tmpmodel.reset()
    return tmpmodel
Пример #6
0
def compose(sourceFile, outputFileName, length):
    markov = Markov(sourceFile)

    sequenceLength = markov.sequenceLength()

    # Build random beging of song
    notes = []
    for i in range(sequenceLength):
        notes.append(random.randint(50, 60))

    # Build list of notes
    while (len(notes) < length):
        newNote = markov.get(notes[-sequenceLength:])
        if newNote is not None:
            notes.append(int(newNote))
        else:
            print "INFO: No possible note found"
            del notes[-1]

    print "Song Composed:"
    print notes

    # Make patern into
    pattern = midi.Pattern()
    pattern.make_ticks_abs()
    track = midi.Track()
    pattern.append(track)

    for note in notes:
        on = midi.NoteOnEvent(tick=0, velocity=NOTE_VELOCITY, pitch=note)
        off = midi.NoteOffEvent(tick=NOTE_DURATION[get_rand_duration()],
                                pitch=note)
        track.append(on)
        track.append(off)

    eot = midi.EndOfTrackEvent(tick=1)
    track.append(eot)
    midi.write_midifile(outputFileName, pattern)

    print "Song written to file: %s" % outputFileName
Пример #7
0
def parseMidis(sourcefolderName, outputFileName, markovOrder, parseFor):
    print "Begining parse"

    folderName = os.path.abspath(sourcefolderName)
    files = getFiles(folderName)
    numOfFiles = len(files)
    print "%d files found" % numOfFiles

    #parse each file and push to markovDS
    transitions = Markov()
    i = 1
    for fileName in files:
        print "parsing file %d of %d" % (i, numOfFiles)
        parseFile(fileName, transitions, markovOrder, parseFor)
        i += 1
        if i % 100 == 0:
            transitions.writeToFile(outputFileName)
    print "Files parsed"

    #write as json to file
    transitions.writeToFile(outputFileName)
    print "Markov written to file"
Пример #8
0
from Markov import Markov

city_weather = {
    'New York': 'rainy',
    'Chicago': 'snowy',
    'Seattle': 'rainy',
    'Boston': 'hailing',
    'Miami': 'windy',
    'Los Angeles': 'cloudy',
    'San Francisco': 'windy'
}

city_weather_prediction = {}

for city in city_weather:
    weather = Markov(day_zero_weather=city_weather[city])
    weather.load_data(file_path='./weather.csv')
    prediction = weather._simulate_weather_for_day(7, trials=100)
    city_weather_prediction[city] = prediction

for city in city_weather_prediction:
    print(city, ':', city_weather_prediction[city])

print('\nMost likely weather in seven days\n---------------------------------')
for city in city_weather_prediction:
    weather_guess = max(city_weather_prediction[city],
                        key=city_weather_prediction[city].get)
    print(city, ':', weather_guess)
Пример #9
0
    markov.load('sina_news_gbk', 'gbk')
    markov.load('novels', 'utf-8')
    markov.load('test_txt', 'utf-8')

    print('start calculating')

    markov.train()
    return


if __name__ == '__main__':
    lambda0 = 0
    if len(sys.argv) == 2:
        lambda0 = float(sys.argv[1])
    markov = Markov(lambda0)
    print('lambda = {0}'.format(lambda0))
    train()

    print('start saving')
    # save the 'first' list
    with open('../data/first.json', 'w', encoding='utf-8') as outputfile:
        json.dump(markov.first, outputfile, indent=4)

    # save the transitionMatrix
    # separate the list into several parts to save
    for number in range((markov.sizeCharacters // 300) + 1):
        with open('../data/transitionMatrix' + str(number) + '.json',
                  'w',
                  encoding='utf-8') as outputfile:
            tmp_list = markov.transition[number *
Пример #10
0
from Markov import Markov
from collections import Counter

city_weather = {
    'New York': 'rainy',
    'Chicago': 'snowy',
    'Seattle': 'rainy',
    'Boston': 'hailing',
    'Miami': 'windy',
    'Los Angeles': 'cloudy',
    'San Francisco': 'windy'
}

print(
    "The number of occurrences of each weather condition over the 100 trials for each city"
)
print("----------------------------------")
predictions = {}
for city, weather in city_weather.items():
    weather_today = Markov(day_zero_weather=weather)
    weather_today.load_data(file_path='./weather.csv')
    ans = Counter(weather_today.get_weather_for_day(day=7, trials=100))
    predictions[city] = max(ans.items(), key=lambda x: x[1])[0]
    print("{}: {}".format(city, dict(ans)))

print()
print("Most likely weather in seven days")
print("----------------------------------")
for city, weather in predictions.items():
    print("{}: {}".format(city, weather))
Пример #11
0
#!/bin/usr/env python3

from Markov import Markov

weather_today = Markov()
weather_today.load_data()
print(weather_today.get_prob('sunny', 'cloudy'))
Пример #12
0
city_weather = {
    'New York': 'rainy',
    'Chicago': 'snowy',
    'Seattle': 'rainy',
    'Boston': 'hailing',
    'Miami': 'windy',
    'Los Angeles': 'cloudy',
    'San Francisco': 'windy'
}

weather_types = ['sunny', 'cloudy', 'rainy', 'snowy', 'windy', 'hailing']

forecasts = []

for key, value in city_weather.items():
    weather_today = Markov(value)
    weather_today.load_data()
    city_forecast = weather_today.get_weather_for_day(7, 100)
    forecasts.append(np.array(city_forecast))

city_forecast_dicts = []

for key, city_forecast in zip(city_weather.keys(), forecasts):
    city_forecast_dict = {}
    #    weather_type_occurances = []
    for weather_type in weather_types:
        occurance = np.count_nonzero(city_forecast == weather_type)
        #        weather_type_occurances.append(occurances)
        city_forecast_dict.update({weather_type: occurance})
    city_forecast_dicts.append(city_forecast_dict)
    print(key, ':', city_forecast_dict)
Пример #13
0
if __name__ == "__main__":
    # Prompt user for parameters
    order = int(input("Enter order: "))
    while (order <= 0):
        order = int(input("Invalid order\nEnter an order greater than 0: "))

    length = int(input("Enter desired length of sampled output: "))
    while (length <= 0):
        length = int(input("Invalid length\nEnter a length greater than 0: "))

    text = readFile(input("Enter name of local text file: "))
    outFileName = input("Enter name of output text file: ")

    # Create the model with text and order as parameters
    model = Markov(text, order)

    # If you wish to see the data dictionary
    # m.printdict(m.seqCollection)
    # m.printdict(m.tran)

    # one random sequence of length (order) that exists in the source text
    # this is the start of the seed
    randomSequence = model.randSeq(model.seqCollection)

    # You can also feed in the first sequence as well
    firstSeq = model.getFirstSeq(text, order)

    output = model.generateMarkovChain(
        randomSequence, length)  # generate output with desired length
    writeFile(output, outFileName)  # write out the results
Пример #14
0
from person import person
import random
from Markov import Markov
from operator import itemgetter

personList = []
numberOfPeople = 500
generationNumber = 500
people = 0
year = 1
generations = 20
generationFactor = 5
babylonian = Markov('babylonian.txt')
latin = Markov('latin.txt')
hebrew = Markov('hebrew.txt')
greek = Markov('greek.txt')
egyptian = Markov('egyptian.txt')


def generateGeneration(tempList):
    personList = sorted(tempList,
                        key=lambda current: current.score,
                        reverse=True)
    toGet = int(generationNumber / generationFactor)
    i = 0
    newList = []
    print("Number Survived: " + str(numberOfPeople) + " toGet " + str(toGet))
    fashion = egyptian
    while i < generationNumber:
        choose = random.randrange(1, 5)
        if choose == 1:
Пример #15
0
 def build_model(self, emails):
     text_list = [email.content for email in emails]
     text = ' '.join(text_list)
     text_model = Markov(text)
     return text_model
Пример #16
0
def compose(pitchInputFileName, rhythmInputFileName, outputFileName,
            songLength):
    pitchTransitions = Markov(pitchInputFileName)
    pithOrder = pitchTransitions.order()
    rhythmTransitions = Markov(rhythmInputFileName)
    rhythmOrder = rhythmTransitions.order()

    print "Markovs loaded into memmory"

    # Build random beging of song
    chords = []
    for i in range(pithOrder):
        chords.append([random.randint(50, 60)])
    # Build list of chords
    while (len(chords) < songLength):
        newChord = pitchTransitions.get(chords[-pithOrder:])
        if newChord is not None:
            newChord = ast.literal_eval(newChord)
            chords.append(newChord)
        else:
            print "INFO: No possible pitch/chord found"
            del chords[-1]
            chords.append([random.randint(50, 60)])
    # prettier endings
    chords[-1] = chords[-2]

    # Create begining of rhythm track
    rhythms = []
    for i in range(rhythmOrder):
        rhythms.append(4)
    # Build list of note lengths
    while (len(rhythms) < songLength):
        newLength = rhythmTransitions.get(rhythms[-rhythmOrder:])
        if newLength is not None:
            newLength = int(newLength)
            rhythms.append(newLength)
        else:
            print "INFO: No possible rhythm found"
            del rhythms[-1]
    #SONG SHOULD END WITH HOLE NOTE
    rhythms[-1] = 16
    rhythms[-2] = 16

    print "Song Composed:"

    # Make patern into
    pattern = midi.Pattern()
    pattern.make_ticks_abs()
    track = midi.Track()
    pattern.append(track)

    for i in range(songLength):
        length = int((220 / 16) * rhythms[i])
        # for pitch in chords[i]:
        for pitch in chords[i]:
            on = midi.NoteOnEvent(tick=0, velocity=NOTE_VELOCITY, pitch=pitch)
            track.append(on)
        for pitch in chords[i]:
            off = midi.NoteOffEvent(tick=length, pitch=pitch)
            track.append(off)

    eot = midi.EndOfTrackEvent(tick=0)
    track.append(eot)
    midi.write_midifile(outputFileName, pattern)

    print "Song written to file: %s" % outputFileName