-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaths_AI.py
More file actions
184 lines (161 loc) · 6.41 KB
/
Copy pathMaths_AI.py
File metadata and controls
184 lines (161 loc) · 6.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import re
import math
import nltk
from nltk.corpus import words
correct_spellings = words.words()
from nltk.metrics.distance import jaccard_distance
from nltk.util import ngrams
from nltk.metrics.distance import edit_distance
def extract_words(sentence):
global extr
global autoc
global low_word
extr = re.findall(r'\d+', sentence )
ignore_words = ['a', 'the', 'if', 'br', 'and', 'of', 'to', 'is', 'are', 'he', 'she', 'my', 'you', 'it','how', 'equals', 'equal', 'with', '=', 'find'] #Ignored words, add more words with , and ""
wordss = re.sub("[^\w]", " ", sentence).split() # this replaces all special chars with ' '
low_word = [w.lower() for w in wordss if w not in ignore_words]
pattern = '[0-9]'
low_word = [re.sub(pattern, '', i) for i in low_word]
while("" in low_word) :
low_word.remove("")
autoc = []
for entry in low_word:
temp = [(edit_distance(entry, w),w) for w in correct_spellings if w[0]==entry[0]]
autoc.append(sorted(temp, key = lambda val:val[0])[0][1])
return wordss
# extr is the list that contains the numbers
# autoc is the list that contains the autocorrected sentence
# low_word contains the non - autocorrected words but it has the SI unit values
test_sentence = input("Enter your questions that relates to perimeter / area: ")
extract_words(test_sentence)
length = 0
breadth = 0
height = 0
peri_meter = 0
unit = 0
units = 0
area = 0
if "cm" or "centimeter" or "centimeters" in low_word:
unit ="cm"
units ="cm sq."
elif "mm" or "millimeter" or "millimeters" in low_word:
unit = "mm"
units ="mm sq."
elif "dm" or "decimeter" or "decimeters" in low_word:
unit = "dm"
units ="dm sq."
elif "m" or "meter" or "meters" in low_word:
unit = "m"
units ="m sq."
elif "dam" or "dekameter" or "dekameters" in low_word:
unit = "dam"
units ="dam sq."
elif "hm" or "hectometer" or "hectometers" in low_word:
unit = "hm"
units ="hm sq."
elif "km" or "kilometer" or "kilometers" in low_word:
unit = "km"
units ="km sq."
else:
unit = "cm"
units ="cm sq."
if "perimeter" in autoc:
print("Perimeter is found in the string.")
if "square" in autoc:
if len(extr) == 2:
length = float(extr[0])
breadth = float(extr[1])
peri_meter = 2*(length + breadth)
print("Perimeter of the given square is: ", peri_meter, "", unit )
elif len(extr) == 1:
length = float(extr[0])
peri_meter = 4*(length)
print("Perimeter of the given square is: ", peri_meter, "", unit )
else:
print("Input maximum of two sides.")
elif "rectangle" in autoc:
if len(extr) == 2:
length = float(extr[0])
breadth = float(extr[1])
peri_meter = 2*(length + breadth)
print("Perimeter of the given rectangle is: ", peri_meter, "", unit )
elif len(extr) == 1:
length = float(extr[0])
peri_meter = 4*(length)
print("Perimeter of the given rectangle is: ", peri_meter, "", unit )
else:
print("Input maximum of two sides.")
elif "triangle" in autoc:
if len(extr) == 3:
length = float(extr[0])
breadth = float(extr[1])
height = float(extr[2])
peri_meter = length + breadth + height
print("Perimeter of the given triangle is: ", peri_meter, "", unit )
elif len(extr) == 2:
print("You have entered only two sides in a triangle, so it is being taken as a right angled triangle with both parameters as sides")
length = float(extr[0])
breadth = float(extr[1])
height = math.hypot(length, breadth)
peri_meter = length + breadth + height
print("Perimeter of the given right angled triangle is: ", peri_meter, "", unit )
else:
print("Input minimum of two sides.")
elif "circumference" in autoc:
print("Circumference is found in the string.")
if "radius" in autoc:
length = float(extr[0])
elif "diameter" in autoc:
length = float(extr[0]) / 2
peri_meter = 2 * math.pi * length
print("Circumference of the given circle is: ", peri_meter, "", unit )
elif "area" in autoc:
print("Area is found in the string.")
if "square" in autoc:
if len(extr) == 2:
length = float(extr[0])
breadth = float(extr[1])
area = length * breadth
print("Area of the given square is: ", area, "", units )
elif len(extr) == 1:
length = float(extr[0])
area = length * length
print("Area of the given square is: ", area, "", units )
else:
print("Input maximum of two sides.")
elif "rectangle" in autoc:
if len(extr) == 2:
length = float(extr[0])
breadth = float(extr[1])
area = length * breadth
print("Area of the given rectangle is: ", area, "", units )
elif len(extr) == 1:
length = float(extr[0])
area = length * length
print("Area of the given rectangle is: ", area, "", units )
else:
print("Input maximum of two sides.")
elif "triangle" in autoc:
if len(extr) == 3:
length = float(extr[0])
breadth = float(extr[1])
height = float(extr[2])
peri_meter = (length + breadth + height) / 2
area = (peri_meter*(peri_meter - length)*(peri_meter - breadth)*(peri_meter - height)) ** 0.5
print("Area of the given triangle is: ", area, "", units )
elif len(extr) == 2:
length = float(extr[0])
breadth = float(extr[1])
area = 1/2 * length * breadth
print("Area of the given triangle is: ", area, "", units )
else:
print("Input minimum of two sides.")
elif "circle" or "radius" or "diameter" in autoc:
if "radius" in autoc:
length = float(extr[0])
elif "diameter" in autoc:
length = float(extr[0]) / 2
area = length * math.pi * length
print("Area of the given circle is: ", area, "", units )
else:
print("Try something different or try wording it better.")