Ejemplo n.º 1
0
def read_corpus(order_stat: int, file) -> dict:
    DATA = list(read_file_values(file))
    myDict = defaultdict(tuple)
    order_stat = int(order_stat)
    key_list = []

    for j in range(0, len(DATA)):
        key = tuple()
        try:
            for i in range(0, order_stat):
                key = key + (DATA[j], ) if i == 0 else key + (DATA[j + 1], )
            key_list.append(key)
        except Exception as e:
            pass
    for i in range(0, len(key_list)):
        try:
            if i + 2 < len(key_list):
                new_value = key_list[i + 2][0]
            elif i + 2 > len(key_list):
                raise Exception
            else:
                new_value = key_list[-1][0]
            if key_list[i] in myDict.keys():  #Check if the key exists
                nv = set(myDict[key_list[i]])
                nv.add(new_value)
                myDict[key_list[i]] = list(nv)
            else:
                myDict[key_list[i]] = list(new_value)
        except:
            pass

    return myDict
def read_graph(file) -> dict:
    """
    This function takes a file and returns a dict with the nodes.
    """
    node_dict = defaultdict(set)
    for line in goody.read_file_values(file, sep=None, conversions=None):
        text_line = line.split(';')
        node_dict[text_line[0]].add(text_line[1])
    return node_dict
def read_voter_preferences(file) -> dict:
    """
    This function takes a file and returns a dict with the voter preferences in a list.
    """
    voter_dict = defaultdict(list)
    for line in goody.read_file_values(file, sep=None, conversions=None):
        text_line = line.split(';')
        voter = text_line.pop(0)
        voter_dict[voter].extend(text_line)
    return voter_dict
Ejemplo n.º 4
0
def read_corpus(order_stas, file:'open file') -> dict:
    corpusdict = defaultdict(list)
    g = goody.read_file_values(f)
    prereadwords = []
    for x in range(eval(order_stas)):
        prereadwords.append(next(g))
    for i in g:
        if i not in corpusdict[tuple(prereadwords)]:
            corpusdict[tuple(prereadwords)].append(i)
        prereadwords = prereadwords[1:]
        prereadwords.append(i)
    return dict(corpusdict)
Ejemplo n.º 5
0
def read_corpus(order_stat, infile):
    corpus=dict()
    word_list = list(goody.read_file_values(infile))
    start = 0
    while (start+order_stat) < len(word_list):
        word_tuple = tuple(word_list[start:start+order_stat])
        corpus.setdefault(word_tuple,{word_list[start+order_stat]}).add(word_list[start+order_stat])
        start+=1
    #Convert the set to a list
    for i in corpus:
        corpus[i]=list(corpus[i])
    infile.close()
    return corpus
Ejemplo n.º 6
0
def read_corpus(order_stat, infile):
    corpus = dict()
    word_list = list(goody.read_file_values(infile))
    start = 0
    while (start + order_stat) < len(word_list):
        word_tuple = tuple(word_list[start:start + order_stat])
        corpus.setdefault(word_tuple, {word_list[start + order_stat]}).add(
            word_list[start + order_stat])
        start += 1
    #Convert the set to a list
    for i in corpus:
        corpus[i] = list(corpus[i])
    infile.close()
    return corpus
def read_corpus(order: int, file) -> dict:
    """
    This function takes an order statistic and reads a file and returns a dict.
    """
    corpus_dict = defaultdict(list)
    text = goody.read_file_values(file, sep=None, conversions=None)
    key = []
    for number in range(order):
        key.append(next(text))
    for line in text:
        next_word = line
        if next_word not in corpus_dict[tuple(key)]:
            corpus_dict[tuple(key)].append(next_word)
        key.pop(0)
        key.append(next_word)
    return corpus_dict
