/
untitled0.py
88 lines (70 loc) · 2.35 KB
/
untitled0.py
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
# -*- coding: utf-8 -*-
"""
Created on Fri Jan 22 01:42:32 2021
@author: Aromal Pradeep
"""
'''
KRIC - Kric is a Random Intellegent Calculator
==============================================
# Project for Mec Labs
'''
# Basic
# Imports
import time as t
#import random as r
#import re
from word2number import w2n
from simpleeval import simple_eval
# NLTK
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
stop_words = set(stopwords.words('english'))
from nltk.tokenize import word_tokenize
from nltk.corpus import wordnet
# Spell Checker
#from spellchecker import SpellChecker
#spell = SpellChecker()
# Date and Time
def today():
return t.strftime("%d.%m.%y.%A.%H.%M").split('.')
# Main
while True:
# User input
i = input('user : ').lower()
if i.lower() in ['break','end','quit']:
break
else:
try:
#case 0 : a Number entered
try:
o = (w2n.word_to_num(i))
except:
try:
#case 1 : Normal calculations (basic calculator)
'''Easy way was to use eval, but using eval has great risks as it could be used to potentially crack your system'''
o = simple_eval(i)
except:
try:
# clean sentence
words = [k for k in word_tokenize(i) if k not in stop_words]
o = words
for i in words:
if i in ['plus','add','addition','positive','summation']:
i = '+'
elif i in ['minus','negative','subtraction','difference']:
i = '-'
elif i in ['x','multiply','product','times']:
i = '*'
elif i in ['divide','by']:
i = '/'
elif i in ['power']:
i = '**'
else:
pass
except:
#raise Exception()
pass
except: # if in case of errors
o = '<x_x> error...'
# Output
print('kric :',o)