# Exercise 22 (and Solution)
#
# Given a .txt file that has a list of a bunch of names, count how many of each name there are in the file,
# and print out the results to the screen. I have a .txt file for you, if you want to use it!
#
# Extra:
# Instead of using the .txt file from above (or instead of, if you want the challenge), take this .txt file,
# and count how many of each “category” of each image there are. This text file is actually a list of files
# corresponding to the SUN database scene recognition database, and lists the file directory hierarchy for the
# images. Once you take a look at the first line or two of the file, it will be clear which part represents the
# scene category. To do this, you’re going to have to remember a bit about string parsing in Python 3. I talked a
# little bit about it in this post.


with open('nameslist.txt', 'r') as open_file:
    data = open_file.read().split()
    dict_names = {}
    for name in data:
        if name in dict_names.keys():
            dict_names[name] += 1
        else:
            dict_names[name] = 1
print(dict_names)

# Extra:
with open('Training_01.txt', 'r') as open_file:
    line = open_file.readline()
    dict_categories = {}
    while line:
        line = line[3:-26]
        if line in dict_categories.keys():
            dict_categories[line] += 1
        else:
            dict_categories[line] = 1
        line = open_file.readline()
print(dict_categories)
