Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
2.1 kB
0
Indexable
Never
import string ## so we can use the string.ascii_letters variable

# open the file and read all the words and spaces into an array
def read():
    f = open("gruffalo.txt", "r") ## open the file
    word = ""
    gruffalo = [] ## empty array to hold the text

    while True:
        character = f.read(1)
        if not character: ## reached the EOF, finish the while
            break

        if character in string.ascii_letters: ## if a letter, start to form a word
            word += character

        elif character in string.punctuation: ## if punctuation
            if character == "'": ## if apostrophe, add this to the word
                word += character

            else: ## otherwise must be the end of a word, so append to array
                gruffalo.append(word)
                gruffalo.append(character) ## and append the punctuation
                word = "" ## reset the word ready for next loop

        else: ## end of word (hit a space)
            gruffalo.append(word) ## append the word to the array
            gruffalo.append(" ") ## append a space
            word = "" ## reset the word ready for next loop

    f.close()
    return gruffalo ## sends the text back to the main program flow

def addToDictionary(words):
    # use a for loop to go through the words array
    # if doesn't already exist in dictionary (research how to do this!)
        # add to dictionary
        # key is word from the gruffalo, value should be a count (e.g 'A':0, ' ': 1, 'mouse':2, 'took': 3)
    # else skip


words = read() ## receive the text into the variable called words
addToDictionary(words)


####################################################################################
test = ["Meg", "Orla", "Cathy", "Clodagh", "Chiamanda", "Anna", "Shalom"]

# initialise an empty dictionary
names = {}

# add names to a dictionary - how do we ensure uniqueness?
for i in range (0, len(test)):
   ##names.update({test[i]:i})
    names[test[i]] = i # only works if we know the key isn't in dictionary
print(names)