Exported from Notepad++
1 import setup, calculations
2
3 # Author: Mostapha Abdelaziz
4 # Program to predict an items classification based on predefined data
5
6 # ask for item classification name
7 classname = input("Enter name of the item classification (e.g vehicle type, player position)\n > ")
8
9 # Call setup functions to define data set being used
10
11 # how many attributes each label/class uses
12 numofat = setup.enter_attribute_count(classname)
13
14 # ask for attribute names
15 allnames = setup.enter_names(numofat)
16
17 #ask for how many classes there are
18 numofclass = setup.enter_class_count()
19
20 # ask for each class name
21 classes = setup.enter_class_names(classname, numofclass)
22
23 # populate dataset that calculations will be based on
24 i = 0
25 j = 1
26 allatts = []
27 attributes = []
28 # loop through each class
29 while i < numofclass:
30 singleatts = []
31 print(f"\nEnter {classes[i]} {j} attributes")
32 # loop through and get each attribute
33 for att in allnames:
34 num = setup.enter_attribute(classes[i], att)
35
36 #store the attribute
37 singleatts.append(num)
38
39 # append to all attributes
40 allatts.append(singleatts)
41
42 # ask if they want to add another of the same class
43 userinput = 't'
44 while userinput not in ("y","Y","n","N"):
45 userinput = input(f"Add another {classes[i]}? \"y\" for yes or \"n\" for no\n > ")
46
47 # if they do, pass otherwise increment i to go to the next class
48 if userinput in ("y","Y"):
49 pass
50 j += 1
51 else:
52 attributes.append(allatts)
53 allatts = []
54 i += 1
55 j = 1
56
57
58 # call functions to perform calculations
59 averages = calculations.calculate_averages(attributes, numofat)
60 weights = calculations.calculate_weight(averages, numofclass, numofat)
61
62
63 # loop through menu of program
64 menuoption = 't'
65 # loop until they quit
66 while menuoption not in '3':
67 menuoption = 't'
68 # validate their input
69 while menuoption not in ("1", "2", "3"):
70 menuoption = input("1. Guess a classification\n2. Enter new data\n3. Quit program\n > ")
71
72 # implement menu options
73 if menuoption == '1':
74 # guess based on data, take in attributes
75 singleatts = []
76 print(f"\nEnter {classname}'s attributes")
77 # loop through and get each attribute
78 for att in allnames:
79 num = setup.enter_attribute(classname, att)
80
81 #store the attribute
82 singleatts.append(num)
83
84 # get the scores
85 scores = calculations.score_data(weights, averages, singleatts)
86
87 # print the most likely one
88 print(f"This is a {classes[scores.index(max(scores))]}\n")
89
90 # print the verdict
91 elif menuoption == '2':
92 # add more data, ask which class first
93 userinput = 't'
94 # ensure it is one of the defined classes
95 while userinput not in classes:
96 userinput = input(f"Which {classname} would you like to add to?\n > ")
97 if userinput not in classes:
98 print(f"Enter one of your predefined classes {classes}. Try again.")
99
100 # add more data for the selected class
101 index = classes.index(userinput)
102 singleatts = []
103 print(f"\nEnter new {classes[index]} attributes")
104 # loop through and get each attribute
105 for att in allnames:
106 num = setup.enter_attribute(classes[index], att)
107
108 #store the attribute
109 singleatts.append(num)
110
111 #store new attributes with the rest of the data
112 attributes[index].append(singleatts)
113
114 # perform calculations again
115 averages = calculations.calculate_averages(attributes, numofat)
116 weights = calculations.calculate_weight(averages, numofclass, numofat)
117
118 elif menuoption == '3':
119 print ("Good Bye.\n")
120