Skip to content

Instantly share code, notes, and snippets.

@CorellanStoma
Forked from Davr1/classfinder.py
Created January 9, 2022 16:20
Show Gist options
  • Select an option

  • Save CorellanStoma/f49f5ea8557e539f20a38a48e9f235e0 to your computer and use it in GitHub Desktop.

Select an option

Save CorellanStoma/f49f5ea8557e539f20a38a48e9f235e0 to your computer and use it in GitHub Desktop.

Revisions

  1. @Davr1 Davr1 revised this gist Jan 9, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion classfinder.py
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@ def best_similarity(master, candidates, originalcandidates):
    # returns a map
    return max(candidate_scores, key=itemgetter(0))[1]

    # sorts a list alphabetically and adds padding so it can be used in the best_similarity function
    # sorts a list alphabetically and adds padding (empty strings) so it can be used in the best_similarity function
    def list_sort(list, padding):
    return sorted(list) + [''] * (padding - len(list))

  2. @Davr1 Davr1 revised this gist Jan 9, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion classfinder.py
    Original file line number Diff line number Diff line change
    @@ -42,7 +42,7 @@ def main():
    else:
    try:
    # splits elements with multiple classes into multiple lines
    for substring in zip(match[element].split(' '), element_map[element].split(' ')):
    for substring in zip(element_map[element].split(' '), match[element].split(' ')):
    output.write(f"{substring[0]} = {substring[1]}\n")
    print(f"Found {element_map[element]}")
    except:
  3. @Davr1 Davr1 created this gist Jan 9, 2022.
    54 changes: 54 additions & 0 deletions classfinder.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    import json
    import re
    from operator import itemgetter

    # finds the most similar list
    # master = [] with n items
    # candidates = [[],[],[],...] each nested list with n items
    # original_candidates = [{}, {}, {},...]
    def best_similarity(master, candidates, originalcandidates):
    counts = [0] * len(candidates)
    for i, masterval in enumerate(master):
    for ci, c in enumerate(candidates):
    counts[ci] += masterval == c[i] #True is numerically 1
    candidate_scores = zip(counts, originalcandidates)
    # returns a map
    return max(candidate_scores, key=itemgetter(0))[1]

    # sorts a list alphabetically and adds padding so it can be used in the best_similarity function
    def list_sort(list, padding):
    return sorted(list) + [''] * (padding - len(list))

    def main():

    old = json.loads(open("old_classes.json").read())
    new = json.loads(open("new_classes.json").read())
    output = open("output.md", "w+")

    # turns the new element map into a nested list
    new_as_list = [list_sort(list(new_element_map), 100) for new_element_map in new]

    for element_map in old:

    element_list = list_sort(list(element_map.keys()), 100)
    match = best_similarity(element_list, new_as_list, new)

    for element in list(element_map.keys()):

    # checks if element contains a number as the first character, or a parenthesis
    # -> gets rid of elements such as "calc(...)" or "24px"
    if element in match and re.search(r"^\d|\(|\)", match[element]):
    print(f"Discarding {element}")
    else:
    try:
    # splits elements with multiple classes into multiple lines
    for substring in zip(match[element].split(' '), element_map[element].split(' ')):
    output.write(f"{substring[0]} = {substring[1]}\n")
    print(f"Found {element_map[element]}")
    except:
    print(f"Couldn't find {element}")

    print("Finished")
    output.close()

    main()