def read_corpus(n: int, file) -> dict:
    file_iterator = read_file_values(file)
    
    preread_list = []
    corpus = defaultdict(list)
    
    for i in range(n):
        preread_list.append(file_iterator.__next__())
    
    for word in file_iterator:
        if word not in corpus[tuple(preread_list)]:
            corpus[tuple(preread_list)].append(word) 
        preread_list.remove(preread_list[0])
        preread_list.append(word)
        
    return corpus
Ejemplo n.º 9
0
def read_corpus(order_static:int, text_file)->dict:
    '''
    the function creates corpus(tuple:list(str))
    and return it.
    '''
    corpus = defaultdict(list)
    raw_words = []
    for word in read_file_values(text_file):
        raw_words.append(word.rstrip("\n"))               # create long list of all words from text_file
    for word_index in range(len(raw_words)-order_static):   # iterate based on lenth of the list by index 
        ## here is fun part, create tuple using comprehension removing \n in front of every first line's letter 
        ## using index tuple will iterate from index through index + order_static which will be tuple(list[index], list[index+1] ... list[index+order_static-1])
        ## then put the tuple into corpus as a key 
        ## append list[index+order_static] to value(list) if it is not in list
        if corpus[tuple(word.lstrip("\n") for word in raw_words[word_index:word_index+order_static])].count(raw_words[word_index + order_static].lstrip("\n")) == 0:
            corpus[tuple(word.lstrip("\n") for word in raw_words[word_index:word_index+order_static])].append(raw_words[word_index + order_static].lstrip("\n"))
    return corpus
Ejemplo n.º 10
0
def read_fa(file) -> dict:
    """
    This function reads a file and returns the finite automaton as a dict.
    """
    fa_dict = defaultdict(list)
    
    for line in goody.read_file_values(file, sep=None, conversions=None):
        text_line = line.split(';')
        odd_list = []
        even_list = []
        for index in range(len(text_line)):
            if index%2 == 0:
                even_list.append(text_line[index])
            else:
                odd_list.append(text_line[index])
        z = zip(odd_list,even_list[1:])
        fa_dict[even_list[0]].extend(list(z))
    return fa_dict
Ejemplo n.º 11
0
def read_ndfa(file) -> dict:
    """
    This function reads a file and returns the non-deterministic finite automaton as a dict.
    """
    ndfa_dict = defaultdict(dict)
    
    for line in goody.read_file_values(file, sep=None, conversions=None):
        text_line = line.split(';')
        ndfa_dict[text_line[0]] = defaultdict(set)
        even_list = []
        odd_list = []
        for index in range(len(text_line)):
            if index%2 == 0:
                even_list.append(text_line[index])
            else:
                odd_list.append(text_line[index])
        even_list.pop(0)
        for index in range(len(odd_list)):
            ndfa_dict[text_line[0]][odd_list[index]].add(even_list[index])
    return ndfa_dict
Ejemplo n.º 12
0
def read_corpus(order, openfile):
    '''
     has an order statistic parameter and and open (file) parameter; 
     it returns the dict representing the corpus of words in a file (mine is 9 lines).
    '''
    dict1 = dict()
    filelist = (openfile)
    old = []
    my = goody.read_file_values(filelist)

    for i in range(order):
        old.append(next(my))
    while True:
        try:
            next_one = next(my)
            dict1.setdefault(tuple(old), set()).add(next_one)
            old.append(next_one)
            old.pop(0)
        except:

            #             print (dict1)
            return dict1
Ejemplo n.º 13
0
def read_corpus(order, openfile):
    '''
     has an order statistic parameter and and open (file) parameter; 
     it returns the dict representing the corpus of words in a file (mine is 9 lines).
    '''
    dict1 = dict()
    filelist = (openfile)
    old = []
    my=goody.read_file_values(filelist)

    for i in range(order):
        old.append(next(my))
    while True:
        try:
            next_one = next(my)        
            dict1.setdefault(tuple(old), set()).add(next_one)
            old.append(next_one)
            old.pop(0)
        except:

#             print (dict1)  
            return dict1
