# Program to search through given strings and
# find HTML tags and HTML entities

# HTML Tags Regex: (<.>)
# HTML Entities Regex: (&.*;)

# RegEx Library for Python
import re

# Should get all <*> HTML tags
tagRegex = "(<.*?>)"
# Should get all &***; HTML entities
entityRegex = "(&.*?;)"

# Retrieve data
data = open("data.txt", "r")
line = data.readline()

# In my format, number of entries is the first line of the data file
numEntries = int(line)

# The identified matches found with RegEx
identifiedMatches = []

# For the number of entries that are in the file...
for entry in range(numEntries):
    # Get the next title
    line = data.readline()
    # If there is a match for HTML Tags....
    if re.search(tagRegex, line) is not None:
        # Add each HTML Tag match to the identifiedMatches list
        for match in re.findall(tagRegex, line):
            identifiedMatches.append({"name" : "tagRegex", "value" : match})
    # If there is a match for HTML entities...
    if re.search(entityRegex, line) is not None:
        # Add each HTML entity to the identifiedMatches list
        for match in re.findall(entityRegex, line):
            identifiedMatches.append({"name" : "entityRegex", "value" : match})

# Dictionary for later displaying the amount of matches of each kind
frequencyMatch = {}

# For each match in the identified matches...
for match in identifiedMatches:
    # Add to dictionary
    valueOfMatch = match["value"]
    if valueOfMatch not in frequencyMatch.keys():
        frequencyMatch[valueOfMatch] = 1
    else:
        frequencyMatch[valueOfMatch] = frequencyMatch[valueOfMatch] + 1

# Print the results in a nice format
for match in frequencyMatch:
    print(match + ": " + str(frequencyMatch[match]))