def store_anagrams():
	db=shelve.open('shelf','c')
	d=anagram_sets.all_anagrams('words.txt')
	for key in d:
		if len(d[key])>1:
			db[key]=d[key]
	db.close()
Example #2
0
def store_anagrams(file_to_analyze):
    write_data = shelve.open('anagram_dbEX.db', 'c')
    anagram_dict = all_anagrams(file_to_analyze)
    for word, word_list in anagram_dict.items():
        write_data[word] = word_list
    write_data.close()
    return
Example #3
0
def main():
    anagram_map = all_anagrams("book/words.txt")
    # store_anagrams("book/anagram_db", anagram_map)
    # print (read_anagrams("book/anagram_db", "stop"))
    for word,wordList in anagram_map.items():
        if len(wordList)>1:
            print(word,wordList)
Example #4
0
def store_anagram(filename):
    db = dbm.open('shelf', 'c')
    anagram_dic = all_anagrams(filename)
    for word in anagram_dic:
        db[word] = anagram_dic[word]

    db.close()
Example #5
0
def main(script, command='make_db'):
    print(command)
    if command == 'make_db':
        anagram_map = anagram_sets.all_anagrams('words.txt')
        store_anagrams('anagrams.db', anagram_map)
    else:
        print(read_anagrams('anagrams.db', command))
Example #6
0
def main(script, command='make_db'):
    #  how it works: <command> can be replace from make_db with the word you are looking for. That`s it!
    if command == 'make_db':
        anagram_map = all_anagrams('words.txt')
        store_anagrams('anagrams.db', anagram_map)
    else:
        print(read_anagrams('anagrams.db', command))
Example #7
0
def store_anagrams(filename):
	d = anagram_sets.all_anagrams(filename)

	with dbm.open('dictionaries', 'c') as db:
		for word, anagrams in d.items():
			s = pickle.dumps(anagrams)
			db[word] = s
def main():
   # First, create your dictionary of anagrams
    d = anagram_sets.all_anagrams('../words.txt')
    # Create our anagram shelf
    anagram_shelf = store_anagrams(d)
    # Test and see if "less" properly returns anagrams
    print read_anagrams(anagram_shelf, 'less')
    # Test and see if "foal" properly returns anagrams
    print read_anagrams(anagram_shelf, 'foal')
Example #9
0
def store_anagrams(filename = 'words.txt'):
    """
    Finds the anagrams of the words in the given file and stores 
    them to 'anagram_shelf'

    """
    fout = shelve.open('anagram_shelf')
    fout.update(anagram_sets.all_anagrams(filename))
    fout.close()
Example #10
0
def main():
    # First, create your dictionary of anagrams
    d = anagram_sets.all_anagrams('../words.txt')
    # Create our anagram shelf
    anagram_shelf = store_anagrams(d)
    # Test and see if "less" properly returns anagrams
    print read_anagrams(anagram_shelf, 'less')
    # Test and see if "foal" properly returns anagrams
    print read_anagrams(anagram_shelf, 'foal')
Example #11
0
def store_anagrams():
    d = anagram_sets.all_anagrams('words.txt')

    #creates database
    db = shelve.open('anagrams.db' ,'c')

    for key in d:
        db[key] = d[key]

    db.close
Example #12
0
def store_anagrams(filepath):
    """
    creates dictionary of anagrams in a list of words
    in a given file at filepath
    stores dictionary in a 'shelf'
    """
    # extract filename from given path
    # for use as shelf file name
    filename = os.path.splitext(filepath)[0]
    anagrams = anagram_sets.all_anagrams(filepath)
    print(anagrams)
    try:
        with shelve.open(filename) as db:
            for key, value in anagrams.items():
                db[key] = value
    except:
        print('error writing to shelf')
Example #13
0
import anagram_sets

anagram_list = anagram_sets.all_anagrams('words.txt')

print(type(anagram_list))
olist = []
for key in anagram_list:
    if len(anagram_list[key]) > 1:
        olist.append((key, anagram_list[key]))


def store_anagrams(alist, ofile='anagrams.txt'):
    fout = open(ofile, 'w')
    for line in alist:
        ans = ' '.join(line[1])
        fout.write(line[0] + ', ' + ans + '\n')


def read_amagrams(aword, ifile='anagrams.txt'):
    fin = open(ifile)
    for line in fin:
        key, anagrams = line.split(',')
        if key == aword:
            print(anagrams)


store_anagrams(olist)
read_amagrams('abhmo')

"""
12_03 - had a wierd ASCII error in this text - so removed it.
"""
import anagram_sets

if __name__ == '__main__':
    anagram_map = anagram_sets.all_anagrams('12_files/words2.txt')
    anagram_sets.print_anagram_sets_in_order(anagram_map)
Example #15
0
 def store_anagrams(self):
     self.shelf = a.all_anagrams(self.filename)
Example #16
0
def main():
    anagram_map = anagram_sets.all_anagrams('words.txt')
   # store_anagrams('funciona', anagram_map)
    print(read_anagrams('task', 'prateleira'))
Example #17
0
    d: map from word to list of anagrams
    """
    for anagrams in d.values():
        for word1 in anagrams:
            for word2 in anagrams:
                if word1 < word2 and word_distance(word1, word2) == 2:
                    print(word1, word2)


def word_distance(word1, word2):
    """Computes the number of differences between two words.

    word1, word2: strings

    Returns: integer
    """
    assert len(word1) == len(word2)

    count = 0
    for c1, c2 in zip(word1, word2):
        if c1 != c2:
            count += 1

    return count


if __name__ == '__main__':
    sets = anagram_sets.all_anagrams('words.txt')
    metathesis_pairs(sets)
    # open shelf
    # store dictionary key and values in shelf

    with shelve.open("shelf.txt", 'c') as shelf:
        for anagram, anagram_list in anag_map.items():
            shelf[anagram] = anagram_list


def read_anagrams(filename, word):
    # take stirngs file name (shelf file) & word to look up
    # open shelf
    # try and except to handle KeyError when sig is not found

    with shelve.open("shelf.txt", 'c') as shelf:
        sig = signature(word)
        try:
            return shelf[sig]
        except KeyError:
            return []


if __name__ == '__main__':
    anagram_map = all_anagrams('words.txt')
    store_anagrams(anagram_map)

    # should return list of anagram for aah
    print(read_anagrams("shelfff.txt", "aah"))

    # should return [] because it does not exist in shelf
    print(read_anagrams("shelfff.txt", "8888888888888888"))
Example #19
0
def store_anagrams(filename):
    """
    builds an anagram list and then stores it with pickle, returning the string to get it back
    """
    d = shelve.open(filename)
    d.update(ana.all_anagrams('../chap11/words.txt'))
Example #20
0
def main(script, command='make_db'):
    if command == 'make_db':
        anagram_map = all_anagrams('words.txt')
        store_anagrams('anagrams.db', anagram_map)
    else:
        print(read_anagrams('anagrams.db', command))
Example #21
0
def store_anagrams(f1):
    #anagram_dict = dict()
    anagram_dict = anagram_sets.all_anagrams(f1)
    return anagram_dict