Ejemplo n.º 14
0
    mins,maxs=-1,-1
    for k in sorted(corpus):
        print(' ',k,'can be followed by any of', corpus[k])
        mins = len(corpus[k]) if mins == -1 or len(corpus[k]) < mins else mins
        maxs = len(corpus[k]) if maxs == -1 or len(corpus[k]) > maxs else maxs
        if len(corpus[k]) == 46:
            print (k,corpus[k])
    print('min/max =',str(mins)+'/'+str(maxs)) 


def produce_text(corpus,text,count):
    os = len(text)
    for i in irange(count):
        key = tuple(start[-os:])
        if key not in corpus:
            text.append(None)
            return text
        start.append(choice(corpus[key]))
    return start    



os = prompt.for_int('Enter order statistic',is_legal=lambda x : x >= 1)
corpus = read_corpus(os, goody.read_file_values(goody.safe_open('Enter file to process', 'r', 'Cannot find that file')))
print_corpus(corpus)

print('\nEnter '+str(os)+' words to start with')
start = [prompt.for_string('Enter word '+str(i)) for i in irange(os)]
how_many = prompt.for_int('Enter # of words to generate',is_legal=lambda x : x > 0)
text = produce_text(corpus,start,how_many)
print('Random text =',text)
# Ford Tang, 46564602, Lab 2
# Somebody Special, Lab 2
# We certify that we worked cooperatively on this programming
#   assignment, according to the rules for pair programming

import goody




if __name__ == '__main__':
    for line in goody.read_file_values(file = goody.safe_open(prompt_text="Enter file with graph", mode='r', error_message="File does not exists.", default='' ), sep=None, conversions=None):
        print(line)
Ejemplo n.º 16
0
        maxs = len(corpus[k]) if maxs == -1 or len(corpus[k]) > maxs else maxs
        if len(corpus[k]) == 46:
            print(k, corpus[k])
    print('min/max =', str(mins) + '/' + str(maxs))


def produce_text(corpus, text, count):
    os = len(text)
    for i in irange(count):
        key = tuple(start[-os:])
        if key not in corpus:
            text.append(None)
            return text
        start.append(choice(corpus[key]))
    return start


os = prompt.for_int('Enter order statistic', is_legal=lambda x: x >= 1)
corpus = read_corpus(
    os,
    goody.read_file_values(
        goody.safe_open('Enter file to process', 'r',
                        'Cannot find that file')))
print_corpus(corpus)

print('\nEnter ' + str(os) + ' words to start with')
start = [prompt.for_string('Enter word ' + str(i)) for i in irange(os)]
how_many = prompt.for_int('Enter # of words to generate',
                          is_legal=lambda x: x > 0)
text = produce_text(corpus, start, how_many)
print('Random text =', text)
Ejemplo n.º 17
0
    """
    result = []
    result.append(start_state)
    current_state = [start_state]
    for item in str_input:
            new_set = set()
            for state in current_state:
                if item in ndfa_dict[state]:
                    for transition in ndfa_dict[state][item]:
                        new_set.add(transition)
            if item in ndfa_dict[state]:
                result.append((item, new_set))
                current_state = list(new_set)
    return result

def interpret(process_list:list) -> None:
    """
    This function takes the process list and prints it.
    """
    print('\nStarting new simulation')
    print('Start state = ' + str(process_list[0]))
    for item in process_list[1:]:
        print('  Input = ' + str(item[0]) + '; new possible states = ' + str(item[1]))
    print('Stop state(s) = ' + str(process_list[-1][-1]))

if __name__ == "__main__":
    ndfa_dict = read_ndfa(goody.safe_open(prompt_text="Enter file with non-deterministic finite automaton", mode='r', error_message="File does not exists.", default='' ))
    print_ndfa(ndfa_dict)
    for line in goody.read_file_values(goody.safe_open(prompt_text="\nEnter file with start-state and input", mode='r', error_message="File does not exists.", default='' ), sep=None, conversions=None):
        text_line = line.split(';')
        interpret(process(ndfa_dict, text_line[0], text_line[1:]))