├── README.md ├── ML ├── kdata.csv ├── folder_wise │ ├── Decision tree │ │ ├── tree.png │ │ ├── tree1.png │ │ ├── Screenshot from 2019-12-26 10-26-40.png │ │ ├── dt.csv │ │ └── decision.py │ ├── KNN │ │ ├── kdata.csv │ │ ├── Screenshot from 2019-12-19 10-28-52.png │ │ ├── Screenshot from 2019-12-19 10-28-57.png │ │ ├── Screenshot from 2019-12-19 10-29-05.png │ │ └── lp3.py │ ├── linear regression │ │ ├── hours.csv │ │ ├── LR.png │ │ └── linear.py │ └── k means │ │ ├── Screenshot from 2019-12-19 10-29-05.png │ │ └── kmeans.py ├── assign2.csv ├── ass3_ml ├── assign_ml_2 └── ml_asg_ml_4 ├── ICS ├── ICSAssignment6.docx ├── ICSAssignment7.docx ├── folder_wise │ ├── ecc │ │ ├── ecc.png │ │ └── ecc.py │ ├── diffie hellman │ │ └── ass7output.odt │ ├── S-AES │ │ ├── Screenshot from 2020-01-23 09-51-47.png │ │ └── saed.py │ ├── RSA │ │ └── RSA.txt │ └── S-DES │ │ └── sdes1.py ├── Assign5 │ ├── ECCProviderTest.java │ ├── ECCKeyGeneration.java │ └── ECCSignature.java ├── DiffieHellmann.java ├── Assn4DiffieHellmann.java ├── SAES.py └── SDES.java ├── BI ├── Sample - Superstore.csv ├── Super Sample Superstore (2).twbx └── Twitter Sentiment Analysis.ipynb └── Compilers ├── assn4 ├── ip.l └── ip.y ├── assn2.c ├── assn3.c ├── assn1.c └── assn5 └── as5.java /README.md: -------------------------------------------------------------------------------- 1 | # BE-Comp-Sem-8 2 | Codes for All Courses 3 | -------------------------------------------------------------------------------- /ML/kdata.csv: -------------------------------------------------------------------------------- 1 | x,y,class 2 | 2,4,neg 3 | 4,6,neg 4 | 4,4,pos 5 | 4,2,neg 6 | 6,4,neg 7 | 6,2,pos 8 | -------------------------------------------------------------------------------- /ICS/ICSAssignment6.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashishsalunkhe/BE-Comp-Sem-8/master/ICS/ICSAssignment6.docx -------------------------------------------------------------------------------- /ICS/ICSAssignment7.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashishsalunkhe/BE-Comp-Sem-8/master/ICS/ICSAssignment7.docx -------------------------------------------------------------------------------- /BI/Sample - Superstore.csv: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashishsalunkhe/BE-Comp-Sem-8/master/BI/Sample - Superstore.csv -------------------------------------------------------------------------------- /ICS/folder_wise/ecc/ecc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashishsalunkhe/BE-Comp-Sem-8/master/ICS/folder_wise/ecc/ecc.png -------------------------------------------------------------------------------- /BI/Super Sample Superstore (2).twbx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashishsalunkhe/BE-Comp-Sem-8/master/BI/Super Sample Superstore (2).twbx -------------------------------------------------------------------------------- /ML/folder_wise/Decision tree/tree.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashishsalunkhe/BE-Comp-Sem-8/master/ML/folder_wise/Decision tree/tree.png -------------------------------------------------------------------------------- /ML/folder_wise/Decision tree/tree1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashishsalunkhe/BE-Comp-Sem-8/master/ML/folder_wise/Decision tree/tree1.png -------------------------------------------------------------------------------- /ML/folder_wise/KNN/kdata.csv: -------------------------------------------------------------------------------- 1 | x,y,class 2 | 2,4,negative 3 | 4,6,negative 4 | 4,4,positive 5 | 4,2,negative 6 | 6,4,negative 7 | 6,2,postive 8 | -------------------------------------------------------------------------------- /ML/folder_wise/linear regression/hours.csv: -------------------------------------------------------------------------------- 1 | hrs,risk 2 | 10,95 3 | 9,80 4 | 2,10 5 | 15,50 6 | 10,45 7 | 16,98 8 | 11,38 9 | 16,93 10 | -------------------------------------------------------------------------------- /ML/folder_wise/linear regression/LR.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashishsalunkhe/BE-Comp-Sem-8/master/ML/folder_wise/linear regression/LR.png -------------------------------------------------------------------------------- /ICS/folder_wise/diffie hellman/ass7output.odt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashishsalunkhe/BE-Comp-Sem-8/master/ICS/folder_wise/diffie hellman/ass7output.odt -------------------------------------------------------------------------------- /ML/folder_wise/KNN/Screenshot from 2019-12-19 10-28-52.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashishsalunkhe/BE-Comp-Sem-8/master/ML/folder_wise/KNN/Screenshot from 2019-12-19 10-28-52.png -------------------------------------------------------------------------------- /ML/folder_wise/KNN/Screenshot from 2019-12-19 10-28-57.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashishsalunkhe/BE-Comp-Sem-8/master/ML/folder_wise/KNN/Screenshot from 2019-12-19 10-28-57.png -------------------------------------------------------------------------------- /ML/folder_wise/KNN/Screenshot from 2019-12-19 10-29-05.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashishsalunkhe/BE-Comp-Sem-8/master/ML/folder_wise/KNN/Screenshot from 2019-12-19 10-29-05.png -------------------------------------------------------------------------------- /ICS/folder_wise/S-AES/Screenshot from 2020-01-23 09-51-47.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashishsalunkhe/BE-Comp-Sem-8/master/ICS/folder_wise/S-AES/Screenshot from 2020-01-23 09-51-47.png -------------------------------------------------------------------------------- /ML/folder_wise/k means/Screenshot from 2019-12-19 10-29-05.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashishsalunkhe/BE-Comp-Sem-8/master/ML/folder_wise/k means/Screenshot from 2019-12-19 10-29-05.png -------------------------------------------------------------------------------- /ML/folder_wise/Decision tree/Screenshot from 2019-12-26 10-26-40.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashishsalunkhe/BE-Comp-Sem-8/master/ML/folder_wise/Decision tree/Screenshot from 2019-12-26 10-26-40.png -------------------------------------------------------------------------------- /Compilers/assn4/ip.l: -------------------------------------------------------------------------------- 1 | %{ 2 | #include "y.tab.h" 3 | %} 4 | ALPHA [A-Z a-z] 5 | DIGIT [0-9] 6 | %% 7 | {ALPHA}({ALPHA}|{DIGIT})* return ID; 8 | {DIGIT}+ {yylval=atoi(yytext); return ID;} 9 | [\n \t] yyterminate(); 10 | . return yytext[0]; 11 | %% 12 | -------------------------------------------------------------------------------- /ML/assign2.csv: -------------------------------------------------------------------------------- 1 | ID,Age,Income,Gender,Marital Status,Buys 2 | 1,<21,high,male,single,no 3 | 2,<21,high,male,married,no 4 | 3,21-35,high,male,single,yes 5 | 4,>35,medium,male,single,yes 6 | 5,>35,low,female,single,yes 7 | 6,>35,low,female,married,no 8 | 7,21-35,low,female,married,yes 9 | 8,<21,medium,male,single,no 10 | 9,<21,low,female,married,yes 11 | 10,>35,medium,female,single,yes 12 | 11,<21,medium,female,married,yes 13 | 12,21-35,medium,male,married,yes 14 | 13,21-35,high,female,single,yes 15 | 14,>35,medium,male,married,no 16 | -------------------------------------------------------------------------------- /ML/folder_wise/Decision tree/dt.csv: -------------------------------------------------------------------------------- 1 | ID,AGE,INCOME,GENDER,MARTIAL STATUS,BUYS 2 | 1,<21,HIGH,MALE,SINGLE,NO 3 | 2,<21,HIGH,MALE,MARRIED,NO 4 | 3,21-35,HIGH,MALE,SINGLE,YES 5 | 4,>35,MEDIUM,MALE,SINGLE,YES 6 | 5,>35,LOW,FEMALE,SINGLE,YES 7 | 6,>35,LOW,FEMALE,MARRIED,NO 8 | 7,21-35,LOW,FEMALE,MARRIED,YES 9 | 8,<21,MEDIUM,MALE,SINGLE,NO 10 | 9,<21,LOW,FEMALE,MARRIED,YES 11 | 10,>35,MEDIUM,FEMALE,SINGLE,YES 12 | 11,<21,MEDIUM,FEMALE,MARRIED,YES 13 | 12,21-35,MEDIUM,MALE,MARRIED,YES 14 | 13,21-35,HIGH,FEMALE,SINGLE,YES 15 | 14,>35,MEDIUM,MALE,MARRIED,NO 16 | -------------------------------------------------------------------------------- /ICS/Assign5/ECCProviderTest.java: -------------------------------------------------------------------------------- 1 | import java.security.Provider; 2 | import java.security.Provider.Service; 3 | import java.security.Security; 4 | import sun.security.ec.SunEC; 5 | 6 | public class ECCProviderTest { 7 | 8 | /** 9 | * @param args the command line arguments 10 | */ 11 | public static void main(final String[] args) { 12 | Provider sunEC = new SunEC(); 13 | Security.addProvider(sunEC); 14 | for(Service service : sunEC.getServices()) { 15 | System.out.println(service.getType() + ": " 16 | + service.getAlgorithm()); 17 | } 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /ICS/Assign5/ECCKeyGeneration.java: -------------------------------------------------------------------------------- 1 | import java.security.*; 2 | import java.security.spec.*; 3 | 4 | public class ECCKeyGeneration { 5 | public static void main(String[] args) throws Exception { 6 | KeyPairGenerator kpg; 7 | kpg = KeyPairGenerator.getInstance("EC","SunEC"); 8 | ECGenParameterSpec ecsp; 9 | ecsp = new ECGenParameterSpec("secp192r1"); 10 | kpg.initialize(ecsp); 11 | 12 | KeyPair kp = kpg.genKeyPair(); 13 | PrivateKey privKey = kp.getPrivate(); 14 | PublicKey pubKey = kp.getPublic(); 15 | 16 | System.out.println(privKey.toString()); 17 | System.out.println(pubKey.toString()); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /ML/ass3_ml: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import pandas as pd 3 | dataset = pd.read_csv("kdata.csv") 4 | print dataset 5 | 6 | X=dataset.iloc[:,:-1].values 7 | print X 8 | Y=dataset.iloc[:,2].values 9 | print Y 10 | 11 | from sklearn.neighbors import KNeighborsClassifier 12 | classifier=KNeighborsClassifier(n_neighbors=3) 13 | classifier.fit(X,Y) 14 | 15 | X_test=np.array([6,6]) 16 | Y_pred=classifier.predict([X_test]) 17 | print 'General KNN:', Y_pred 18 | 19 | classifier=KNeighborsClassifier(n_neighbors=3, weights='distance') 20 | classifier.fit(X,Y) 21 | X_test=np.array([6,2]) 22 | Y_pred=classifier.predict([X_test]) 23 | print 'Distance Weighted KNN:', Y_pred 24 | -------------------------------------------------------------------------------- /Compilers/assn4/ip.y: -------------------------------------------------------------------------------- 1 | %{ 2 | #include 3 | #include 4 | %} 5 | %token ID 6 | %left '+' '-' 7 | %left '*' '/' 8 | %left UMINUS 9 | %% 10 | S : E 11 | E : E'+'{A1();}T{A2();} 12 | | E'-'{A1();}T{A2();} 13 | | T; 14 | T : T'*'{A1();}F{A2();} 15 | | T'/'{A1();}F{A2();} 16 | | F; 17 | F : '('E{A2();}')' 18 | | '-'{A1();}F{A2();} 19 | | ID{A3();}; 20 | %% 21 | #include "lex.yy.c" 22 | char st[100]; 23 | int top=0; 24 | main() 25 | { 26 | printf("Enter infix expression: "); 27 | yyparse(); 28 | printf("\n"); 29 | } 30 | A1() 31 | { 32 | st[top++]=yytext[0]; 33 | } 34 | A2() 35 | {printf("%c",st[--top]); 36 | } 37 | A3() 38 | { 39 | printf("%c",yytext[0]); 40 | } 41 | -------------------------------------------------------------------------------- /ML/folder_wise/KNN/lp3.py: -------------------------------------------------------------------------------- 1 | #import the packages 2 | import pandas as pd 3 | import numpy as np 4 | 5 | #Read dataset 6 | dataset=pd.read_csv("kdata.csv") 7 | X=dataset.iloc[:,:-1].values 8 | y=dataset.iloc[:,2].values 9 | 10 | #import KNeighborsClassifier and create object of it 11 | from sklearn.neighbors import KNeighborsClassifier 12 | classifier=KNeighborsClassifier(n_neighbors=3) 13 | classifier.fit(X,y) 14 | 15 | #predict the class for the point(6,6) 16 | 17 | X_test=np.array([6,6]) 18 | y_pred=classifier.predict([X_test]) 19 | 20 | print 'General KNN:',y_pred 21 | 22 | classifier=KNeighborsClassifier(n_neighbors=3,weights='distance') 23 | classifier.fit(X,y) 24 | 25 | #predict the class for the point(6,6) 26 | 27 | X_test=np.array([6,2]) 28 | y_pred=classifier.predict([X_test]) 29 | 30 | print 'Distance Weighted KNN:',y_pred 31 | 32 | -------------------------------------------------------------------------------- /ML/folder_wise/linear regression/linear.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import pandas as pd 3 | 4 | # Read Dataset 5 | dataset=pd.read_csv("hours.csv") 6 | X=dataset.iloc[:,:-1].values 7 | y=dataset.iloc[:,1].values 8 | 9 | # Import the Linear Regression and Create object of it 10 | from sklearn.linear_model import LinearRegression 11 | regressor=LinearRegression() 12 | regressor.fit(X,y) 13 | Accuracy=regressor.score(X, y)*100 14 | print("Accuracy :") 15 | print(Accuracy) 16 | 17 | # Predict the value using Regressor Object 18 | y_pred=regressor.predict([[10]]) 19 | print(y_pred) 20 | 21 | # Take user input 22 | hours=int(input('Enter the no of hours')) 23 | 24 | #calculate the value of y 25 | eq=regressor.coef_*hours+regressor.intercept_ 26 | #y='%f*%f+%f' %(regressor.coef_,hours,regressor.intercept_) 27 | #print("y :") 28 | #print(y) 29 | print("Risk Score : ", eq[0]) 30 | plt.plot(X,y,'o') 31 | plt.plot(X,regressor.predict(X)); 32 | plt.show() 33 | -------------------------------------------------------------------------------- /ML/folder_wise/Decision tree/decision.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | import numpy as np 3 | 4 | #reading Dataset 5 | dataset=pd.read_csv("dt.csv") 6 | X=dataset.iloc[:,:-1] 7 | y=dataset.iloc[:,5] 8 | 9 | #Perform Label encoding 10 | from sklearn.preprocessing import LabelEncoder 11 | le=LabelEncoder() 12 | 13 | X=X.apply(le.fit_transform) 14 | print("X") 15 | 16 | from sklearn.tree import DecisionTreeClassifier 17 | regressor=DecisionTreeClassifier() 18 | regressor.fit(X.iloc[:,1:5],y) 19 | 20 | #Predict value for the given Expression 21 | X_in=np.array([1,1,0,0]) 22 | y_pred=regressor.predict([X_in]) 23 | print("Prediction:", y_pred) 24 | from sklearn.externals.six import StringIO 25 | from IPython.display import Image 26 | from sklearn.tree import export_graphviz 27 | import pydotplus 28 | 29 | dot_data=StringIO() 30 | 31 | export_graphviz(regressor,out_file=dot_data,filled=True,rounded=True,special_characters=True) 32 | graph=pydotplus.graph_from_dot_data(dot_data.getvalue()) 33 | graph.write_png('tree.png') 34 | -------------------------------------------------------------------------------- /Compilers/assn2.c: -------------------------------------------------------------------------------- 1 | //Implementation of symbol table 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | void main() 8 | { 9 | int i=0,j=0,x=0,n; 10 | void *p,*add[5]; 11 | char ch,srch,b[15],d[15],c; 12 | printf("Expression terminated by $:"); 13 | while((c=getchar())!='$') 14 | { 15 | b[i]=c; 16 | i++; 17 | } 18 | n=i-1; 19 | printf("Given Expression:"); 20 | i=0; 21 | while(i<=n) 22 | { 23 | printf("%c",b[i]); 24 | i++; 25 | } 26 | printf("\n Symbol Table\n"); 27 | printf("Symbol \t addr \t type"); 28 | while(j<=n) 29 | { 30 | c=b[j]; 31 | if(isalpha(toascii(c))) 32 | { 33 | p=malloc(c); 34 | add[x]=p; 35 | d[x]=c; 36 | printf("\n%c \t %d \t identifier\n",c,p); 37 | x++; 38 | j++; 39 | } 40 | else 41 | { 42 | ch=c; 43 | if(ch=='+'||ch=='-'||ch=='*'||ch=='=') 44 | { 45 | p=malloc(ch); 46 | add[x]=p; 47 | d[x]=ch; 48 | printf("\n %c \t %d \t operator\n",ch,p); 49 | x++; 50 | j++; 51 | }}}} 52 | -------------------------------------------------------------------------------- /Compilers/assn3.c: -------------------------------------------------------------------------------- 1 | //Implementation of symbol table 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | void main() 8 | { 9 | int i=0,j=0,x=0,n; 10 | void *p,*add[5]; 11 | char ch,srch,b[15],d[15],c; 12 | printf("Expression terminated by $:"); 13 | while((c=getchar())!='$') 14 | { 15 | b[i]=c; 16 | i++; 17 | } 18 | n=i-1; 19 | printf("Given Expression:"); 20 | i=0; 21 | while(i<=n) 22 | { 23 | printf("%c",b[i]); 24 | i++; 25 | } 26 | printf("\n Symbol Table\n"); 27 | printf("Symbol \t addr \t type"); 28 | while(j<=n) 29 | { 30 | c=b[j]; 31 | if(isalpha(toascii(c))) 32 | { 33 | p=malloc(c); 34 | add[x]=p; 35 | d[x]=c; 36 | printf("\n%c \t %d \t identifier\n",c,p); 37 | x++; 38 | j++; 39 | } 40 | else 41 | { 42 | ch=c; 43 | if(ch=='+'||ch=='-'||ch=='*'||ch=='=') 44 | { 45 | p=malloc(ch); 46 | add[x]=p; 47 | d[x]=ch; 48 | printf("\n %c \t %d \t operator\n",ch,p); 49 | x++; 50 | j++; 51 | }}}} 52 | -------------------------------------------------------------------------------- /ML/assign_ml_2: -------------------------------------------------------------------------------- 1 | # Import package 2 | import numpy as np 3 | import pandas as pd 4 | # reading dataset 5 | dataset= pd.read_csv(r"/home/gmail/assign2.csv") 6 | X=dataset.iloc[:,:-1] 7 | Y=dataset.iloc[:,5].values 8 | #perform label encoding 9 | from sklearn.preprocessing import LabelEncoder 10 | labelencoder_X = LabelEncoder() 11 | X=X.apply(LabelEncoder().fit_transform) 12 | print (X) 13 | 14 | from sklearn.tree import DecisionTreeClassifier 15 | regressor=DecisionTreeClassifier() 16 | regressor.fit(X.iloc[:,1:5],Y) 17 | 18 | #predict value for the given expression 19 | X_in=np.array([1,1,0,0]) 20 | 21 | y_pred=regressor.predict([X_in]) 22 | print ("Prediction", y_pred) 23 | 24 | from sklearn.externals.six import StringIO 25 | from IPython.display import Image 26 | from sklearn.tree import export_graphviz 27 | import pydotplus 28 | 29 | dot_data = StringIO() 30 | 31 | export_graphviz(regressor, out_file=dot_data , filled=True, rounded = True) 32 | 33 | graph= pydotplus.graph_from_dot_data(dot_data.getvalue()) 34 | graph.write_png("tree.png") 35 | -------------------------------------------------------------------------------- /ICS/DiffieHellmann.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Diffie_Hellman 4 | { 5 | public static void main(String args[]) 6 | { 7 | Scanner sc=new Scanner(System.in); 8 | System.out.println("Enter Prime Number P"); 9 | int p=sc.nextInt(); 10 | System.out.println("Enter primitive root of "+p); 11 | int g=sc.nextInt(); 12 | System.out.println("Choose Private Key (Alice)"); 13 | int a=sc.nextInt(); 14 | System.out.println("Choose Private Key (BOB)"); 15 | int b=sc.nextInt(); 16 | 17 | int A = (int)Math.pow(g,a)%p; 18 | int B = (int)Math.pow(g,b)%p; 19 | 20 | System.out.println("Public Key (Alice) : "+A); 21 | System.out.println("Public Key (BOB) : "+B); 22 | 23 | int S_A = (int)Math.pow(B,a)%p; 24 | int S_B =(int)Math.pow(A,b)%p; 25 | 26 | if(S_A==S_B) 27 | { 28 | System.out.println("ALice and Bob can communicate with each other!!!"); 29 | System.out.println("They share a secret no = "+S_A); 30 | } 31 | 32 | else 33 | { 34 | System.out.println("ALice and Bob cannot communicate with each other!!!"); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /ICS/folder_wise/RSA/RSA.txt: -------------------------------------------------------------------------------- 1 | from math import sqrt 2 | def prime(pr): 3 | for i in range(2, int(sqrt(pr))): 4 | if pr%i == 0: 5 | return False 6 | return True 7 | 8 | def gcd(x, y): 9 | while(y): 10 | x, y = y, x % y 11 | return x 12 | 13 | import sys 14 | def calc_key(phi): 15 | t = phi+1 16 | for i in range(2, phi): 17 | if prime(i) and t%i == 0: 18 | e = i 19 | d = int(t/e) 20 | return e, d 21 | if i == phi-1: 22 | sys.exit("Can't compute keys for existing prime nos.") 23 | 24 | p = 11 25 | q = 17 26 | n = p*q 27 | phi = (p-1)*(q-1) 28 | e, d = calc_key(phi) 29 | msg = 88 30 | encrypt_msg = pow(msg, e, n) 31 | decrypt_msg = pow(encrypt_msg, d, n) 32 | 33 | print("Original Message: ", msg) 34 | print("Encryption Key: ", e) 35 | print("Decryption Key: ", d) 36 | print("Encrypted Message: ", encrypt_msg) 37 | print("Decrypted Message: ", decrypt_msg) 38 | 39 | 40 | ### 41 | ubuntu@PL-lab:~$ gedit RSA.py 42 | ubuntu@PL-lab:~$ python3 RSA.py 43 | Original Message: 88 44 | Encryption Key: 7 45 | Decryption Key: 23 46 | Encrypted Message: 11 47 | Decrypted Message: 88 48 | ubuntu@PL-lab:~$ 49 | ### 50 | -------------------------------------------------------------------------------- /Compilers/assn1.c: -------------------------------------------------------------------------------- 1 | %{ 2 | int COMMENT=0; 3 | %} 4 | identifier [a-zA-Z][a-zA-Z0-9]* 5 | %% 6 | #.* {printf("\n%s is a preprocessor directive",yytext);} 7 | int | 8 | float | 9 | char | 10 | double | 11 | while | 12 | for | 13 | struct | 14 | typedef | 15 | do | 16 | if | 17 | break | 18 | continue | 19 | void | 20 | switch | 21 | return | 22 | else | 23 | goto {printf("\n\t%s is a keyword",yytext);} 24 | "/*" {COMMENT=1;}{printf("\n\t %s is a COMMENT",yytext);} 25 | {identifier}\( {if(!COMMENT)printf("\nFUNCTION \n\t%s",yytext);} 26 | \{ {if(!COMMENT)printf("\n BLOCK BEGINS");} 27 | \} {if(!COMMENT)printf("BLOCK ENDS ");} 28 | {identifier}(\[[0-9]*\])? {if(!COMMENT) printf("\n %s IDENTIFIER",yytext);} 29 | \".*\" {if(!COMMENT)printf("\n\t %s is a STRING",yytext);} 30 | [0-9]+ {if(!COMMENT) printf("\n %s is a NUMBER ",yytext);} 31 | \)(\:)? {if(!COMMENT)printf("\n\t");ECHO;printf("\n");} 32 | \( ECHO; 33 | = {if(!COMMENT)printf("\n\t %s is an ASSIGNMENT OPERATOR",yytext);} 34 | \<= | 35 | \>= | 36 | \< | 37 | == | 38 | \> {if(!COMMENT) printf("\n\t%s is a RELATIONAL OPERATOR",yytext);} 39 | %% 40 | int main(int argc, char **argv) 41 | { 42 | FILE *file; 43 | file=fopen("var.c","r"); 44 | if(!file) 45 | { 46 | printf("could not open the file"); 47 | exit(0); 48 | } 49 | yyin=file; 50 | yylex(); 51 | printf("\n"); 52 | return(0); 53 | } 54 | int yywrap() 55 | { 56 | return(1); 57 | } 58 | 59 | -------------------------------------------------------------------------------- /ICS/Assign5/ECCSignature.java: -------------------------------------------------------------------------------- 1 | import java.math.BigInteger; 2 | import java.security.*; 3 | import java.security.spec.*; 4 | 5 | public class ECCSignature { 6 | public static void main(String[] args) throws Exception { 7 | KeyPairGenerator kpg; 8 | kpg = KeyPairGenerator.getInstance("EC","SunEC"); 9 | 10 | ECGenParameterSpec ecsp; 11 | ecsp = new ECGenParameterSpec("sect163k1"); 12 | kpg.initialize(ecsp); 13 | 14 | KeyPair kp = kpg.genKeyPair(); 15 | PrivateKey privKey = kp.getPrivate(); 16 | PublicKey pubKey = kp.getPublic(); 17 | System.out.println(privKey.toString()); 18 | System.out.println(pubKey.toString()); 19 | 20 | Signature ecdsa; 21 | ecdsa = Signature.getInstance("SHA1withECDSA","SunEC"); 22 | ecdsa.initSign(privKey); 23 | 24 | String text = "In teaching others we teach ourselves"; 25 | System.out.println("Text: " + text); 26 | byte[] baText = text.getBytes("UTF-8"); 27 | 28 | ecdsa.update(baText); 29 | byte[] baSignature = ecdsa.sign(); 30 | System.out.println("Signature: 0x" + (new BigInteger(1, baSignature).toString(16)).toUpperCase()); 31 | 32 | Signature signature; 33 | signature = Signature.getInstance("SHA1withECDSA","SunEC"); 34 | signature.initVerify(pubKey); 35 | signature.update(baText); 36 | boolean result = signature.verify(baSignature); 37 | System.out.println("Valid: " + result); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /ICS/Assn4DiffieHellmann.java: -------------------------------------------------------------------------------- 1 | import java.math.*; 2 | import java.util.Scanner; 3 | class RSA 4 | { 5 | public static void main(String args[]) 6 | { 7 | Scanner sc=new Scanner(System.in); 8 | int p,q,n,z,d=0,e,i; 9 | System.out.println("Enter the number to be encrypted and decrypted"); 10 | int msg=sc.nextInt(); 11 | double c; 12 | BigInteger msgback; 13 | System.out.println("Enter 1st prime number p"); 14 | p=sc.nextInt(); 15 | System.out.println("Enter 2nd prime number q"); 16 | q=sc.nextInt(); 17 | 18 | n=p*q; 19 | z=(p-1)*(q-1); 20 | System.out.println("the value of z = "+z); 21 | 22 | for(e=2;e#include#includevoid main(){char pg[100][100],str1[24];int tem=-1,ct=0,i=-1,j=0,j1,pos=-1,t=-1,flag,flag1,tt=0,fg=0;clrscr(); printf("Enter the codings \n");while(i>-2){i++;lab1:t++;scanf("%s",&pg[i]);if((strcmp(pg[i],"getch();"))==0){i=-2;goto lab1;}} printf("\n pos \t oper \t arg1 \t arg2 \tresult \n");while(j 0: 29 | if b % 2 == 0: 30 | x = (x * y) % c; 31 | y = (y * y) % c 32 | b = int(b / 2) 33 | 34 | return x % c 35 | 36 | # Asymmetric encryption 37 | def encrypt(msg, q, h, g): 38 | 39 | en_msg = [] 40 | 41 | k = gen_key(q)# Private key for sender 42 | s = power(h, k, q) 43 | p = power(g, k, q) 44 | 45 | for i in range(0, len(msg)): 46 | en_msg.append(msg[i]) 47 | 48 | print("g^k used : ", p) 49 | print("g^ak used : ", s) 50 | for i in range(0, len(en_msg)): 51 | en_msg[i] = s * ord(en_msg[i]) 52 | 53 | return en_msg, p 54 | 55 | def decrypt(en_msg, p, key, q): 56 | 57 | dr_msg = [] 58 | h = power(p, key, q) 59 | for i in range(0, len(en_msg)): 60 | dr_msg.append(chr(int(en_msg[i]/h))) 61 | 62 | return dr_msg 63 | 64 | # Driver code 65 | def main(): 66 | 67 | msg = 'encryption' 68 | print("Original Message :", msg) 69 | 70 | q = random.randint(pow(10, 20), pow(10, 50)) 71 | g = random.randint(2, q) 72 | 73 | key = gen_key(q)# Private key for receiver 74 | h = power(g, key, q) 75 | print("g used : ", g) 76 | print("g^a used : ", h) 77 | 78 | en_msg, p = encrypt(msg, q, h, g) 79 | dr_msg = decrypt(en_msg, p, key, q) 80 | dmsg = ''.join(dr_msg) 81 | print("Decrypted Message :", dmsg); 82 | 83 | 84 | if __name__ == '__main__': 85 | main() 86 | -------------------------------------------------------------------------------- /ML/ml_asg_ml_4: -------------------------------------------------------------------------------- 1 | #Load the required Libraries 2 | import numpy as np 3 | import pandas as pd 4 | from copy import deepcopy 5 | from matplotlib import pyplot as plt 6 | %matplotlib inline 7 | #Given Dataset 8 | dataset = { 9 | 'Points':['P1','P2','P3','P4','P5','P6','P7','P8',], 10 | 'x_coordinate':[0.1,0.15,0.08,0.16,0.2,0.25,0.24,0.3], 11 | 'y_coordinate':[0.6,0.71,0.9,0.85,0.3,0.5,0.1,0.2] 12 | } 13 | #dataframe 14 | df = pd.DataFrame(dataset,columns=['Points','x_coordinate','y_coordinate']) 15 | df 16 | # Getting the values and plotting it 17 | f1 = df['x_coordinate'].values 18 | f2 = df['y_coordinate'].values 19 | X = np.array(list(zip(f1, f2))) 20 | plt.scatter(f1, f2, c='black', s=7) 21 | # Euclidean Distance Caculator 22 | def dist(a, b, ax=1): 23 | return np.linalg.norm(a - b, axis=ax) 24 | # Number of clusters 25 | k = 2 26 | # Two initia Centroids are given 27 | # m1 = P1 28 | # m2 = P8 29 | Centroid_m1 = list(X[0]) 30 | Centroid_m2 = list(X[7]) 31 | Centroids = np.array([Centroid_m1,Centroid_m2]) 32 | print(Centroids) 33 | # Plotting along with the Centroids 34 | plt.scatter(f1, f2, c='#050505', s=7) 35 | plt.scatter(Centroid_m1[0],Centroid_m1[1] ,marker='*', s=200, c='g') 36 | plt.scatter(Centroid_m2[0],Centroid_m2[1] ,marker='*', s=200, c='g') 37 | # To store the value of centroids when it updates 38 | C_old = np.zeros(Centroids.shape) 39 | # Cluster Lables(0, 1, 2) 40 | clusters = np.zeros(len(X)) 41 | # Error func. - Distance between new centroids and old centroids 42 | error = dist(Centroids, C_old, None) 43 | # Loop will run till the error becomes zero 44 | while error != 0: 45 | # Assigning each value to its closest cluster 46 | for i in range(len(X)): 47 | distances = dist(X[i], Centroids) 48 | cluster = np.argmin(distances) 49 | clusters[i] = cluster 50 | # Storing the old centroid values 51 | C_old = deepcopy(Centroids) 52 | print(C_old) 53 | # Finding the new centroids by taking the average value 54 | for i in range(k): 55 | points = [X[j] for j in range(len(X)) if clusters[j] == i] 56 | Centroids[i] = np.mean(points, axis=0) 57 | print(Centroids[i]) 58 | error = dist(Centroids, C_old, None) 59 | colors = ['r', 'g'] 60 | fig, ax = plt.subplots() 61 | for i in range(k): 62 | points = np.array([X[j] for j in range(len(X)) if clusters[j] == i]) 63 | ax.scatter(points[:, 0], points[:, 1], s=7, c=colors[i]) 64 | ax.scatter(Centroids[:, 0], Centroids[:, 1], marker='*', s=200, c='#050505') 65 | -------------------------------------------------------------------------------- /ICS/folder_wise/S-DES/sdes1.py: -------------------------------------------------------------------------------- 1 | FIXED_IP = [2, 6, 3, 1, 4, 8, 5, 7] 2 | FIXED_EP = [4, 1, 2, 3, 2, 3, 4, 1] 3 | FIXED_IP_INVERSE = [4, 1, 3, 5, 7, 2, 8, 6] 4 | FIXED_P10 = [3, 5, 2, 7, 4, 10, 1, 9, 8, 6] 5 | FIXED_P8 = [6, 3, 7, 4, 8, 5, 10, 9] 6 | FIXED_P4 = [2, 4, 3, 1] 7 | 8 | S0 = [[1, 0, 3, 2], 9 | [3, 2, 1, 0], 10 | [0, 2, 1, 3], 11 | [3, 1, 3, 2]] 12 | 13 | S1 = [[0, 1, 2, 3], 14 | [2, 0, 1, 3], 15 | [3, 0, 1, 0], 16 | [2, 1, 0, 3]] 17 | 18 | 19 | def permute(bits, pertable): 20 | string_new = "" 21 | for i in range(len(pertable)): 22 | string_new = string_new + bits[pertable[i] - 1] 23 | return string_new 24 | 25 | 26 | def shift(bits): 27 | string_new = "" 28 | for i in range(1, len(bits)): 29 | string_new = string_new + bits[i] 30 | string_new = string_new + bits[0] 31 | return string_new 32 | 33 | 34 | def get_keys(key): 35 | keys = [] 36 | key = permute(key, FIXED_P10) 37 | ls_1 = shift(key[:5]) + shift(key[5:]) 38 | keys.append(permute(ls_1, FIXED_P8)) 39 | ls_2 = shift(ls_1[:5]) + shift(ls_1[5:]) 40 | ls_2 = shift(ls_2[:5]) + shift(ls_2[5:]) 41 | keys.append(permute(ls_2, FIXED_P8)) 42 | return keys 43 | 44 | 45 | def xor(bits1, bits2): 46 | new_string = "" 47 | for i in range(len(bits1)): 48 | new_string = new_string + str((int(bits1[i]) + int(bits2[i])) % 2) 49 | return new_string 50 | 51 | 52 | def substitute(bits,table): 53 | row = bits[0]+bits[3] 54 | col = bits[1]+bits[2] 55 | row_num = int(row, base=2) 56 | col_num = int(col, base=2) 57 | 58 | output = table[row_num][col_num] 59 | output = format(output, 'b').zfill(2) 60 | 61 | return output 62 | 63 | 64 | def fk(right, key): 65 | ep = permute(right, FIXED_EP) 66 | output = xor(ep, key) 67 | s0 = substitute(output[:4], S0) 68 | s1 = substitute(output[4:], S1) 69 | output = permute(s0+s1,FIXED_P4) 70 | return output 71 | 72 | 73 | def function_k(bits, key): 74 | right = fk(bits[4:],key) 75 | left = bits[:4] 76 | output = xor(left, right) 77 | return output 78 | 79 | 80 | def swap(left,right): 81 | return right + left 82 | 83 | 84 | def encryption(bits, key): 85 | keys = get_keys(key) 86 | print("[key1,key2]=") 87 | print(keys) 88 | bits = permute(bits, FIXED_IP) 89 | left = function_k(bits, keys[0]) 90 | right = bits[4:] 91 | bits = swap(left, right) 92 | left = function_k(bits, keys[1]) 93 | right = bits[4:] 94 | output = left+right 95 | output = permute(output,FIXED_IP_INVERSE) 96 | return output 97 | 98 | 99 | def decryption(bits, key): 100 | keys = get_keys(key) 101 | bits = permute(bits, FIXED_IP) 102 | left = function_k(bits, keys[1]) 103 | right = bits[4:] 104 | bits = swap(left, right) 105 | left = function_k(bits, keys[0]) 106 | right = bits[4:] 107 | output = left+right 108 | output = permute(output,FIXED_IP_INVERSE) 109 | return output 110 | 111 | if __name__ == '__main__': 112 | key = "1010000010" 113 | plaintext = "10101110" 114 | print("Enter 10 bit Key: ") 115 | key = input() 116 | print("Enter 8 bit PlainText: ") 117 | plaintext = input() 118 | #print(int("100", base=2)) 119 | ciper = encryption(plaintext, key) 120 | print("ciper text afer sdes: "+ciper) 121 | print("After decrypting ciper text: "+decryption(ciper,key)) 122 | 123 | ### 124 | output: 125 | Enter 10 bit Key: 126 | 1010101011 127 | Enter 8 bit PlainText: 128 | 10100101 129 | [key1,key2]= 130 | ['11100110', '01011011'] 131 | ciper text afer sdes: 10111111 132 | After decrypting ciper text: 10100101 133 | ### 134 | -------------------------------------------------------------------------------- /ICS/SAES.py: -------------------------------------------------------------------------------- 1 | # S boxe 2 | sbox = [9, 4, 10, 11, 13, 1, 8, 5, 6, 2, 0, 3, 12, 14, 15, 7] 3 | 4 | def keyExpansion(key): # generates 2 round keys 5 | x = [key[:4], key[4:8], key[8:12], key[12:16]] 6 | for i in range(4): # binary to decimal for each nible 7 | x[i] = list(map(int, x[i])) 8 | x[i] = x[i][0] * 8 + x[i][1] * 4 + x[i][2] * 2 + x[i][3] 9 | keylist = [x[0], x[1], x[2], x[3]] 10 | for i in range(2): 11 | w2 = [0, 0, 0, 0] 12 | if i == 0: 13 | val = 8 # rcon for first round 14 | else: 15 | val = 3 # rcon for 2nd round 16 | w2[0] = keylist[4 * i] ^ val ^ (sbox[keylist[4 * i + 3]]) 17 | w2[1] = keylist[4 * i + 1] ^ 0 ^ (sbox[keylist[4 * i + 2]]) 18 | w2[2] = w2[0] ^ keylist[4 * i + 2] 19 | w2[3] = w2[1] ^ keylist[4 * i + 3] 20 | keylist.append(w2[0]) 21 | keylist.append(w2[1]) 22 | keylist.append(w2[2]) 23 | keylist.append(w2[3]) 24 | return keylist # has all 3 sub-keys 25 | 26 | 27 | def getByteFromBit(x):# converts binary to bytes 28 | y = [] 29 | i = 0 30 | while i < (len(x)): 31 | y.append(8 * x[i] + 4 * x[i + 1] + 2 * x[i + 2] + x[i + 3]) 32 | i += 4 33 | return y 34 | 35 | 36 | def mixCols(y): # applies Mix-Columns 37 | w = [] 38 | for i in range(len(y)): 39 | val = y[i] * 4 40 | if val >= 32: 41 | val ^= 38 42 | if val >= 16: 43 | val ^= 19 44 | w.append(val) 45 | ans = [0, 0, 0, 0] 46 | ans[0] = y[0] ^ w[1] 47 | ans[1] = y[1] ^ w[0] 48 | ans[2] = y[2] ^ w[3] 49 | ans[3] = y[3] ^ w[2] 50 | return ans 51 | 52 | 53 | def convertByteToBit(y): # converts byte value to binary 54 | cipher = [] 55 | for i in range(len(y)): 56 | val = y[i] 57 | val1 = val // 8 58 | cipher.append(val1) 59 | val = val % 8 60 | val1 = val // 4 61 | cipher.append(val1) 62 | val = val % 4 63 | val1 = val // 2 64 | cipher.append(val1) 65 | val1 = val % 2 66 | cipher.append(val1) 67 | cipher = list(map(str, cipher)) 68 | return "".join(cipher) 69 | 70 | 71 | def mult(x, y): 72 | val = x * y 73 | if y == 2: 74 | if val >= 32: 75 | val ^= 38 76 | if val >= 16: 77 | val ^= 19 78 | else: 79 | val = x * 8 80 | if val >= 64: 81 | val ^= 76 82 | if val >= 32: 83 | val ^= 38 84 | if val >= 16: 85 | val ^= 19 86 | val ^= x 87 | return val 88 | 89 | 90 | def inverseMixCols(y): # applies inverse Mix-Columns 91 | w = [0, 0, 0, 0] 92 | w[0] = mult(y[0], 9) ^ mult(y[1], 2) 93 | w[1] = mult(y[1], 9) ^ mult(y[0], 2) 94 | w[2] = mult(y[2], 9) ^ mult(y[3], 2) 95 | w[3] = mult(y[3], 9) ^ mult(y[2], 2) 96 | return w 97 | 98 | def aesDecrypt(y, keylist): # applies Decryption Algorithm 99 | j = 2 100 | for i in range(len(y)): 101 | y[i] ^= keylist[4 * j + i] 102 | j = 1 103 | while j >= 0: 104 | y[1], y[3] = y[3], y[1] 105 | for i in range(len(y)): 106 | y[i] = sbox.index(y[i]) 107 | for i in range(len(y)): 108 | y[i] ^= keylist[4 * j + i] 109 | if j != 0: 110 | y = inverseMixCols(y) 111 | j -= 1 112 | return convertByteToBit(y) 113 | 114 | 115 | def aesEncrypt(y, keylist): # applies Encryption Algorithm 116 | for i in range(len(y)): 117 | y[i] ^= keylist[i % 4] 118 | for i in range(1, 3): 119 | for j in range(len(y)): 120 | y[j] = sbox[y[j]] 121 | y[1], y[3] = y[3], y[1] 122 | if i != 2: 123 | y = mixCols(y) 124 | for j in range(len(y)): 125 | y[j] = y[j] ^ keylist[4 * i + j] 126 | return convertByteToBit(y) 127 | 128 | 129 | if __name__ == "__main__": 130 | print("Enter the plaintext : ") # any length char input 131 | x = raw_input() 132 | print("Enter the key : ") # char input of length 2 133 | key = raw_input() 134 | 135 | keylist = keyExpansion(key) 136 | print(keylist[0:4]); 137 | 138 | x = list(map(int, x)) 139 | i = 0 140 | cipher = "" 141 | while i < len(x) - 1: 142 | y = getByteFromBit(x[i:i + 16]) 143 | cipher += aesEncrypt(y, keylist) 144 | i += 16 145 | print("Cipher text after encryption is : ") 146 | print(cipher) 147 | x = list(map(int, cipher)) 148 | i = 0 149 | plaintext = "" 150 | while i < len(x) - 1: 151 | y = getByteFromBit(x[i:i + 16]) 152 | plaintext += aesDecrypt(y, keylist) 153 | i += 16 154 | print("Plain text after decryption is : ") 155 | print(plaintext) 156 | -------------------------------------------------------------------------------- /ICS/folder_wise/S-AES/saed.py: -------------------------------------------------------------------------------- 1 | # S boxe 2 | sbox = [9, 4, 10, 11, 13, 1, 8, 5, 6, 2, 0, 3, 12, 14, 15, 7] 3 | 4 | 5 | def convertNumToAsciiBit(x): # coverts decimal to binary 6 | y = "" 7 | for i in range(len(x)): 8 | val = ord(x[i]) 9 | j = 7 10 | ans = "" 11 | while j >= 0: 12 | w = val // (pow(2, j)) 13 | ans += str(w) 14 | val = val % (pow(2, j)) 15 | j -= 1 16 | y += ans 17 | return y 18 | 19 | 20 | def convertAsciiToChar(x): # converts ASCII value to char 21 | y = "" 22 | for i in range(0, len(x), 8): 23 | ans = 0 24 | for j in range(8): 25 | ans += int(x[i + j]) * pow(2, 7 - j) 26 | if i == len(x) - 8: 27 | if chr(ans) != '#': 28 | y += chr(ans) 29 | else: 30 | y += chr(ans) 31 | return y 32 | 33 | 34 | def keyExpansion(key): # generates 2 round keys 35 | x = [key[:4], key[4:8], key[8:12], key[12:16]] 36 | for i in range(4): # binary to decimal for each nible 37 | x[i] = list(map(int, x[i])) 38 | x[i] = x[i][0] * 8 + x[i][1] * 4 + x[i][2] * 2 + x[i][3] 39 | keylist = [x[0], x[1], x[2], x[3]] 40 | for i in range(2): 41 | w2 = [0, 0, 0, 0] 42 | if i == 0: 43 | val = 8 # rcon for first round 44 | else: 45 | val = 3 # rcon for 2nd round 46 | w2[0] = keylist[4 * i] ^ val ^ (sbox[keylist[4 * i + 3]]) 47 | w2[1] = keylist[4 * i + 1] ^ 0 ^ (sbox[keylist[4 * i + 2]]) 48 | w2[2] = w2[0] ^ keylist[4 * i + 2] 49 | w2[3] = w2[1] ^ keylist[4 * i + 3] 50 | keylist.append(w2[0]) 51 | keylist.append(w2[1]) 52 | keylist.append(w2[2]) 53 | keylist.append(w2[3]) 54 | return keylist # has all 3 sub-keys 55 | 56 | 57 | def getByteFromBit(x):# converts binary to bytes 58 | y = [] 59 | i = 0 60 | while i < (len(x)): 61 | y.append(8 * x[i] + 4 * x[i + 1] + 2 * x[i + 2] + x[i + 3]) 62 | i += 4 63 | return y 64 | 65 | 66 | def mixCols(y): # applies Mix-Columns 67 | w = [] 68 | for i in range(len(y)): 69 | val = y[i] * 4 70 | if val >= 32: 71 | val ^= 38 72 | if val >= 16: 73 | val ^= 19 74 | w.append(val) 75 | ans = [0, 0, 0, 0] 76 | ans[0] = y[0] ^ w[1] 77 | ans[1] = y[1] ^ w[0] 78 | ans[2] = y[2] ^ w[3] 79 | ans[3] = y[3] ^ w[2] 80 | return ans 81 | 82 | 83 | def convertByteToBit(y): # converts byte value to binary 84 | cipher = [] 85 | for i in range(len(y)): 86 | val = y[i] 87 | val1 = val // 8 88 | cipher.append(val1) 89 | val = val % 8 90 | val1 = val // 4 91 | cipher.append(val1) 92 | val = val % 4 93 | val1 = val // 2 94 | cipher.append(val1) 95 | val1 = val % 2 96 | cipher.append(val1) 97 | cipher = list(map(str, cipher)) 98 | return "".join(cipher) 99 | 100 | 101 | def mult(x, y): 102 | val = x * y 103 | if y == 2: 104 | if val >= 32: 105 | val ^= 38 106 | if val >= 16: 107 | val ^= 19 108 | else: 109 | val = x * 8 110 | if val >= 64: 111 | val ^= 76 112 | if val >= 32: 113 | val ^= 38 114 | if val >= 16: 115 | val ^= 19 116 | val ^= x 117 | return val 118 | 119 | 120 | def inverseMixCols(y): # applies inverse Mix-Columns 121 | w = [0, 0, 0, 0] 122 | w[0] = mult(y[0], 9) ^ mult(y[1], 2) 123 | w[1] = mult(y[1], 9) ^ mult(y[0], 2) 124 | w[2] = mult(y[2], 9) ^ mult(y[3], 2) 125 | w[3] = mult(y[3], 9) ^ mult(y[2], 2) 126 | return w 127 | 128 | 129 | def aesDecrypt(y, keylist): # applies Decryption Algorithm 130 | j = 2 131 | for i in range(len(y)): 132 | y[i] ^= keylist[4 * j + i] 133 | j = 1 134 | while j >= 0: 135 | y[1], y[3] = y[3], y[1] 136 | for i in range(len(y)): 137 | y[i] = sbox.index(y[i]) 138 | for i in range(len(y)): 139 | y[i] ^= keylist[4 * j + i] 140 | if j != 0: 141 | y = inverseMixCols(y) 142 | j -= 1 143 | return convertByteToBit(y) 144 | 145 | 146 | def aesEncrypt(y, keylist): # applies Encryption Algorithm 147 | for i in range(len(y)): 148 | y[i] ^= keylist[i % 4] 149 | for i in range(1, 3): 150 | for j in range(len(y)): 151 | y[j] = sbox[y[j]] 152 | y[1], y[3] = y[3], y[1] 153 | if i != 2: 154 | y = mixCols(y) 155 | for j in range(len(y)): 156 | y[j] = y[j] ^ keylist[4 * i + j] 157 | return convertByteToBit(y) 158 | 159 | 160 | if __name__ == "__main__": 161 | print("Enter the plaintext : ") # any length char input 162 | x = input() 163 | print("Enter the key : ") # char input of length 2 164 | key = input() 165 | if len(key) != 2: 166 | print("BAD KEY : Should be 16 bits") 167 | exit(0) 168 | key = convertNumToAsciiBit(key) 169 | keylist = keyExpansion(key) 170 | if len(x) % 2 != 0: 171 | x += '#' # filler - # 172 | x = convertNumToAsciiBit(x) 173 | x = list(map(int, x)) 174 | i = 0 175 | cipher = "" 176 | while i < len(x) - 1: 177 | y = getByteFromBit(x[i:i + 16]) 178 | cipher += aesEncrypt(y, keylist) 179 | i += 16 180 | print("Cipher text after encryption is : ") 181 | print(cipher) 182 | print(convertAsciiToChar(cipher)) 183 | x = list(map(int, cipher)) 184 | i = 0 185 | plaintext = "" 186 | while i < len(x) - 1: 187 | y = getByteFromBit(x[i:i + 16]) 188 | plaintext += aesDecrypt(y, keylist) 189 | i += 16 190 | print("Plain text after decryption is : ") 191 | print(plaintext) 192 | print(convertAsciiToChar(plaintext)) 193 | 194 | 195 | 196 | -------------------------------------------------------------------------------- /ICS/SDES.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | /** This class Generated two 8-bit subkeys from 10-bit input key **/ 4 | class KeyGeneration 5 | { 6 | private int[] key = new int[10]; 7 | private int[] k1 = new int[8]; 8 | private int[] k2 = new int[8]; 9 | private boolean flag = false; 10 | 11 | KeyGeneration() 12 | { 13 | 14 | } 15 | 16 | void GenerateKeys(String inputkey ) 17 | { 18 | int[] key = new int[10]; 19 | char c1; 20 | String ts ; 21 | 22 | try 23 | { 24 | for(int i=0;i<10;i++) 25 | { 26 | c1 = inputkey.charAt(i); 27 | ts = Character.toString(c1); 28 | key[i] = Integer.parseInt(ts); 29 | 30 | if(key[i] !=0 && key[i]!=1) 31 | { 32 | Print.msg("\n .. Invalid Key .."); 33 | System.exit(0); 34 | return ; 35 | } 36 | } 37 | } 38 | catch(Exception e) 39 | { 40 | Print.msg("\n .. Invalid Key .. "); 41 | System.exit(0); 42 | return ; 43 | 44 | } 45 | this.key = key; 46 | 47 | Print.msg("Input Key : "); 48 | Print.array(this.key,10); 49 | Print.msg("\n"); 50 | 51 | permutationP10(); 52 | 53 | Print.msg("After Permutation(P10) Key : "); 54 | Print.array(this.key,10); 55 | Print.msg("\n"); 56 | 57 | leftshiftLS1(); 58 | 59 | Print.msg("After LeftShift LS-1 Key : "); 60 | Print.array(this.key,10); 61 | Print.msg("\n"); 62 | 63 | 64 | this.k1 = permutationP8(); 65 | 66 | Print.msg("Subkey K1 Generated : "); 67 | Print.array(this.k1,8); 68 | Print.msg("\n"); 69 | 70 | leftshiftLS2(); 71 | 72 | Print.msg("After LeftShift LS-2 Key : "); 73 | Print.array(this.key,10); 74 | Print.msg("\n"); 75 | 76 | this.k2 = permutationP8(); 77 | Print.msg("Subkey K2 Generated : "); 78 | Print.array(this.k2,8); 79 | Print.msg("\n"); 80 | 81 | flag = true; 82 | 83 | } 84 | 85 | /** Perform permutation P10 on 10-bit key 86 | P10(k1, k2, k3, k4, k5, k6, k7, k8, k9, k10) = (k3, k5, k2, k7, k4, k10, k1, k9, k8, k6) 87 | **/ 88 | 89 | private void permutationP10() 90 | { 91 | int[] temp = new int[10]; 92 | 93 | temp[0] = key[2]; 94 | temp[1] = key[4]; 95 | temp[2] = key[1]; 96 | temp[3] = key[6]; 97 | temp[4] = key[3]; 98 | temp[5] = key[9]; 99 | temp[6] = key[0]; 100 | temp[7] = key[8]; 101 | temp[8] = key[7]; 102 | temp[9] = key[5]; 103 | 104 | 105 | key = temp; 106 | 107 | } 108 | 109 | /** Performs a circular left shift (LS-1), or rotation, separately on the first 110 | five bits and the second five bits. **/ 111 | 112 | private void leftshiftLS1() 113 | { 114 | int[] temp = new int[10]; 115 | 116 | temp[0] = key[1]; 117 | temp[1] = key[2]; 118 | temp[2] = key[3]; 119 | temp[3] = key[4]; 120 | temp[4] = key[0]; 121 | 122 | temp[5] = key[6]; 123 | temp[6] = key[7]; 124 | temp[7] = key[8]; 125 | temp[8] = key[9]; 126 | temp[9] = key[5]; 127 | 128 | key = temp; 129 | 130 | } 131 | 132 | /** apply Permutaion P8, which picks out and permutes 8 of the 10 bits according to the following 133 | rule: P8[ 6 3 7 4 8 5 10 9 ] , 8-bit subkey is returned **/ 134 | private int[] permutationP8() 135 | { 136 | int[] temp = new int[8]; 137 | 138 | temp[0] = key[5]; 139 | temp[1] = key[2]; 140 | temp[2] = key[6]; 141 | temp[3] = key[3]; 142 | temp[4] = key[7]; 143 | temp[5] = key[4]; 144 | temp[6] = key[9]; 145 | temp[7] = key[8]; 146 | 147 | return temp; 148 | } 149 | 150 | 151 | private void leftshiftLS2() 152 | { 153 | int[] temp = new int[10]; 154 | 155 | temp[0] = key[2]; 156 | temp[1] = key[3]; 157 | temp[2] = key[4]; 158 | temp[3] = key[0]; 159 | temp[4] = key[1]; 160 | 161 | temp[5] = key[7]; 162 | temp[6] = key[8]; 163 | temp[7] = key[9]; 164 | temp[8] = key[5]; 165 | temp[9] = key[6]; 166 | 167 | key = temp; 168 | 169 | } 170 | 171 | 172 | public int[] getK1() 173 | { 174 | if(!flag) 175 | { 176 | Print.msg("\nError Occured: Keys are not generated yet "); 177 | return null; 178 | } 179 | return k1; 180 | } 181 | 182 | public int[] getK2() 183 | { 184 | if(!flag) 185 | { 186 | Print.msg("\nError Occured: Keys are not generated yet "); 187 | return null; 188 | } 189 | return k2; 190 | } 191 | 192 | } 193 | 194 | 195 | class Encryption 196 | { 197 | private int[] K1 = new int[8]; 198 | private int[] K2 = new int[8]; 199 | private int[] pt = new int[8]; 200 | 201 | void SaveParameters(String plaintext , int[] k1, int[] k2) 202 | { 203 | int[] pt = new int[8]; 204 | char c1; 205 | String ts ; 206 | 207 | try 208 | { 209 | for(int i=0;i<8;i++) 210 | { 211 | c1 = plaintext.charAt(i); 212 | ts = Character.toString(c1); 213 | pt[i] = Integer.parseInt(ts); 214 | 215 | if(pt[i] !=0 && pt[i]!=1) 216 | { 217 | Print.msg("\n .. Invalid Plaintext .."); 218 | System.exit(0); 219 | return ; 220 | } 221 | } 222 | } 223 | catch(Exception e) 224 | { 225 | Print.msg("\n .. Invalid Plaintext .. "); 226 | System.exit(0); 227 | return ; 228 | 229 | } 230 | 231 | this.pt = pt; 232 | 233 | Print.msg("Plaintext array : "); 234 | Print.array(this.pt,8); 235 | Print.msg("\n"); 236 | 237 | this.K1 = k1; 238 | this.K2 = k2; 239 | 240 | //Print.array(K1,8); 241 | //Print.msg("\n"); 242 | //Print.array(K2,8); 243 | } 244 | 245 | /** perform Initial Permutation in following manner [2 6 3 1 4 8 5 7] **/ 246 | void InitialPermutation() 247 | { 248 | int[] temp = new int[8]; 249 | 250 | temp[0] = pt[1]; 251 | temp[1] = pt[5]; 252 | temp[2] = pt[2]; 253 | temp[3] = pt[0]; 254 | temp[4] = pt[3]; 255 | temp[5] = pt[7]; 256 | temp[6] = pt[4]; 257 | temp[7] = pt[6]; 258 | 259 | pt = temp; 260 | 261 | Print.msg("Initial Permutaion(IP) : "); 262 | Print.array(this.pt,8); 263 | Print.msg("\n"); 264 | 265 | } 266 | void InverseInitialPermutation() 267 | { 268 | int[] temp = new int[8]; 269 | 270 | temp[0] = pt[3]; 271 | temp[1] = pt[0]; 272 | temp[2] = pt[2]; 273 | temp[3] = pt[4]; 274 | temp[4] = pt[6]; 275 | temp[5] = pt[1]; 276 | temp[6] = pt[7]; 277 | temp[7] = pt[5]; 278 | 279 | pt = temp; 280 | 281 | 282 | } 283 | 284 | /** mappingF . arguments 4-bit right-half of plaintext & 8-bit subkey **/ 285 | int[] mappingF(int[] R, int[] SK) 286 | { 287 | int[] temp = new int[8]; 288 | 289 | // EXPANSION/PERMUTATION [4 1 2 3 2 3 4 1] 290 | temp[0] = R[3]; 291 | temp[1] = R[0]; 292 | temp[2] = R[1]; 293 | temp[3] = R[2]; 294 | temp[4] = R[1]; 295 | temp[5] = R[2]; 296 | temp[6] = R[3]; 297 | temp[7] = R[0]; 298 | 299 | Print.msg("EXPANSION/PERMUTATION on RH : "); 300 | Print.array(temp,8); 301 | Print.msg("\n"); 302 | // Bit by bit XOR with sub-key 303 | temp[0] = temp[0] ^ SK[0]; 304 | temp[1] = temp[1] ^ SK[1]; 305 | temp[2] = temp[2] ^ SK[2]; 306 | temp[3] = temp[3] ^ SK[3]; 307 | temp[4] = temp[4] ^ SK[4]; 308 | temp[5] = temp[5] ^ SK[5]; 309 | temp[6] = temp[6] ^ SK[6]; 310 | temp[7] = temp[7] ^ SK[7]; 311 | 312 | Print.msg("XOR With Key : "); 313 | Print.array(temp,8); 314 | Print.msg("\n"); 315 | 316 | // S-Boxes 317 | final int[][] S0 = { {1,0,3,2} , {3,2,1,0} , {0,2,1,3} , {3,1,3,2} } ; 318 | final int[][] S1 = { {0,1,2,3}, {2,0,1,3}, {3,0,1,0}, {2,1,0,3}} ; 319 | 320 | 321 | int d11 = temp[0]; // first bit of first half 322 | int d14 = temp[3]; // fourth bit of first half 323 | 324 | int row1 = BinaryOp.BinToDec(d11,d14); // for input in s-box S0 325 | 326 | 327 | int d12 = temp[1]; // second bit of first half 328 | int d13 = temp[2]; // third bit of first half 329 | int col1 = BinaryOp.BinToDec(d12,d13); // for input in s-box S0 330 | 331 | 332 | int o1 = S0[row1][col1]; 333 | 334 | int[] out1 = BinaryOp.DecToBinArr(o1); 335 | 336 | Print.msg("S-Box S0: "); 337 | Print.array(out1,2); 338 | Print.msg("\n"); 339 | 340 | int d21 = temp[4]; // first bit of second half 341 | int d24 = temp[7]; // fourth bit of second half 342 | int row2 = BinaryOp.BinToDec(d21,d24); 343 | 344 | int d22 = temp[5]; // second bit of second half 345 | int d23 = temp[6]; // third bit of second half 346 | int col2 = BinaryOp.BinToDec(d22,d23); 347 | 348 | int o2 = S1[row2][col2]; 349 | 350 | int[] out2 = BinaryOp.DecToBinArr(o2); 351 | 352 | Print.msg("S-Box S1: "); 353 | Print.array(out2,2); 354 | Print.msg("\n"); 355 | 356 | //4 output bits from 2 s-boxes 357 | int[] out = new int[4]; 358 | out[0] = out1[0]; 359 | out[1] = out1[1]; 360 | out[2] = out2[0]; 361 | out[3] = out2[1]; 362 | 363 | //permutation P4 [2 4 3 1] 364 | 365 | int [] O_Per = new int[4]; 366 | O_Per[0] = out[1]; 367 | O_Per[1] = out[3]; 368 | O_Per[2] = out[2]; 369 | O_Per[3] = out[0]; 370 | 371 | Print.msg("Output of mappingF : "); 372 | Print.array(O_Per,4); 373 | Print.msg("\n"); 374 | 375 | return O_Per; 376 | } 377 | 378 | /** fK(L, R, SK) = (L (XOR) mappingF(R, SK), R) .. returns 8-bit output**/ 379 | int[] functionFk(int[] L, int[] R,int[] SK) 380 | { 381 | int[] temp = new int[4]; 382 | int[] out = new int[8]; 383 | 384 | 385 | temp = mappingF(R,SK); 386 | 387 | 388 | //XOR left half with output of mappingF 389 | out[0] = L[0] ^ temp[0]; 390 | out[1] = L[1] ^ temp[1]; 391 | out[2] = L[2] ^ temp[2]; 392 | out[3] = L[3] ^ temp[3]; 393 | 394 | out[4] = R[0]; 395 | out[5] = R[1]; 396 | out[6] = R[2]; 397 | out[7] = R[3]; 398 | 399 | 400 | return out; 401 | 402 | 403 | } /** switch function (SW) interchanges the left and right 4 bits **/ 404 | int[] switchSW(int[] in) 405 | { 406 | 407 | int[] temp = new int[8]; 408 | 409 | temp[0] = in[4]; 410 | temp[1] = in[5]; 411 | temp[2] = in[6]; 412 | temp[3] = in[7]; 413 | 414 | temp[4] = in[0]; 415 | temp[5] = in[1]; 416 | temp[6] = in[2]; 417 | temp[7] = in[3]; 418 | 419 | return temp; 420 | } 421 | 422 | int[] encrypt(String plaintext , int[] LK, int[] RK) 423 | { 424 | 425 | 426 | SaveParameters(plaintext,LK,RK); 427 | 428 | Print.msg("\n---------------------------------------\n"); 429 | InitialPermutation(); 430 | Print.msg("\n---------------------------------------\n"); 431 | //saperate left half & right half from 8-bit pt 432 | int[] LH = new int[4]; 433 | int[] RH = new int[4]; 434 | LH[0] = pt[0]; 435 | LH[1] = pt[1]; 436 | LH[2] = pt[2]; 437 | LH[3] = pt[3]; 438 | 439 | 440 | RH[0] = pt[4]; 441 | RH[1] = pt[5]; 442 | RH[2] = pt[6]; 443 | RH[3] = pt[7]; 444 | 445 | 446 | Print.msg("First Round LH : "); 447 | Print.array(LH,4); 448 | Print.msg("\n"); 449 | 450 | Print.msg("First Round RH: "); 451 | Print.array(RH,4); 452 | Print.msg("\n"); 453 | 454 | //first round with sub-key K1 455 | int[] r1 = new int[8]; 456 | r1 = functionFk(LH,RH,K1); 457 | 458 | Print.msg("After First Round : "); 459 | Print.array(r1,8); 460 | Print.msg("\n"); 461 | Print.msg("\n---------------------------------------\n"); 462 | //Switch the left half & right half of about output 463 | int[] temp = new int[8]; 464 | temp = switchSW(r1); 465 | 466 | Print.msg("After Switch Function : "); 467 | Print.array(temp,8); 468 | Print.msg("\n"); 469 | Print.msg("\n---------------------------------------\n"); 470 | // again saperate left half & right half for second round 471 | LH[0] = temp[0]; 472 | LH[1] = temp[1]; 473 | LH[2] = temp[2]; 474 | LH[3] = temp[3]; 475 | 476 | RH[0] = temp[4]; 477 | RH[1] = temp[5]; 478 | RH[2] = temp[6]; 479 | RH[3] = temp[7]; 480 | 481 | 482 | Print.msg("Second Round LH : "); 483 | Print.array(LH,4); 484 | Print.msg("\n"); 485 | 486 | Print.msg("Second Round RH: "); 487 | Print.array(RH,4); 488 | Print.msg("\n"); 489 | 490 | 491 | //second round with sub-key K2 492 | int[] r2 = new int[8]; 493 | r2 = functionFk(LH,RH,K2); 494 | 495 | pt = r2; 496 | 497 | Print.msg("After Second Round : "); 498 | Print.array(this.pt,8); 499 | Print.msg("\n"); 500 | Print.msg("\n---------------------------------------\n"); 501 | 502 | InverseInitialPermutation(); 503 | Print.msg("After Inverse IP (Result) : "); 504 | Print.array(this.pt,8); 505 | Print.msg("\n"); 506 | 507 | //Encryption done... return 8-bit output . 508 | return pt; 509 | 510 | 511 | 512 | 513 | } 514 | 515 | } 516 | 517 | 518 | public class SDES 519 | { 520 | public static void main(String[] args) 521 | { 522 | 523 | KeyGeneration KG = new KeyGeneration(); 524 | Encryption enc = new Encryption(); 525 | Scanner sc = new Scanner(System.in); 526 | 527 | String pt ; 528 | String key; 529 | int[] ct = new int[8]; 530 | 531 | try 532 | { 533 | 534 | 535 | //Ex Input : 10101010 536 | System.out.print("Enter 8-bit Plaintext : "); 537 | pt = sc.next(); 538 | 539 | 540 | System.out.println(" \n "); 541 | 542 | //Ex Input : 1010000010 543 | System.out.print("Enter 10-bit Key : "); 544 | key = sc.next(); 545 | 546 | 547 | System.out.println(" \n "); 548 | 549 | Print.msg("\n Key Generation ...\n"); 550 | Print.msg("\n---------------------------------------\n"); 551 | KG.GenerateKeys(key); 552 | Print.msg("\n---------------------------------------\n"); 553 | ct = enc.encrypt( pt ,KG.getK1(),KG.getK2()); 554 | 555 | Print.msg("\n---------------------------------------\n"); 556 | System.out.println(" \n Decryption "); 557 | 558 | //Ex Input : 10001101 559 | System.out.print("Enter 8-bit Ciphertext : "); 560 | pt = sc.next(); 561 | 562 | 563 | System.out.println(" \n "); 564 | 565 | //Ex Input : 1010000010 566 | System.out.print("Enter 10-bit Key : "); 567 | key = sc.next(); 568 | 569 | 570 | System.out.println(" \n "); 571 | 572 | Print.msg("\n Key Generation ...\n"); 573 | Print.msg("\n---------------------------------------\n"); 574 | Print.msg("\n For decryption Two Sub-keys will be used in reverse order \n"); 575 | Print.msg("\n---------------------------------------\n\n"); 576 | KG.GenerateKeys(key); 577 | Print.msg("\n---------------------------------------\n"); 578 | 579 | ct = enc.encrypt( pt ,KG.getK2(),KG.getK1()); 580 | 581 | Print.msg("\n---------------------------------------\n"); 582 | 583 | 584 | 585 | 586 | 587 | } 588 | catch(InputMismatchException e) 589 | { 590 | System.out.println("-- Error Occured : Invalid Input "); 591 | } 592 | catch(Exception e) 593 | { 594 | System.out.println("-- Error Occured : "+e); 595 | } 596 | 597 | } 598 | 599 | } 600 | 601 | 602 | /** Class to print Strings & arrays shortly **/ 603 | class Print 604 | { 605 | /** Prints array to console **/ 606 | static void array(int[] arr,int len) 607 | { 608 | System.out.print(" - "); 609 | 610 | for(int i=0;i=0;i--) 633 | { 634 | temp = temp + (bits[i]*base); 635 | base = base * 2 ; 636 | } 637 | 638 | return temp; 639 | } 640 | 641 | /** gets decimal number as argument and returns array of binary bits 642 | for example input arg [10] will return [1,0,1,0]**/ 643 | static int[] DecToBinArr(int no) 644 | { 645 | // 13 1 646 | // 6 0 647 | // 3 1 648 | // 1 1 649 | // 0 650 | 651 | 652 | if(no==0) 653 | { 654 | int[] zero = new int[2]; 655 | zero[0] = 0; 656 | zero[1] = 0; 657 | return zero; 658 | } 659 | int[] temp = new int[10] ; 660 | 661 | 662 | int count = 0 ; 663 | for(int i= 0 ; no!= 0 ; i++) 664 | { 665 | temp[i] = no % 2; 666 | no = no/2; 667 | count++; 668 | } 669 | 670 | 671 | int[] temp2 = new int[count]; 672 | 673 | 674 | for(int i=count-1, j=0;i>=0 && j" 74 | ] 75 | }, 76 | "metadata": { 77 | "needs_background": "light" 78 | }, 79 | "output_type": "display_data" 80 | } 81 | ], 82 | "source": [ 83 | "train_tweets['length'] = train_tweets['tweet'].apply(len)\n", 84 | "fig1 = sns.barplot('label','length',data = train_tweets,palette='PRGn')\n", 85 | "plt.title('Average Word Length vs label')\n", 86 | "plot = fig1.get_figure()\n", 87 | "plot.savefig('Barplot.png')" 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": 92, 93 | "metadata": {}, 94 | "outputs": [ 95 | { 96 | "data": { 97 | "image/png": "iVBORw0KGgoAAAANSUhEUgAAAZUAAAEWCAYAAACufwpNAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAFqVJREFUeJzt3X/wXXV95/HnywD+qCIgUTEBw2rqGG2LkgWqna6KA4EdG23BBWvJKDvpdGGrU7crOrOLBdnRVusWq8ymYwqoFVmpEttUNstqHVcFglIgUIYUUSIIwQBiqSj43j/u5yt3w01yEz73e/P1+3zMnLn3vM/nnPM5mZAX53zOPSdVhSRJPTxp2h2QJP38MFQkSd0YKpKkbgwVSVI3hookqRtDRZLUjaEijSnJl5L8+9leV5pLDBXNO0luT/LaafdjWJJfTPI/k9yb5IEk1yf5gyQLJrzfC5O8d5L70PxiqEhTluQFwFXAHcAvVdUzgZOB5cAzptk3aXcZKlKT5MAkf5Nka5L72vfF2zV7QZKr29nE5UkOGlr/mCRfTXJ/kn9I8qoxd/1HwFer6g+q6i6Aqrqlqt5UVfe3bf9Gkk1t219K8uKh/VaSFw7N/+zsI8mrkmxJ8o4k9yS5K8lb2rLVwG8D/znJD5N8vtXfmeS7SR5MckuSY3f3z1Lzl6EiPeZJwF8CzwcOA/4F+PPt2pwGv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hw7NLwbunFJfJGnem+uhcg2wNMnhSfYDTgHWTblPkjRvzenLX1X1SJIzgSuABcDaqto05W7NJ15S1N7Mv59TkKrHDUFIkrRH5vrlL0nSXsRQkSR1Y6hoj/h4HO2tkqxNck+SG6fdl/nIUNFu8/E42stdCKyYdifmK0NFe8LH42ivVVVfBrZNux/zlaGiPTHq8TiLptQXSXsRQ0V7YqzH40iafwwV7QkfjyNpJENFe8LH40gayVDRbquqR4CZx+PcDFzq43G0t0jyKeBrwIuSbEly+rT7NJ/4mBZJUjeeqUiSujFUJEndGCqSpG4MFUlSN4aKJKkbQ0WaoCQ/3MXyJbv7NN0kFyY56Yn1TJoMQ0WS1I2hIs2CJE9PcmWSbyS5IcnwU533SXJRkuuTfCbJ09o6Ryb5+yTXJrkiySFT6r40NkNFmh0/At5QVS8HXg18MMnMgzlfBKypql8GfgD8hyT7Ah8GTqqqI4G1wHlT6Le0W/aZdgekeSLAf0vy68BPGbwq4Dlt2R1V9X/b908Avw98AXgpsKFlzwLgrlntsbQHDBVpdvw2sBA4sqp+kuR24Clt2fbPSioGIbSpqn519rooPXFe/pJmxzOBe1qgvBp4/tCyw5LMhMepwFeAW4CFM/Uk+yZ5yaz2WNoDhoo0Oz4JLE+ykcFZyz8OLbsZWJXkeuAg4IL2muaTgPcn+QfgOuAVs9xnabf5lGJJUjeeqUiSujFUJEndGCqSpG4MFUlSN4aKJKkbQ0WS1I2hIknq5v8B97ZRUEKC0VcAAAAASUVORK5CYII=\n", 98 | "text/plain": [ 99 | "
" 100 | ] 101 | }, 102 | "metadata": { 103 | "needs_background": "light" 104 | }, 105 | "output_type": "display_data" 106 | } 107 | ], 108 | "source": [ 109 | "fig2 = sns.countplot(x= 'label',data = train_tweets)\n", 110 | "plt.title('Label Counts')\n", 111 | "plot = fig2.get_figure()\n", 112 | "plot.savefig('Count Plot.png')" 113 | ] 114 | }, 115 | { 116 | "cell_type": "markdown", 117 | "metadata": {}, 118 | "source": [ 119 | "## Feature Engineering" 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": 77, 125 | "metadata": {}, 126 | "outputs": [], 127 | "source": [ 128 | "def text_processing(tweet):\n", 129 | " \n", 130 | " #Generating the list of words in the tweet (hastags and other punctuations removed)\n", 131 | " def form_sentence(tweet):\n", 132 | " tweet_blob = TextBlob(tweet)\n", 133 | " return ' '.join(tweet_blob.words)\n", 134 | " new_tweet = form_sentence(tweet)\n", 135 | " \n", 136 | " #Removing stopwords and words with unusual symbols\n", 137 | " def no_user_alpha(tweet):\n", 138 | " tweet_list = [ele for ele in tweet.split() if ele != 'user']\n", 139 | " clean_tokens = [t for t in tweet_list if re.match(r'[^\\W\\d]*$', t)]\n", 140 | " clean_s = ' '.join(clean_tokens)\n", 141 | " clean_mess = [word for word in clean_s.split() if word.lower() not in stopwords.words('english')]\n", 142 | " return clean_mess\n", 143 | " no_punc_tweet = no_user_alpha(new_tweet)\n", 144 | " \n", 145 | " #Normalizing the words in tweets \n", 146 | " def normalization(tweet_list):\n", 147 | " lem = WordNetLemmatizer()\n", 148 | " normalized_tweet = []\n", 149 | " for word in tweet_list:\n", 150 | " normalized_text = lem.lemmatize(word,'v')\n", 151 | " normalized_tweet.append(normalized_text)\n", 152 | " return normalized_tweet\n", 153 | " \n", 154 | " \n", 155 | " return normalization(no_punc_tweet)" 156 | ] 157 | }, 158 | { 159 | "cell_type": "code", 160 | "execution_count": 79, 161 | "metadata": {}, 162 | "outputs": [], 163 | "source": [ 164 | "train_tweets['tweet_list'] = train_tweets['tweet'].apply(text_processing)\n", 165 | "test_tweets['tweet_list'] = test_tweets['tweet'].apply(text_processing)" 166 | ] 167 | }, 168 | { 169 | "cell_type": "code", 170 | "execution_count": 81, 171 | "metadata": {}, 172 | "outputs": [ 173 | { 174 | "data": { 175 | "text/html": [ 176 | "
\n", 177 | "\n", 190 | "\n", 191 | " \n", 192 | " \n", 193 | " \n", 194 | " \n", 195 | " \n", 196 | " \n", 197 | " \n", 198 | " \n", 199 | " \n", 200 | " \n", 201 | " \n", 202 | " \n", 203 | " \n", 204 | " \n", 205 | " \n", 206 | " \n", 207 | " \n", 208 | " \n", 209 | " \n", 210 | " \n", 211 | " \n", 212 | " \n", 213 | " \n", 214 | " \n", 215 | " \n", 216 | " \n", 217 | " \n", 218 | " \n", 219 | " \n", 220 | " \n", 221 | " \n", 222 | " \n", 223 | " \n", 224 | " \n", 225 | " \n", 226 | " \n", 227 | " \n", 228 | " \n", 229 | " \n", 230 | " \n", 231 | " \n", 232 | " \n", 233 | " \n", 234 | " \n", 235 | " \n", 236 | " \n", 237 | "
labellengthtweet_sentencetweet_list
13174[cnn, call, michigan, middle, school, wall, ch...[cnn, call, michigan, middle, school, wall, ch...
141101[comment, australia, opkillingbay, seashepherd...[comment, australia, opkillingbay, seashepherd...
17122[retweet, agree][retweet, agree]
23147[lumpy, say, prove, lumpy][lumpy, say, prove, lumpy]
341104[unbelievable, century, need, something, like,...[unbelievable, century, need, something, like,...
\n", 238 | "
" 239 | ], 240 | "text/plain": [ 241 | " label length tweet_sentence \\\n", 242 | "13 1 74 [cnn, call, michigan, middle, school, wall, ch... \n", 243 | "14 1 101 [comment, australia, opkillingbay, seashepherd... \n", 244 | "17 1 22 [retweet, agree] \n", 245 | "23 1 47 [lumpy, say, prove, lumpy] \n", 246 | "34 1 104 [unbelievable, century, need, something, like,... \n", 247 | "\n", 248 | " tweet_list \n", 249 | "13 [cnn, call, michigan, middle, school, wall, ch... \n", 250 | "14 [comment, australia, opkillingbay, seashepherd... \n", 251 | "17 [retweet, agree] \n", 252 | "23 [lumpy, say, prove, lumpy] \n", 253 | "34 [unbelievable, century, need, something, like,... " 254 | ] 255 | }, 256 | "execution_count": 81, 257 | "metadata": {}, 258 | "output_type": "execute_result" 259 | } 260 | ], 261 | "source": [ 262 | "train_tweets[train_tweets['label']==1].drop('tweet',axis=1).head()" 263 | ] 264 | }, 265 | { 266 | "cell_type": "markdown", 267 | "metadata": {}, 268 | "source": [ 269 | "## Model Selection and Machine Learning" 270 | ] 271 | }, 272 | { 273 | "cell_type": "code", 274 | "execution_count": 83, 275 | "metadata": {}, 276 | "outputs": [], 277 | "source": [ 278 | "X = train_tweets['tweet']\n", 279 | "y = train_tweets['label']\n", 280 | "test = test_tweets['tweet']" 281 | ] 282 | }, 283 | { 284 | "cell_type": "code", 285 | "execution_count": 84, 286 | "metadata": {}, 287 | "outputs": [], 288 | "source": [ 289 | "from sklearn.model_selection import train_test_split\n", 290 | "msg_train, msg_test, label_train, label_test = train_test_split(train_tweets['tweet'], train_tweets['label'], test_size=0.3)" 291 | ] 292 | }, 293 | { 294 | "cell_type": "code", 295 | "execution_count": 85, 296 | "metadata": {}, 297 | "outputs": [ 298 | { 299 | "data": { 300 | "text/plain": [ 301 | "Pipeline(memory=None,\n", 302 | " steps=[('bow', CountVectorizer(analyzer=,\n", 303 | " binary=False, decode_error='strict', dtype=,\n", 304 | " encoding='utf-8', input='content', lowercase=True, max_df=1.0,\n", 305 | " max_features=None, min_df=1, ngram_range=(1, 1), preprocessor=Non...f=False, use_idf=True)), ('classifier', MultinomialNB(alpha=1.0, class_prior=None, fit_prior=True))])" 306 | ] 307 | }, 308 | "execution_count": 85, 309 | "metadata": {}, 310 | "output_type": "execute_result" 311 | } 312 | ], 313 | "source": [ 314 | "pipeline = Pipeline([\n", 315 | " ('bow',CountVectorizer(analyzer=text_processing)), # strings to token integer counts\n", 316 | " ('tfidf', TfidfTransformer()), # integer counts to weighted TF-IDF scores\n", 317 | " ('classifier', MultinomialNB()), # train on TF-IDF vectors w/ Naive Bayes classifier\n", 318 | "])\n", 319 | "pipeline.fit(msg_train,label_train)" 320 | ] 321 | }, 322 | { 323 | "cell_type": "code", 324 | "execution_count": 86, 325 | "metadata": {}, 326 | "outputs": [ 327 | { 328 | "name": "stdout", 329 | "output_type": "stream", 330 | "text": [ 331 | " precision recall f1-score support\n", 332 | "\n", 333 | " 0 1.00 0.94 0.97 6321\n", 334 | " 1 0.15 1.00 0.27 72\n", 335 | "\n", 336 | " micro avg 0.94 0.94 0.94 6393\n", 337 | " macro avg 0.58 0.97 0.62 6393\n", 338 | "weighted avg 0.99 0.94 0.96 6393\n", 339 | "\n", 340 | "\n", 341 | "\n", 342 | "[[5927 394]\n", 343 | " [ 0 72]]\n", 344 | "0.9383700922884405\n" 345 | ] 346 | } 347 | ], 348 | "source": [ 349 | "predictions = pipeline.predict(msg_test)\n", 350 | "\n", 351 | "print(classification_report(predictions,label_test))\n", 352 | "print ('\\n')\n", 353 | "print(confusion_matrix(predictions,label_test))\n", 354 | "print(accuracy_score(predictions,label_test))" 355 | ] 356 | } 357 | ], 358 | "metadata": { 359 | "kernelspec": { 360 | "display_name": "Python 3", 361 | "language": "python", 362 | "name": "python3" 363 | }, 364 | "language_info": { 365 | "codemirror_mode": { 366 | "name": "ipython", 367 | "version": 3 368 | }, 369 | "file_extension": ".py", 370 | "mimetype": "text/x-python", 371 | "name": "python", 372 | "nbconvert_exporter": "python", 373 | "pygments_lexer": "ipython3", 374 | "version": "3.7.4" 375 | } 376 | }, 377 | "nbformat": 4, 378 | "nbformat_minor": 4 379 | } 380 | --------------------------------------------------------------------------------