-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnlp.py
858 lines (792 loc) · 36.1 KB
/
nlp.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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
import nltk
import re, math, json
nltk.download('punkt')
required_bits= 4
default_bits = 0
isSignednumber = False
userbits = 0
keyword_list = [
'write', 'one', 'two','ones',
'twos', 'binary',
"one's", "two's", 'compliment',
'convert', 'complement', 'represent',
'number', 'bits', 'value',
'decimal', '+', '-',
'sum', 'difference', 'required'
]
# retrieve common keywords.
def compare(query):
tokenize = nltk.word_tokenize(query)
return [x for x in tokenize if x in keyword_list]
def check_binary_format(string) :
p = set(string)
s = {'0', '1'} # declare set of '0', '1' .
# case 1: string has both 1s and 0s
# case 2: string only has 1s
# case 3: string only has 0s
if s == p or p == {'0'} or p == {'1'}:
return "yes"
else:
return "no"
def binary_to_decimal(binary_numbers, decimal_numbers =None):
if len(binary_numbers) == 1:
steps = 'Starting from LSB, take sum of (2*i)^x where i = bit value and x = bit position.<br />'
steps = steps + ' For this example, <br /><b>' + binary_numbers[0] + ' = '
sum = 0
position = len(binary_numbers[0])
for i in range(0, len(binary_numbers[0]) ):
sum = sum + (2 * int(binary_numbers[0][i], base = 2) ** position)
position = position - 1
steps = steps + '(2*' + binary_numbers[0][i] + ')^' + str(position) + ' + '
steps = steps[0: len(steps) - 2]
steps = steps + ' = ' + str(int(binary_numbers[0], base = 2))
steps = steps + '</b><br/>'
return [str(int(binary_numbers[0], base = 2)), steps[:len(steps) - 3]]
elif len(decimal_numbers) == 1:
return decimal_to_binary(decimal_numbers)
else:
return ['Error! 1 argument required, given 0 or more than 1', ""]
def signed_ones_complement(decimal_numbers, all_numbers):
global required_bits
i = all_numbers[0]
result = []
answer = ""
steps = "For Negative number: First creates the number to its . Then takes one's compliment.<br/>"
if len(all_numbers) == 1:
if(all_numbers[0][1] == "int"):
steps = steps + 'To convert decimal number into binary, divide the number by 2 repeatedly until<br />'
steps = steps + 'remainder becomes smaller than 2. Then read all the carry in backward(bottom to top) direction.<br />'
steps = steps + ' For this example, <br /> <b>'
num = all_numbers[0][0]
if(num > 0):
count = 0
carry = 0
bits = math.ceil(math.log(int(num), 2))
while (count < bits):
steps = steps + "Iteration # " + str(count) + ": " + "remainder = " + str(int(num/2))
carry = num % 2
num = num / 2
steps = steps + ', carry = ' + str(int(carry)) + '<br />'
count = count + 1
steps = steps + "<b/> <br/>"
answer = str(bin(all_numbers[0][0]).replace("0b", ""))
else:
answer = str(all_numbers[0][0])
anslen = len(answer) + 1
r_bit = anslen
if(anslen < required_bits):
r_bit = required_bits
answer = answer.zfill(required_bits)
else:
answer = answer.zfill(anslen)
steps = steps + "Now take twos compliment of " + answer + "<br/>"
result.append(getBinTwosComplement(answer,r_bit))
steps = steps + result[0][1]
steps = steps + answer + " ==> " + result[0][0]
steps = steps + "<br/>Now take one's complement for final answer: "
answer = result[0][0]
a = answer
temp = ""
for i in range(0, len(a)):
if a[i] == '0':
temp = temp + '1'
else:
temp = temp + '0'
steps = steps + "<br/> invert every bit of the given bit string i.e change 0 to 1 and 1 to 0 : <br />" + a + " ==> " + temp
if(all_numbers[0][1] == "int"):
steps = steps + "<br/> Convert decimal to binary"
answer = temp
ans = answer
answer=0
steps = steps + ""
next_step = "= "
if(ans[0]=='1'):
answer = int(math.pow(2, len(ans)-1)) * -1
steps = steps +" "+ ans + "= - ( 2^" + str(len(ans)-1) + " * " + ans[0]+" ) "
next_step = next_step + "- " + str(int(math.pow(2, len(ans)-1)))
else:
steps = steps + "- ( 2^" + str(len(ans)-1) + " * " + ans[0]+" ) "
next_step = next_step + "- 0"
index= len(ans) - 1
index1= 1
while (index > 1):
steps = steps + "+ ( 2^" + str(index-1) + " * " + ans[index1]+" ) "
if(ans[index1]=="1"):
answer = answer + int(math.pow(2, (index-1)) )
next_step = next_step + " + " + str(int(math.pow(2, (index-1) )))
else:
next_step = next_step + " + 0"
index1 = index1 + 1
index = index - 1
steps = steps + "+ ( 2^0" + " * " + ans[len(ans)-1]+" )<br/>"
if(ans[len(ans)-1] == "1"):
answer = answer + 1
next_step = next_step + " + 1<br/>"
else:
next_step = next_step + " + 0<br/>"
next_step = next_step + " = " + str(answer) + "<br/>"
steps = steps + next_step
final_ans = "Base 2 ( " + ans + " ), Base 10 ( " + str(answer) + ")"
else:
final_ans = temp
else:
return ["Error! 1 argument required, given 0 or more than 1", ""]
return [final_ans, steps]
def signed_twos_complement(decimal_numbers, all_numbers):
global required_bits
i = all_numbers[0]
result = []
answer = ""
steps = "*Here is a video showing how to get two's complement of a negative decimal number. You can use this to understand how the calculatoin below was performed, and answer generated.<a href='https://youtu.be/yFiG8H4Ekoc'>https://youtu.be/yFiG8H4Ekoc</a><br/>For Negative number: First convert the number to its binary and than take its two's compliment. And the takes its two's complement again to get the required answer. <br/>"
if len(all_numbers) == 1:
if(all_numbers[0][1] == "int"):
steps = steps + 'To convert decimal number into binary, divide the number by 2 repeatedly until<br />'
steps = steps + 'remainder becomes smaller than 2. Then read all the carry in backward(bottom to top) direction.<br />'
steps = steps + ' For this example, <br /> <b>'
num = all_numbers[0][0]
if(num > 0):
count = 0
carry = 0
bits = math.ceil(math.log(int(num), 2))
while (count < bits):
steps = steps + "Iteration # " + str(count) + ": " + "remainder = " + str(int(num/2))
carry = num % 2
num = num / 2
steps = steps + ', carry = ' + str(int(carry)) + '<br />'
count = count + 1
steps = steps + "<b/> <br/>"
answer = str(bin(all_numbers[0][0]).replace("0b", ""))
anslen = len(answer) + 1
r_bit = anslen
if(anslen < required_bits):
r_bit = required_bits
answer = answer.zfill(required_bits)
else:
answer = answer.zfill(anslen)
else:
answer = all_numbers[0][0]
anslen = len(answer) + 1
r_bit = anslen
if(anslen < required_bits):
r_bit = required_bits
answer = answer.zfill(required_bits)
else:
answer = answer.zfill(anslen)
result.append(getBinTwosComplement(answer,r_bit))
steps = steps + result[0][1]
result[0][0] = result[0][0][1:]
steps = steps + "Now take twos compliment of " + answer + " ==> " + result[0][0] +"<br/>"
r_bit = len(result[0][0])
steps = steps + answer + " ==> " + result[0][0]
steps = steps + "<br/>Now take two's complement for final answer: <br/>"
steps = steps + "apply one's compliment to binary string first and then add 1 to LSB (Least Significant Bit)<br />"
a = result[0][0]
reqBit = len(a)
temp = ""
for i in range(0, len(a)):
if a[i] == '0':
temp = temp + '1'
else:
temp = temp + '0'
answer = str(bin(int(temp, base = 2) + 1).replace('0b', '')).zfill(r_bit)
if(len(answer) > reqBit):
answer = answer[1:]
if(all_numbers[0][1] == "int"):
steps = steps + "<br/> Convert decimal to binary<br/>"
ans = answer
steps = steps + ""
next_step = "= "
answer = 0
if(ans[0]=='1'):
answer = int(math.pow(2, len(ans)-1)) * -1
steps = steps +" "+ ans + "= - ( 2^" + str(len(ans)-1) + " * " + ans[0]+" ) "
next_step = next_step + "- " + str(int(math.pow(2, len(ans)-1)))
else:
steps = steps + "- ( 2^" + str(len(ans)-1) + " * " + ans[0]+" ) "
next_step = next_step + "- 0"
index= len(ans) - 1
index1= 1
while (index > 1):
steps = steps + "+ ( 2^" + str(index-1) + " * " + ans[index1]+" ) "
if(ans[index1]=="1"):
answer = answer + int(math.pow(2, (index-1)) )
next_step = next_step + " + " + str(int(math.pow(2, (index-1) )))
else:
next_step = next_step + " + 0"
index1 = index1 + 1
index = index - 1
steps = steps + "+ ( 2^0" + " * " + ans[len(ans)-1]+" )<br/>"
if(ans[len(ans)-1] == "1"):
answer = answer + 1
next_step = next_step + " + 1<br/>"
else:
next_step = next_step + " + 0<br/>"
next_step = next_step + " = " + str(answer) + "<br/>"
steps = steps + next_step
final_ans = "Base 2 ( " + ans + " ), Base 10 ( " + str(answer) + ")"
else:
final_ans = answer
else:
return ["Error! 1 argument required, given 0 or more than 1", ""]
return [final_ans, steps]
def decimal_to_binary(decimal_numbers):
if len(decimal_numbers) == 1:
steps = 'To convert decimal number into binary, divide the number by 2 repeatedly until<br />'
steps = steps + 'remainder becomes smaller than 2. Then read all the carry in backward(bottom to top) direction.<br />'
steps = steps + ' For this example, <br /> <b>'
num = decimal_numbers[0]
if(num > 0):
count = 0
carry = 0
bits = math.ceil(math.log(int(num), 2))
while (count < bits):
steps = steps + "Iteration # " + str(count) + ": " + "remainder = " + str(int(num/2))
carry = num % 2
num = num / 2
steps = steps + ', carry = ' + str(int(carry)) + '<br />'
count = count + 1
steps = steps + "<b/><br/>" + str(decimal_numbers[0]) + " ==> " + str(bin(decimal_numbers[0]).replace("0b", ""))
else:
steps = "<b> its a signed number.</b>"
answer = str(bin(decimal_numbers[0]).replace("0b", ""))
return [answer, steps]
else:
return ["Error! 1 argument required, given 0 or more than 1", ""]
def signed_decimal_to_binary(decimal_numbers, all_numbers):
global required_bits
result = []
steps = "For Negative number: First convert the number to its binary and than take its two's compliment.<br/>"
if len(decimal_numbers) == 1:
steps = steps + 'To convert decimal number into binary, divide the number by 2 repeatedly until<br />'
steps = steps + 'remainder becomes smaller than 2. Then read all the carry in backward(bottom to top) direction.<br />'
steps = steps + ' For this example, <br /> <b>'
num = decimal_numbers[0]
if(num > 0):
count = 0
carry = 0
bits = math.ceil(math.log(int(num), 2))
while (count < bits):
steps = steps + "Iteration # " + str(count) + ": " + "remainder = " + str(int(num/2))
carry = num % 2
num = num / 2
steps = steps + ', carry = ' + str(int(carry)) + '<br />'
count = count + 1
steps = steps + "<b/> <br/>"
answer = str(bin(decimal_numbers[0]).replace("0b", ""))
anslen = len(answer) + 1
r_bit = anslen
if(anslen < required_bits):
r_bit = required_bits
answer = answer.zfill(required_bits)
else:
answer = answer.zfill(anslen)
steps = steps + answer + "<br>(why the extra zero 0? In order, to get the correct bit representation for this number in binary. We hare to add an zero 0, thus extending the number)<br>"
steps = steps + "Now take twos compliment of " + answer + "<br>"
result.append(getBinTwosComplement(answer,r_bit))
steps = steps + answer + " ==> " + result[0][0]
steps = steps + "<br> *Here is a video showing how to convert negative decimal numbers into a two's complement binary number. You can use this to understand how these calculatoin were performed, and answer generated.<br><a href='https://youtu.be/yFiG8H4Ekoc'>https://youtu.be/yFiG8H4Ekoc</a>"
print(steps)
answer = result[0][0]
else:
return ["Error! 1 argument required, given 0 or more than 1", ""]
return [answer, steps]
def one_compliment(x, y = None):
global required_bits
if len(x) == 1:
a = x[0]
a = a.zfill(required_bits)
temp = ""
for i in range(0, len(a)):
if a[i] == '0':
temp = temp + '1'
else:
temp = temp + '0'
result = "invert every bit of the given bit string i.e change 0 to 1 and 1 to 0 : <br />" + a + " ==> " + temp
return [temp,result]
elif len(y) == 1:
temp = bin(int(y[0])).replace("0b", "")
temp = temp.zfill(required_bits)
temp2 = ""
for i in range(0, len(temp)):
if temp[i] == '0':
temp2 = temp2 + '1'
else:
temp2 = temp2 + '0'
result = decimal_to_binary(y)
steps = "First, convert decimal number into binary number.<br />"\
"To convert decimal number into binary, "
steps = steps + result[1]
steps = steps + "<br /> Then, invert every bit of the given bit string i.e change 0 to 1 and 1 to 0."
steps = steps + "<br /> Finally, convert the result back to decimal format. For this,"\
" follow the following steps: <br />"
steps = steps + binary_to_decimal([temp2])[1]
return ["Base 2: ( " + temp2 + " ) , Base 10: ( " + str(int("0b" + temp2, base = 2)) + ')', steps]
else:
return ["Error! 1 argumnent required, given 0.", ""]
def twos_compliment(binary_numbers, decimal_numbers = None):
if len(binary_numbers) == 1:
steps = "apply one's compliment to binary string first and then add 1 to LSB (Least Significant Bit)<br>"
compliment = one_compliment(binary_numbers)
steps = steps + compliment[1]
bitlen = compliment[0].__len__()
answer = str(bin(int(compliment[0], base = 2) + 1).replace('0b', ''))
answer = answer.zfill(bitlen)
answer = answer.zfill(required_bits)
steps = steps + "<br>add 1 to it:<br>" + compliment[0] + " ==> " + answer
return [answer , steps]
elif len(decimal_numbers) == 1:
temp = [bin(decimal_numbers[0]).replace("0b", "")]
temp[0] = '0' + temp[0]
compliment = one_compliment(temp)
sum = int("0b" + compliment[0], base = 2) + 1
result = decimal_to_binary([sum])
steps = "First, convert decimal number into binary number.<br />"\
"To convert decimal number into binary, "
steps = steps + result[1]
steps = steps + "<br /> Then, apply one's compliment to binary string and then add 1 to LSB (Least Significant Bit)"
steps = steps + "<br /> Finally, convert the result back to decimal format. For this,"\
" follow the following steps: <br />"
ans = result[0]
next_step = "= "
answer = 0
if(ans[0]=='1'):
answer = int(math.pow(2, len(ans)-1)) * -1
steps = steps +" "+ ans + "= - ( 2^" + str(len(ans)-1) + " * " + ans[0]+" ) "
next_step = next_step + "- " + str(int(math.pow(2, len(ans)-1)))
else:
steps = steps + "- ( 2^" + str(len(ans)-1) + " * " + ans[0]+" ) "
next_step = next_step + "- 0"
index= len(ans) - 1
index1= 1
while (index > 1):
steps = steps + "+ ( 2^" + str(index-1) + " * " + ans[index1]+" ) "
if(ans[index1]=="1"):
answer = answer + int(math.pow(2, (index-1)) )
next_step = next_step + " + " + str(int(math.pow(2, (index-1) )))
else:
next_step = next_step + " + 0"
index1 = index1 + 1
index = index - 1
steps = steps + "+ ( 2^0" + " * " + ans[len(ans)-1]+" )<br/>"
if(ans[len(ans)-1] == "1"):
answer = answer + 1
next_step = next_step + " + 1<br/>"
else:
next_step = next_step + " + 0<br/>"
next_step = next_step + " = " + str(answer) + "<br/>"
steps = steps + next_step
final_ans = "Base 2 ( " + ans + " ), Base 10 ( " + str(answer) + ")"
#steps = steps + binary_to_decimal([result[0]])[1]
#result[0] = result[0].zfill(required_bits)
#dec_answer = "Base 2 ( " + result[0] + " ) , Base 10 ( " +str(sum) + ' )'
return [final_ans, steps]
else:
return ["Error! 1 argumnent required, given 0.", ""]
def bit_representation(decimal_numbers,all_numbers):
if len(decimal_numbers) == 1:
steps = "Take log base 2 of the given binary string i.e log<sub>2</sub>(" + str(decimal_numbers[0]) + " + 1) = " + str(math.log(decimal_numbers[0]+1, 2))
steps = steps + "<br />take ceiling of the previous result like this: ⌈" + str(math.log(decimal_numbers[0] + 1, 2) ) + "⌉ = " + str(math.ceil(math.log(decimal_numbers[0] + 1, 2 )))
answer = str(math.ceil(math.log(decimal_numbers[0] + 1, 2)))
if(all_numbers[0][2] == "-"):
steps = steps + "<br/>Since its a negative number: there is atleast 1 extra bit needed to represent it. So answer: " + str(math.ceil(math.log(decimal_numbers[0] + 1, 2)) + 1)
answer = str(int(answer) + 1)
steps = steps + "<br /> <b>Note: The answer has to be a integer, so we round up to the nearest biggest integer.</b>"
return [answer, steps]
else:
return ['Error! 1 arg required, given 0', ""]
def getBinTwosComplement(num, reqBit):
steps = "apply one's compliment to binary string first and then add 1 to LSB (Least Significant Bit)<br />"
num = num.zfill(reqBit)
if(userbits > num.__len__()+1):
num = num.zfill(userbits)
else:
num = num.zfill(num.__len__()+1)
a = num
temp = ""
for i in range(0, len(a)):
if a[i] == '0':
temp = temp + '1'
else:
temp = temp + '0'
bitlen = temp.__len__()
answer = str(bin(int(temp, base = 2) + 1).replace('0b', ''))
if(len(answer) > reqBit):
answer = answer[1:]
return [answer , steps ,"bin"]
def getIntTwosComplement(num, reqBit):
global userbits
temp = bin(num).replace("0b", "")
temp=temp.zfill(reqBit)
a = temp
temp = ""
for i in range(0, len(a)):
if a[i] == '0':
temp = temp + '1'
else:
temp = temp + '0'
sum = int("0b" + temp, base = 2) + 1
result = decimal_to_binary([sum])
binary_res = result[0]
if(len(binary_res)> reqBit):
binary_res = binary_res[1:]
if(userbits > binary_res.__len__()+1):
binary_res = binary_res.zfill(userbits)
steps = "First, convert decimal number into binary number.<br />"\
"To convert decimal number into binary, "
steps = steps + result[1]
steps = steps + "<br /> Then, apply one's compliment to binary string and then add 1 to LSB (Least Significant Bit) to get its negative representation i.e. " + binary_res
steps = steps + binary_to_decimal([binary_res])[1]
return [binary_res, steps,"int", str(sum)]
def getDecimal(num,isSigned):
numLen = num.__len__()
decimal_rep=""
if(isSigned):
if(num[0] == "0"):
decimal_rep = str(int(num, base = 2))
else:
num1 = ""
num1 = num1.zfill(numLen)
num1 = '1' + num1[1:]
dec1 = int(num1, base = 2) * -1
num = '0' + num[1:]
dec = int(num, base = 2)
decimal_rep = str(dec1 + dec)
else:
decimal_rep = str(int(num, base = 2))
return decimal_rep
def binary_add_sub(op_type,all_numbers,binary_numbers,decimal_numbers = None):
if(all_numbers.__len__() < 2):
return ["Error: Not enough operands" ,""]
isDoubleNeg = False
global userbits
global isSignednumber
two_comp = 0
isSignednumber = False
result = []
explanation= "</br><br/>"
if(op_type == "subtraction"):
if(all_numbers.__len__() > 1):
if(all_numbers[1][2]=="-"):
isDoubleNeg = True
op_type = "addition"
all_numbers[1][2] = "+"
else:
op_type = "addition"
all_numbers[1][2] = "-"
if(all_numbers[1][2]=="-" or all_numbers[0][2]=="-"):
explanation = explanation + "Here is a video showing how to subtract binary numbers. You can use this to understand how the calculation below was performed, and answer generated.<br/><a href='https://www.youtube.com/watch?v=S9LJknZTyos'>https://www.youtube.com/watch?v=S9LJknZTyos</a><br/>"
isSignednumber = True
else:
explanation = explanation + "*Here is a video showing how to add binary numbers. You can use this to understand how the calculatoin below was performed, and answer generated.<br/><a href='https://www.youtube.com/watch?v=jB_sRh5yoZk'>https://www.youtube.com/watch?v=jB_sRh5yoZk</a><br/>"\
"*If a column has all zeros (0), you drop down a zero (0)<br/>"\
"*If a column has a one (1) and a zero (0), you drop down a one (1)<br/>"\
"*If a column has two ones, (1), you carry a one (1) and down a zero (0)<br/>"\
"*If a column has three ones, (1), you carry a one (1) and down a one (1)"
binaries=[]
for i in all_numbers:
if(i[2] == "-"):
if(i[1] == "int"):
bin_rep = decimal_to_binary([i[0]])
r_bit=bin_rep[0].__len__()+1
if(userbits > r_bit):
r_bit = userbits
i.append(r_bit)
binaries.append(bin_rep[0])
else:
r_bit=i[0].__len__()+1
if(userbits > r_bit):
r_bit = userbits
i.append(r_bit)
binaries.append(i[0])
else:
if(i[1] == "int"):
bin_rep = decimal_to_binary([i[0]])
r_bit=bin_rep[0].__len__()
if(userbits > r_bit):
r_bit = userbits
i.append(r_bit)
binaries.append(bin_rep[0])
else:
r_bit=i[0].__len__()
if(userbits > r_bit):
r_bit = userbits
i.append(r_bit)
binaries.append(i[0])
if(all_numbers[0][3] < all_numbers[1][3]):
all_numbers[0][3] = all_numbers[1][3]
else:
all_numbers[1][3] = all_numbers[0][3]
for i in all_numbers:
if(i[2] == "-"):
two_comp = two_comp + 1
if(i[1] == "int"):
result.append(getIntTwosComplement(i[0],i[3]))
else:
result.append(getBinTwosComplement(i[0],i[3]))
else:
if(i[1] == "int"):
bin_rep = decimal_to_binary([i[0]])
bin_rep[0] = bin_rep[0].zfill(i[3])
result.append([bin_rep[0], bin_rep[1],"int", str(i[1])])
else:
i[0] = i[0].zfill(i[3])
result.append([i[0] , "" ,"bin"])
op0 = result[0][0]
op1 = result[1][0]
op0len = op0.__len__()
op1len = op1.__len__()
if(op0len > op1len):
op1 = op1.zfill(op0len)
else:
op0 = op0.zfill(op1len)
ans = bin(int(op0, 2) + int(op1, 2)).replace("0b", '')
op0len = op0.__len__()
op1len = op1.__len__()
anslen = ans.__len__()
ans = ans.zfill(op1len)
isCarry = False
isDecimal = False
dec_value = ""
if(isSignednumber):
if(anslen > op1len):
isCarry = True
final_ans= ""
if(all_numbers[0][1] =="int" or all_numbers[1][1]=="int"):
isDecimal = True
steps=""
if(all_numbers[0][1]=="int"):
if(all_numbers[0][2] == "-"):
steps = steps + "Operand 1 : Convert to binary: -" + str(all_numbers[0][0]) + " ==> -" + str(binaries[0]) + "<br/>"
else:
steps = steps + "Operand 1 : Convert to binary: " + str(all_numbers[0][0]) + " ==> " + str(binaries[0]) + "<br/>"
if(all_numbers[0][2] =="-"):
steps = steps + "Take two's compliment of negative number to get his binary representation: <br/> "
steps = steps + str(binaries[0]).zfill(len(result[0][0])) + " ==> " + result[0][0] + "<br/>"
else:
if(all_numbers[0][2] == "-"):
steps = steps + "Operand 1 : Base 2( -" + str(all_numbers[0][0]) + " )<br/>"
else:
steps = steps + "Operand 1 : Base 2( " + str(all_numbers[0][0]) + " )<br/>"
if(isDoubleNeg):
steps = steps + "Double negation results in addition i.e. - -" + str(all_numbers[1][0]) + " ==> + " + str(all_numbers[1][0]) + "<br/>"
if(all_numbers[1][2] == "-"):
steps = steps + "so it is: " + str(all_numbers[0][2]) + str(all_numbers[0][0]) + " + " + str(all_numbers[1][0]) + "<br/>"
else:
steps = steps + "so it is: " + str(all_numbers[0][0]) + " + " + str(all_numbers[1][0]) + "<br/>"
if(all_numbers[1][1]=="int"):
if(all_numbers[1][2] == "-"):
steps = steps + "Operand 2 : Convert to binary: -" + str(all_numbers[1][0]) + " ==> -" + str(binaries[1]) + "<br/>"
else:
steps = steps + "Operand 2 : Convert to binary: " + str(all_numbers[1][0]) + " ==> " + str(binaries[1]) + "<br/>"
if(all_numbers[1][2] =="-"):
steps = steps + "Take two's compliment of negative number to get his binary representation: <br/> "
steps = steps + str(binaries[1]).zfill(len(result[1][0])) + " ==> " + result[1][0] + "<br/>"
else:
if(all_numbers[1][2] == "-"):
steps = steps + "Operand 2 : Base 2( -" + str(all_numbers[1][0]) + " )<br/>"
else:
steps = steps + "Operand 2 : Base 2( " + str(all_numbers[1][0]) + " )<br/>"
steps = steps + "Adding numbers: <br/>"
if(isSignednumber):
steps = steps + "0" + str(op0) + "<br/>"
steps = steps + "0" + str(op1) + "<br/>"
steps = steps + str(ans).zfill(len(op0)+1) + "<br/>"
ans = str(ans).zfill(len(op0)+1)
temp_ans = ans[1:]
carry = ans[0]
if (two_comp == 1):
steps = steps + "As there was only one negative operand, we are discarding the carry."
ans = temp_ans.zfill(len(op0)+1)
answer = 0
steps = steps + explanation
prev_steps = steps
steps = "<br/>Lastly convert the answer to its decimal value using, take sum of (2*i)^x where i = bit value and x = bit position: <br/>"
next_step = "= "
if(ans[0]=='1'):
if (two_comp == 1):
if(carry == '0'):
answer = int(math.pow(2, len(ans)-1))
steps = steps +" "+ ans + "= ( 2^" + str(len(ans)-1) + " * " + ans[0]+" ) "
next_step = next_step + str(int(math.pow(2, len(ans)-1)))
else:
answer = int(math.pow(2, len(ans)-1)) * -1
steps = steps +" "+ ans + "= - ( 2^" + str(len(ans)-1) + " * " + ans[0]+" ) "
next_step = next_step + "- " + str(int(math.pow(2, len(ans)-1)))
else:
answer = int(math.pow(2, len(ans)-1)) * -1
steps = steps +" "+ ans + "= - ( 2^" + str(len(ans)-1) + " * " + ans[0]+" ) "
next_step = next_step + "- " + str(int(math.pow(2, len(ans)-1)))
else:
steps = steps + "- ( 2^" + str(len(ans)-1) + " * " + ans[0]+" ) "
next_step = next_step + "- 0"
index= len(ans) - 1
index1= 1
while (index > 1):
steps = steps + "+ ( 2^" + str(index-1) + " * " + ans[index1]+" ) "
if(ans[index1]=="1"):
answer = answer + int(math.pow(2, (index-1)) )
next_step = next_step + " + " + str(int(math.pow(2, (index-1) )))
else:
next_step = next_step + " + 0"
index1 = index1 + 1
index = index - 1
steps = steps + "+ ( 2^0" + " * " + ans[len(ans)-1]+" )<br/>"
if(ans[len(ans)-1] == "1"):
answer = answer + 1
next_step = next_step + " + 1<br/>"
else:
next_step = next_step + " + 0<br/>"
next_step = next_step + " = " + str(answer) + "<br/>"
steps = steps + next_step
else:
op0len = len(op0)
op1len = len(op1)
anslen = len(ans)
maxlen = op0len
if(maxlen<op1len):
maxlen = op0len
elif(maxlen<anslen):
maxlen = anslen
op0=op0.zfill(maxlen)
op1=op1.zfill(maxlen)
ans=ans.zfill(maxlen)
steps = steps + str(op0) + "<br/>"
steps = steps + str(op1) + "<br/>"
steps = steps + str(ans) + "<br/>"
answer = 0
steps = steps + explanation
prev_steps = steps
steps = "Lastly convert the answer to its decimal value using, take sum of (2*i)^x where i = bit value and x = bit position:<br/> = "
next_step = "= "
index= len(ans)
index1= 0
while (index > 1):
steps = steps + "( 2^" + str(index-1) + " * " + ans[index1]+" ) + "
if(ans[index1]=="1"):
answer = answer + int(math.pow(2, (index-1)) )
next_step = next_step + " " + str(int(math.pow(2, (index-1) ))) + " + "
else:
next_step = next_step + " 0 + "
index1 = index1 + 1
index = index - 1
steps = steps + "( 2^0" + " * " + ans[len(ans)-1]+" ) <br/>"
if(ans[len(ans)-1] == "1"):
answer = answer + 1
next_step = next_step + " 1<br/>"
else:
next_step = next_step + " 0<br/>"
next_step = next_step + " = " + str(answer) + "<br/>"
steps = steps + next_step
if(isDecimal):
#dec_value = getDecimal(ans,isSignednumber)
prev_steps = prev_steps + steps
steps = prev_steps
final_ans = "Base 2 ( " + ans + "), Base 10 ( " + str(answer) + " )"
else:
steps = prev_steps
final_ans = ans
return [final_ans, steps]
def binary_module(query):
global required_bits
global userbits
global default_bits
global isSignednumber
required_bits= 4
default_bits = 0
isSignednumber = False
userbits = 0
isuserbit = False
tokenize = nltk.word_tokenize(query)
for word in tokenize:
bitindex = word.find("bit")
bitindex1 = word.find("bits")
if(bitindex > -1 and bitindex1 < 0):
if(bitindex > 0):
isuserbit = True
user_bit= int(word[:bitindex])
userbits= user_bit
if(user_bit > required_bits):
required_bits = user_bit
if(isuserbit == False):
required_bits=0
kwd = [x for x in tokenize if x in keyword_list] # get common keywords
binar_numbers = []
binar_signed =[]
decimal_numbers = []
decimal_signed =[]
all_numbers=[]
# isolate decimal and binary number arguments.
for i in tokenize:
x = i
isSigned = False
if i[0]== '-':
isSigned = True
if len(i) > 1:
i = i[1:]
if i[0]== '+':
if len(i)>1:
if len(i) > 1:
i = i[1:]
if re.match(r'[0-9]', i):
if check_binary_format(i) == "yes" and "base10" not in query and "base ten" not in query:
if(i.find("bit") < 0 ):
if ((len(i) < required_bits) and ('sum' not in kwd and '+' not in kwd and "difference" not in kwd and "-" not in kwd)):
i.zfill(required_bits)
binar_numbers.append(i)
binar_signed.append(isSigned)
if(isSigned):
all_numbers.append([i,"bin","-"])
else:
all_numbers.append([i,"bin","+"])
else:
if(i.find("bit") < 0 ):
decimal_numbers.append(int(i))
decimal_signed.append(isSigned)
if(isSigned):
all_numbers.append([int(i),"int","-"])
else:
all_numbers.append([int(i),"int","+"])
# print(binar_numbers, decimal_numbers)
try:
if (('one' in kwd or 'ones' in kwd or "one's" in kwd) or ('two' not in kwd and 'twos' not in kwd and "two's" not in kwd)) and ('compliment' in kwd or 'complement' in kwd):
if(all_numbers[0][2] == "-"):
return signed_ones_complement(decimal_numbers, all_numbers)
else:
return one_compliment(binar_numbers, decimal_numbers)
elif ('two' in kwd or 'twos' in kwd or "two's" in kwd) and ('compliment' in kwd or 'complement' in kwd):
if(all_numbers[0][2] == "-"):
return signed_twos_complement(decimal_numbers, all_numbers)
else:
return twos_compliment(binar_numbers, decimal_numbers)
elif 'bits' in kwd:
return bit_representation(decimal_numbers,all_numbers)
elif 'sum' in kwd or '+' in kwd:
return binary_add_sub("addition",all_numbers,binar_numbers,decimal_numbers)
elif 'difference' in kwd or '-' in kwd:
return binary_add_sub("subtraction",all_numbers,binar_numbers,decimal_numbers)
elif 'convert' in kwd or 'write' in kwd or 'represent':
if "to decimal" in query or "binary to decimal" in query or 'decimal' in query:
return binary_to_decimal(binar_numbers, decimal_numbers)
elif "to binary" in query or "decimal to binary" in query or 'binary' in query:
if(all_numbers[0][2] == "-"):
return signed_decimal_to_binary(decimal_numbers,all_numbers)
else:
return decimal_to_binary(decimal_numbers)
else:
return ["query format not correct, please repeat the question again.", ""]
except:
# raise Exception
return ["Sorry, can you repeat your question", ""]
# print(binary_module("what's the one's compliment of 1010?"))
# print(binary_module("how many bits are required to represent 37 in binary"))
# print(binary_module("what's the sum of 10101 and 11"))
# print(binary_module("11010 - 001"))
# print(binary_module("convert 10101 to decimal"))
# print(binary_module("convert 24 from decimal to binary "))
# print(twos_compliment(['1101011']))
# print(binary_module("how do i write 67 in binary"))
# print(binary_module("how do i write 110101 in decimal"))
# print(binary_module("what's the one's compliment of 25"))
# print(binary_module("what's the two's compliment of 24"))
# print(binary_module("26 - 9"))
# print(binary_module("26 + 12"))