Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Embedded Systems Final Journal
- Practical 1
- Aim: Elevator using 8051
- Solution:
- Code-
- #include <reg51.h>
- sbit floorg=P1^0;
- sbit floor1=P1^1;
- sbit floor2=P1^2;
- sbit floor3=P1^3;
- static int current_floor=0;
- static int req_floor;
- void MsDelay(unsigned int values)
- {
- unsigned int x,y;
- for(x=0;x<1275;x++);
- for(y=0;y<values;y++);
- }
- void userinput()
- {
- if(floorg==0)
- {
- req_floor=0;
- }
- else if(floor1==0)
- {
- req_floor=1;
- }
- else if(floor2==0)
- {
- req_floor=2;
- }
- else if(floor3==0)
- {
- req_floor=3;
- }
- }
- void reqresponse(int req_floor)
- {
- unsigned int digi_val[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
- unsigned int bin[]={0x01,0x03,0x02,0x06,0x04,0x06,0x08,0x09};
- unsigned int i;
- if(req_floor==current_floor)
- {
- P2=digi_val[current_floor];
- P0=0x00;
- }
- else if(req_floor > current_floor)
- {
- int diff=req_floor-current_floor;
- P2=digi_val[current_floor+diff];
- MsDelay(200);
- current_floor=req_floor;
- for(i=8;i>0;i--)
- {
- P0=bin[i];
- MsDelay(7);
- }
- }
- else if(req_floor < current_floor)
- {
- int diff=current_floor-req_floor;
- P2=digi_val[current_floor-diff];
- MsDelay(200);
- current_floor=req_floor;
- for(i=0;i<8;i++)
- {
- P0=bin[i];
- MsDelay(7);
- }
- }
- }
- void main()
- {
- while(1)
- {
- reqresponse(req_floor);
- userinput();
- }
- }
- Design-
- Working-
- Practical 2 Design Traffic Simulator
- Code:
- #### Traffic Light #####
- #include<reg51.h>
- sbit R1=P1^0;
- sbit Y1=P1^1;
- sbit G1=P1^2;
- sbit R2=P1^3;
- sbit Y2=P1^4;
- sbit G2=P1^5;
- sbit R3=P2^0;
- sbit Y3=P2^1;
- sbit G3=P2^2;
- sbit R4=P2^3;
- sbit Y4=P2^4;
- sbit G4=P2^5;
- void msdelay(unsigned int t)
- {
- int i,j;
- int k=100*t;
- for(i=0;i<k;i++)
- for(j=0;j<1275;j++);
- }
- void clear()
- {
- R1=0;
- R2=0;
- R3=0;
- R4=0;
- Y1=0;
- Y2=0;
- Y3=0;
- Y4=0;
- G1=0;
- G2=0;
- G3=0;
- G4=0;
- }
- void phase1()
- {
- R1=1;
- R2=1;
- G3=1;
- R4=1;
- msdelay(25);
- Y3=1;
- Y1=1;
- msdelay(5);
- }
- void phase2()
- {
- G1=1;
- R2=1;
- R3=1;
- R4=1;
- msdelay(25);
- Y2=1;
- Y1=1;
- Y4=1;
- msdelay(5);
- }
- void phase3()
- {
- R1=1;
- G2=1;
- R3=1;
- G4=1;
- msdelay(25);
- Y2=1;
- Y4=1;
- msdelay(5);
- }
- void phase4()
- {
- R1=1;
- G2=1;
- R3=1;
- R4=1;
- msdelay(25);
- Y1=1;
- Y2=1;
- Y3=1;
- msdelay(5);
- }
- void phase5()
- {
- G1=1;
- R2=1;
- G3=1;
- R4=1;
- msdelay(25);
- Y1=1;
- Y3=1;
- Y4=1;
- msdelay(5);
- }
- void phase6()
- {
- R1=1;
- R2=1;
- R3=1;
- G4=1;
- msdelay(25);
- Y3=1;
- Y4=1;
- msdelay(5);
- }
- void main()
- {
- P1=0x00;
- P2=0x00;
- while(1)
- {
- phase1();
- clear();
- phase2();
- clear();
- phase3();
- clear();
- phase4();
- clear();
- phase5();
- clear();
- phase6();
- clear();
- } }
- Design:
- Simulation:
- Practical No:-3
- Analog to Digital Convertor
-
- Main.c
- #include "Includes.h"
- // Main function
- void main()
- {
- unsigned char ADC_Value = 0; // To capture ADC value
- unsigned char Digit[3] = { 0,0,0 }; // To make digits to display on LCD
- InitADC(); // Initialize ADC
- InitLCD(); // Initialize LCD display
- WriteStringToLCD("Starting..."); // Show Welcome Message
- delay_sec(1); // 1 Sec delay
- ClearLCDScreen(); // Clear LCD
- while(1)
- {
- ADC_Value = ReadADC(AN1); // Read ADC value from Channel 1
- Digit[2] = (unsigned char)( ADC_Value/100); // Find out first digit
- Digit[1] = (unsigned char)( ADC_Value/10) - Digit[2]*10; // Find out second digit
- Digit[0] = ADC_Value - Digit[2]*100 - Digit[1]*10; // Find out third digit
- ClearLCDScreen(); // Clear LCD
- WriteStringToLCD("ADC value = "); // Display string
- WriteDataToLCD(Digit[2]+0x30); // Display first digit
- WriteDataToLCD(Digit[1]+0x30); // Display second digit
- WriteDataToLCD(Digit[0]+0x30); // Display third digit
- delay_sec(1); // Delay of one second
- }
- }
- LED.c
- #include "Includes.h"
- // Function Purpose: Produce approximate delay in Secs.
- void delay_sec(unsigned int d)
- {
- unsigned int i;
- for(i=0;i<(d*20);i++)
- __delay_us(50000);
- }
- // Function Purpose: Produce approximate delay in given uSecs.
- void __delay_us(unsigned int d)
- {
- unsigned int i, limit;
- limit = d/15;
- for(i=0;i<limit;i++);
- }
- void ToggleEpinOfLCD(void)
- {
- E = 1; // Give a pulse on E pin
- __delay_us(E_Delay); // so that LCD can latch the
- E = 0; // data from data bus
- __delay_us(E_Delay);
- }
- void WriteDataToLCD(char t)
- {
- RS = 1; // This is data
- P1 &= 0x0F; // Make P1.4 to P1.7 zero
- P1 |= (t&0xF0); // Write Upper nibble of data
- ToggleEpinOfLCD(); // Toggle E pin to send data
- P1 &= 0x0F; // Make P1.4 to P1.7 zero
- P1 |= ((t<<4)&0xF0);// Write Lower nibble of data
- ToggleEpinOfLCD(); // Toggle E pin to send data
- }
- void WriteCommandToLCD(int z)
- {
- RS = 0; // This is command
- P1 &= 0x0F; // Make P1.4 to P1.7 zero
- P1 |= (z&0xF0); // Write Upper nibble of data
- ToggleEpinOfLCD(); // Toggle E pin to send data
- P1 &= 0x0F; // Make P1.4 to P1.7 zero
- P1 |= ((z<<4)&0xF0);// Write Lower nibble of data
- ToggleEpinOfLCD(); // Toggle E pin to send data
- }
- void InitLCD(void)
- {
- RS = 0; // Make pin zero
- E = 0; // Make Pin zero
- ///////////// Reset process from datasheet /////////
- __delay_us(15000);
- P1 &= 0x0F; // Make P1.4 to P1.7 zero
- P1 |= 0x30; // Write 0x3
- ToggleEpinOfLCD(); // Toggle E pin to send data
- __delay_us(4500);
- P1 &= 0x0F; // Make P1.4 to P1.7 zero
- P1 |= 0x30; // Write 0x3
- ToggleEpinOfLCD(); // Toggle E pin to send data
- __delay_us(300);
- P1 &= 0x0F; // Make P1.4 to P1.7 zero
- P1 |= 0x30; // Write 0x3
- ToggleEpinOfLCD(); // Toggle E pin to send data
- __delay_us(650);
- P1 &= 0x0F; // Make P1.4 to P1.7 zero
- P1 |= 0x20; // Write 0x2
- ToggleEpinOfLCD(); // Toggle E pin to send data
- __delay_us(650);
- /////////////////////////////////////////////////////
- WriteCommandToLCD(0x28); //function set
- WriteCommandToLCD(0x0c); //display on,cursor off,blink off
- WriteCommandToLCD(0x01); //clear display
- WriteCommandToLCD(0x06); //entry mode, set increment
- }
- void WriteStringToLCD(const char *s)
- {
- while(*s)
- WriteDataToLCD(*s++); // print first character on LCD
- }
- void ClearLCDScreen(void) // Clear the Screen and return cursor to zero position
- {
- WriteCommandToLCD(0x01); // Clear the screen
- __delay_us(2000); // Delay for cursor to return at zero position
- }
- ADC0808.c
- #include "Includes.h"
- void InitADC(void)
- {
- Add_A = 0; // Make output
- Add_B = 0; // Make output
- Add_C = 0; // Make output
- ALE = 0; // Make output
- EOC = 1; // Make input
- OE = 0; // Make output
- START = 0; // Make output
- CLK = 0; // Make output
- Data_Bus = 0xFF; // Make Inputs
- }
- unsigned char ReadADC(unsigned char Channel)
- {
- unsigned int i = 0;
- unsigned int ADC_value = 0;
- // Select Channel
- switch(Channel)
- {
- case AN0: Add_C = 0; Add_B = 0; Add_A = 0; break;
- case AN1: Add_C = 0; Add_B = 0; Add_A = 1; break;
- case AN2: Add_C = 0; Add_B = 1; Add_A = 0; break;
- case AN3: Add_C = 0; Add_B = 1; Add_A = 1; break;
- case AN4: Add_C = 1; Add_B = 0; Add_A = 0; break;
- case AN5: Add_C = 1; Add_B = 0; Add_A = 1; break;
- case AN6: Add_C = 1; Add_B = 1; Add_A = 0; break;
- case AN7: Add_C = 1; Add_B = 1; Add_A = 1; break;
- }
- __delay_us(HalfCycleDelay); // 250kHz Frequency
- ALE = 1; // Enable Address Latch
- CLK = 1; // Make CLK High
- __delay_us(HalfCycleDelay); // 250kHz Frequency
- CLK = 0; // Make CLK Low
- START = 1; // Start ADC Conversion
- __delay_us(HalfCycleDelay); // 250kHz Frequency
- CLK = 1; // Make CLK High
- ALE = 0; // Disable Address Latch
- __delay_us(HalfCycleDelay); // 250kHz Frequency
- CLK = 0; // Make CLK Low
- START = 0; // Complete the start pulse
- for(i=0;i<2000;i++)
- {
- CLK = !CLK; // Toggle Clock
- __delay_us(HalfCycleDelay); // 250kHz Frequency
- if(!EOC) // Wait for EOC to be low
- break;
- }
- for(i=0;i<2000;i++)
- {
- CLK = !CLK; // Toggle Clock
- __delay_us(HalfCycleDelay); // 250kHz Frequency
- if(EOC) // Wait for EOC to be High
- break;
- }
- CLK = 0; // Make CLK Low
- OE = 1; // Enable Output
- __delay_us(HalfCycleDelay); // 250kHz Frequency
- CLK = 1; // Make CLK High
- __delay_us(HalfCycleDelay); // 250kHz Frequency
- CLK = 0; // Make CLK Low
- __delay_us(HalfCycleDelay); // 250kHz Frequency
- CLK = 1; // Make CLK High
- ADC_value = Data_Bus; // Read value
- __delay_us(HalfCycleDelay); // 250kHz Frequency
- OE = 0; // Disable Output
- CLK = 0; // Make CLK Low
- __delay_us(HalfCycleDelay); // 250kHz Frequency
- return ADC_value; // Return ADC value
- }
- ADC0808.h
- #ifndef __ADC0808_H
- #define __ADC0808_H
- // Define ADC Channels
- #define AN0 0
- #define AN1 1
- #define AN2 2
- #define AN3 3
- #define AN4 4
- #define AN5 5
- #define AN6 6
- #define AN7 7
- // Define Pins
- sbit Add_A = P2^0; // Address Pin A
- sbit Add_B = P2^1; // Address Pin B
- sbit Add_C = P2^2; // Address Pin C
- sbit ALE = P2^3; // Address Latch Enable
- sbit EOC = P2^4; // End Of Conversion
- sbit OE = P2^5; // Output Enable
- sbit START = P2^6; // Start Conversion
- sbit CLK = P2^7; // Clock for AD0808
- // Define Data Bus
- #define Data_Bus P0
- #define HalfCycleDelay 10 // usecs
- // Function Declarations
- void InitADC(void);
- unsigned char ReadADC(unsigned char);
- #endif
- Includes.h
- #ifndef __INCLUDES_H
- #define __INCLUDES_H
- #include<reg51.h>
- #include "LCD.h"
- #include "ADC0808.h"
- #endif
- LCD.h
- #ifndef __LCD_H
- #define __LCD_H
- //*******************
- //Pin description
- /*
- P1.4 to P1.7 is data bus
- P1.1 is RS
- P1.0 is E
- */
- //********************
- // Defines Pins
- sbit RS = P1^1;
- sbit E = P1^0;
- // Constants
- #define E_Delay 250
- // Function Declarations
- void delay_sec(unsigned int);
- void __delay_us(unsigned int);
- void InitLCD(void);
- void WriteCommandToLCD(int);
- void WriteDataToLCD(char);
- void WriteStringToLCD(const char*);
- void ClearLCDScreen(void);
- #endif
- reg51.h
- /*--------------------------------------------------------------------------
- REG51.H
- Header file for generic 80C51 and 80C31 microcontroller.
- Copyright (c) 1988-2002 Keil Elektronik GmbH and Keil Software, Inc.
- All rights reserved.
- --------------------------------------------------------------------------*/
- #ifndef __REG51_H__
- #define __REG51_H__
- /* BYTE Register */
- sfr P0 = 0x80;
- sfr P1 = 0x90;
- sfr P2 = 0xA0;
- sfr P3 = 0xB0;
- sfr PSW = 0xD0;
- sfr ACC = 0xE0;
- sfr B = 0xF0;
- sfr SP = 0x81;
- sfr DPL = 0x82;
- sfr DPH = 0x83;
- sfr PCON = 0x87;
- sfr TCON = 0x88;
- sfr TMOD = 0x89;
- sfr TL0 = 0x8A;
- sfr TL1 = 0x8B;
- sfr TH0 = 0x8C;
- sfr TH1 = 0x8D;
- sfr IE = 0xA8;
- sfr IP = 0xB8;
- sfr SCON = 0x98;
- sfr SBUF = 0x99;
- /* BIT Register */
- /* PSW */
- sbit CY = 0xD7;
- sbit AC = 0xD6;
- sbit F0 = 0xD5;
- sbit RS1 = 0xD4;
- sbit RS0 = 0xD3;
- sbit OV = 0xD2;
- sbit P = 0xD0;
- /* TCON */
- sbit TF1 = 0x8F;
- sbit TR1 = 0x8E;
- sbit TF0 = 0x8D;
- sbit TR0 = 0x8C;
- sbit IE1 = 0x8B;
- sbit IT1 = 0x8A;
- sbit IE0 = 0x89;
- sbit IT0 = 0x88;
- /* IE */
- sbit EA = 0xAF;
- sbit ES = 0xAC;
- sbit ET1 = 0xAB;
- sbit EX1 = 0xAA;
- sbit ET0 = 0xA9;
- sbit EX0 = 0xA8;
- /* IP */
- sbit PS = 0xBC;
- sbit PT1 = 0xBB;
- sbit PX1 = 0xBA;
- sbit PT0 = 0xB9;
- sbit PX0 = 0xB8;
- /* P3 */
- sbit RD = 0xB7;
- sbit WR = 0xB6;
- sbit T1 = 0xB5;
- sbit T0 = 0xB4;
- sbit INT1 = 0xB3;
- sbit INT0 = 0xB2;
- sbit TXD = 0xB1;
- sbit RXD = 0xB0;
- /* SCON */
- sbit SM0 = 0x9F;
- sbit SM1 = 0x9E;
- sbit SM2 = 0x9D;
- sbit REN = 0x9C;
- sbit TB8 = 0x9B;
- sbit RB8 = 0x9A;
- sbit TI = 0x99;
- sbit RI = 0x98;
- #endif
- KEIL
- Proteous
- Practical 4
- Stepper Motor
- Code -
- #include<reg51.h>
- void MSDelay(unsigned int value);
- void main()
- {
- unsigned int i,j;
- const char bin[]= {0x01, 0x02, 0x04, 0x08};
- P1 = 0x00;
- while(1)
- {
- if(P1^4)
- {
- for(i=0;i<4;i++)
- {
- P1 = bin[i];
- MSDelay(2);
- }
- }
- else
- P1 = 0x00;
- }
- }
- void MSDelay(unsigned int value)
- {
- unsigned int x,y;
- for(x=0; x<1200; x++)
- for(y=0; y<value; y++);
- }
- Circuit -
- Practical No 5
- Create a push button interface
- Code:
- #include<reg51.h>
- void delay(int dly)
- {
- int i,j;
- for(i=0;i<=dly;i++)
- for(j=0;j<=500;j++)
- {};
- }
- sbit l1=P2^0;
- sbit l2=P2^1;
- sbit l3=P2^2;
- sbit l4=P2^3;
- sbit l5=P2^4;
- sbit l6=P2^5;
- sbit l7=P2^6;
- sbit l8=P2^7;
- sbit s1 =P1^0;
- sbit s2 =P1^1;
- sbit s3 =P1^2;
- sbit s4 =P1^3;
- sbit s5 =P1^4;
- sbit s6 =P1^5;
- sbit s7 =P1^6;
- sbit s8 =P1^7;
- main()
- {
- unsigned int digit_val[]={0x40,0xF9,0x24,0x30,0x19,0x12,0x02,0xF8,0x00,0x10};
- P1=0x00;
- P2=0x00;
- while(1)
- {
- if(s1==1)
- {
- l1=1;
- delay(50);
- P3=0x40;
- }
- else if(s2==1)
- {
- l2=1;
- delay(50);
- P3=0xF9;
- }
- else if(s3==1)
- {
- l3=1;
- delay(50);
- P3=0x24;
- }
- else if(s4==1)
- {
- l4=1;
- delay(50);
- P3= 0x30;
- }
- else if(s5==1)
- {
- l5=1;
- delay(50);
- P3=0x19;
- }
- else if(s6==1)
- {
- l6=1;
- delay(50);
- P3=0x12;
- }
- else if(s7==1)
- {
- l7=1;
- delay(50);
- P3=0x02;
- }
- else if(s8==1)
- {
- l8=1;
- delay(50);
- P3=0xF8;
- }
- }
- }
- Proteous simulation:
- Practical 6
- SerialCommuncation
- #include <reg51.h>
- void cct_init(void);
- void SerialInitialize(void);
- void uart_msg(unsigned char *c);
- void uart_tx(unsigned char);
- sbit led1 = P1^0;
- sbit led2 = P1^1;
- sbit led3 = P1^2;
- sbit led4 = P1^3;
- void main()
- {
- cct_init();
- SerialInitialize();
- EA = 1;
- ES = 1;
- uart_msg("Initializing Serial Communication");
- uart_tx(0x0d);
- uart_msg("1,2,3,4 key can on leds and a,b,c,d can off them respectively.");
- uart_tx(0x0d); //next line
- uart_msg("Press the key for particular LED");
- uart_tx(0x0d);
- while(1);
- }
- void cct_init(void) //initialize cct
- {
- P0 = 0x00; //not used
- P1 = 0x00; //output port used for leds
- P2 = 0x00; //not used
- P3 = 0x03; //used for serial communication
- }
- void SerialInitialize(void) //Initialize Serial Port
- {
- TMOD = 0x20; //Timer 1 In Mode 2 -Auto Reload to Generate Baud Rate
- SCON = 0x50; //Serial Mode 1, 8-Data Bit, REN Enabled
- TH1 = 0xFD; //Load Baud Rate 9600 To Timer Register
- TR1 = 1; //Start Timer
- }
- void uart_msg(unsigned char *c)
- {
- while(*c != 0)
- {
- uart_tx(*c++);
- }
- }
- void uart_tx(unsigned char serialdata)
- {
- SBUF = serialdata; //Load Data to Serial Buffer Register
- while(TI == 0); //Wait Until Transmission To Complete
- TI = 0; //Clear Transmission Interrupt Flag
- }
- void serial_ISR (void) interrupt 4
- {
- char chr; //receive character
- if(RI==1)
- {
- chr = SBUF;
- RI = 0;
- }
- P0 = ~P0; //Show the data has been updated
- switch(chr)
- {
- case '1': led1 = 1; uart_msg("1st on"); uart_tx(0x0d); break;
- case '2': led2 = 1; uart_msg("2nd on"); uart_tx(0x0d); break;
- case '3': led3 = 1; uart_msg("3rd on"); uart_tx(0x0d); break;
- case '4': led4 = 1; uart_msg("4th on"); uart_tx(0x0d); break;
- case 'a': led1 = 0; uart_msg("1st off"); uart_tx(0x0d); break;
- case 'b': led2 = 0; uart_msg("2nd off"); uart_tx(0x0d); break;
- case 'c': led3 = 0; uart_msg("3rd off"); uart_tx(0x0d); break;
- case 'd': led4 = 0; uart_msg("4th off"); uart_tx(0x0d); break;
- default: ; break; //do nothing
- }
- RI = 0;
- }
- Build new project in keil
- Right click on virtual terminal/edit properties/baud rate = 9600
- Right click on 8051/edit properties/clock frequency = 11.0592
- Practical 7
- 7 Segment display
- Practical 8
- Binary counter
- #include <reg51.h>
- void main()
- {
- unsigned char x,y;
- unsigned int i;
- P1=0x00;
- while(1)
- {
- x=0x01;
- for(y=0;y<9;y++)
- {
- P1=x;
- for(i=0;i<60000;i++);
- x=x<<1;
- }
- }
- }
- Big Data Final Journal
- Practical No 1
- TimeSeries analysis
- mydata<-read.csv("D:/jobs.csv")
- mydata
- attach(mydata)
- x<-Month
- Y<-Total.Filled.Jobs
- X
- d.y<-diff(Y)
- plot(x,Y)
- acf(Y)
- pacf(Y)
- acf(d.y)
- arima(x=Y, order=c(1,0,1))
- arima(x=Y, order=c(0,0,1))
- show(mydata)
- mydata.arima001<-arima(Y,order=c(0,0,1))
- mydata.pred1<-predict(mydata.arima001,n.ahead=100)
- plot(Y)
- lines(mydata.pred1$pred,col="blue")
- attach(mydata.pred1)
- head(mydata.pred1)
- tail(mydata.pred1$pred)
- head(mydata.pred1$pred)
- Practical No 2
- Topic Modeling
- library(tm)
- library(topicmodels)
- setwd("D:/british-fiction-corpus")
- filenames<-list.files(path="D:/british-fiction-corpus",pattern="*.txt")
- filenames
- filetext<-lapply(filenames,readLines)
- mycorpus<-Corpus(VectorSource(filetext))
- mycorpus<-tm_map(mycorpus,removeNumbers)
- mycorpus<-tm_map(mycorpus,removePunctuation)
- mycorpus
- mystopwords=c("of","a","and","the","in","to","for","that","is","on","are","with","as","by"
- ,"be","an","which","it","from","or","can","have","these","has","such","you")
- mycorpus<-tm_map(mycorpus,tolower)
- mycorpus<-tm_map(mycorpus,removeWords,mystopwords)
- dtm<-DocumentTermMatrix(mycorpus)
- k<-3
- #lda_output_3<-LDA(dtm,k,method="VEM",control=control_VEM)
- # control_VEM
- lda_output_3<-LDA(dtm,k,method="VEM",control=NULL)
- lda_output_3<-LDA(dtm,k,method="VEM")
- dtm
- lda_output_3<-LDA(dtm,k,method="VEM")
- lda_output_3@Dim
- lda_output_3<-LDA(dtm,k,method="VEM")
- show (dtm)
- topics(lda_output_3)
- terms(lda_output_3,10)
- library(tm)
- library(topicmodels)
- setwd("D:/wd")
- filenames<-list.files(path="D:/wd",pattern="*.txt")
- filenames
- filetext<-lapply(filenames,readLines)
- mycorpus<-Corpus(VectorSource(filetext))
- mycorpus<-tm_map(mycorpus,removeNumbers)
- mycorpus<-tm_map(mycorpus,removePunctuation)
- mycorpus
- mystopwords=c("of","a","and","the","in","to","for","that","is","on","are","with","as","by"
- ,"be","an","which","it","from","or","can","have","these","has","such","you")
- mycorpus<-tm_map(mycorpus,tolower)
- mycorpus<-tm_map(mycorpus,removeWords,mystopwords)
- dtm<-DocumentTermMatrix(mycorpus)
- k<-2
- #lda_output_3<-LDA(dtm,k,method="VEM",control=control_VEM)
- # control_VEM
- #lda_output_3<-LDA(dtm,k,method="VEM",control=NULL)
- lda_output_3<-LDA(dtm,k,method="VEM")
- dtm
- lda_output_3<-LDA(dtm,k,method="VEM")
- lda_output_3@Dim
- lda_output_3<-LDA(dtm,k,method="VEM")
- show (dtm)
- topics(lda_output_3)
- terms(lda_output_3,10)
- Practical 3
- Sentiment Analysis
- weatherdata<-read.csv("D:/weather.csv")
- library(tm)
- corpus<-iconv(weatherdata$tweet_text,to="utf-8")
- corpus<-Corpus(VectorSource(corpus))
- corpus<tm_map(corpus,tolower)
- corpus<-tm_mapcorpus,tolower)
- corpus<-tm_map(corpus,removePunctuation)
- inspect(corpus[1:5])
- corpus<-tm_map(corpus,removeNumbers)
- inspect(corpus[1:5])
- corpus<-tm_map(corpus,removeWords,stopwords('english'))
- corpus<-tm_map(corpus,stripWhitespace)
- tdm<-TermDocumentMatrix(corpus)
- tdm
- tdm<-as.matrix(tdm)
- tdm[1:10,1:2]
- w<-rowSums(tdm)
- library(wordcloud)
- w<-sort(rowSums(tdm),decreasing = TRUE)
- set.seed(222)
- wordcloud(words = names(w),freq =
- w,max.words=150,random.order=F,min.freq = 5,colors =
- brewer.pal(8,'Dark2'))
- wordcloud(words = names(w),freq =
- w,max.words=150,random.order=F,min.freq = 5,colors =
- brewer.pal(8,'Dark2'),rot.per=0.3)
- library(syuzhet)
- library(lubridate)
- library(ggplot2)
- library(scales)
- library(reshape2)
- library(dplyr)
- weatherdata<-read.csv("D:/weather.csv")
- tweets<-iconv(weatherdata$tweet_text,to="utf-8")
- s<-get_nrc_sentiment(tweets)
- head(s)
- tweets[4]
- barplot(colSums(s),las=2,col=rainbow(10),ylab='count',main='Sentiments
- for weather')
- Practical 4
- Kshingles
- readinteger<- function()
- {
- n <-readline(prompt = "enter value of k-1: ")
- k <- as.integer(n)
- u1 <- readLines("D:\\wine.txt")
- Shingle <- 0
- i<- 0
- while(i<nchar(u1)-k+1)
- {
- Shingle[i] <- substr(u1,start = i,stop = i+k)
- print(Shingle[i])
- i=i+1
- }
- }
- Type command after running the above code.
- readinteger()
- Output
- Practical 5
- Document similarity
- Code -
- library(textreuse)
- library(graphics)
- minhash<- minhash_generator(200, seed = 235)
- ats<- TextReuseCorpus(dir="D:/R/french-plays",n=5,minhash_func = minhash)
- buckets<- lsh(ats, bands = 50 ,progress = interactive())
- candidates<- lsh_candidates(buckets)
- my.df<- lsh_compare(candidates, ats , jaccard_similarity)
- my.df
- color <- c("red" , "green" , "blue" , "orange" , "yellow" , "pink")
- barplot(as.matrix(my.df),col=color)
- Output -
- Practical 6
- Setting up environment for Big Data Hadoop Sandbox:
- 1. Download HDP Sandbox
- Give full access control in file property
- Use vitualbox 5.2 above
- Restart machine
- 2. Import Appliance from VirtualBox
- once extraction is complete
- Visit localhost:1080
- 3. Install WinSCP
- 4. Install putty
- 5. Install/ launch Eclipse
- 6.reset Password
- user id: root
- old pass: hadoop
- new pass: ********
- Admin password
- to reset admin password type command:
- ambari-admin-password-reset
- new: ********
- WinSCP
- accessing on Cent OS File sys
- host name: 127.0.0.1 port 2222
- user: root Pass: jd@ruia19
- accessing on Cent OS File sys using shell in box.
- Ambari file explorer for Hadoop file system
- at web shell cleint
- login with : root pass: ********
- If any alerts found restart processes
- Practical 7 Wordcount
- Start Virtual box and hadoop sandbox
- Go to the given virtualbox link
- Login in web shell client
- Open ambari
- Go to files view
- Create program in eclipse
- WordCount Code -
- import java.io.IOException;
- import java.util.StringTokenizer;
- import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.fs.Path;
- import org.apache.hadoop.io.IntWritable;
- import org.apache.hadoop.io.Text;
- import org.apache.hadoop.mapreduce.Job;
- import org.apache.hadoop.mapreduce.Mapper;
- import org.apache.hadoop.mapreduce.Reducer;
- import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
- import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
- import org.apache.hadoop.util.GenericOptionsParser;
- public class WordCount {
- public static class TokenizerMapper
- extends Mapper<Object, Text, Text, IntWritable>{
- private final static IntWritable one = new IntWritable(1);
- private Text word = new Text();
- public void map(Object key, Text value, Context context
- ) throws IOException, InterruptedException {
- StringTokenizer itr = new StringTokenizer(value.toString());
- while (itr.hasMoreTokens()) {
- word.set(itr.nextToken());
- context.write(word, one);
- }
- }
- }
- public static class IntSumReducer
- extends Reducer<Text,IntWritable,Text,IntWritable> {
- private IntWritable result = new IntWritable();
- public void reduce(Text key, Iterable<IntWritable> values,
- Context context
- ) throws IOException, InterruptedException {
- int sum = 0;
- for (IntWritable val : values) {
- sum += val.get();
- }
- result.set(sum);
- context.write(key, result);
- }
- }
- public static void main(String[] args) throws Exception {
- Configuration conf = new Configuration();
- Job job = new Job(conf, "word count");
- job.setJarByClass(WordCount.class);
- job.setMapperClass(TokenizerMapper.class);
- job.setCombinerClass(IntSumReducer.class);
- job.setReducerClass(IntSumReducer.class);
- job.setOutputKeyClass(Text.class);
- job.setOutputValueClass(IntWritable.class);
- FileInputFormat.addInputPath(job, new Path(args[0]));
- FileOutputFormat.setOutputPath(job, new Path(args[1]));
- System.exit(job.waitForCompletion(true) ? 0 : 1);
- }
- }
- Add jar files
- Right click on project name (wordcount) / properties / java build path / libraries / add external jar files
- Export jar file
- Right click on project name (wordcount) / Export / java / jar files / select your jar file / select main class
- Create a wordlist on notepad or take any wordlist
- open winscp
- login with web shell client username and password
- set host name 127.0.0.1 and port 2222
- Copy the jar file you exported (wordcount.jar) in web shell
- Change permissions in winscp - select the file (wordcount) right click/ properties/ chose all read write execute permissions
- go to web shell and check if wordcount.jar file is uploaded
- Command - ls
- go to ambari
- create a new folder - Wordcount
- inside Wordcount create a new folder - Input
- Change permissions to all folders to read write and execute - select folder and click on permissions
- Then upload your wordlist in Input folder and give all permissions
- go to web shell and type command -
- hadoop jar wordcount.jar /Wordcount/Input /Wordcount/Output
- Note - hadoop jar wordcount.jar(is the name of your jar file you uploaded)
- New Output folder will be created with the output of your wordlist
- Now go in the Wordcount/Output folder
- Select and open the part-r-00000 file. This file shows the output of the wordcount
- Practical 8
- Top 5 categories
- Start Virtual box and hadoop sandbox
- Go to the given virtualbox link
- Login in web shell client
- Open ambari
- Go to files view
- Create program in eclipse
- Top 5 categories of videos Code -
- import java.io.IOException;
- import org.apache.hadoop.fs.Path;
- import org.apache.hadoop.conf.*;
- import org.apache.hadoop.io.*;
- import org.apache.hadoop.mapreduce.*;
- import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
- import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
- import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
- import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
- public class Top5_categories {
- public static class Map extends Mapper<LongWritable, Text, Text,
- IntWritable> {
- private Text category = new Text();
- private final static IntWritable one = new IntWritable(1);
- public void map(LongWritable key, Text value, Context context )
- throws IOException, InterruptedException {
- String line = value.toString();
- String str[]=line.split("\t");
- if(str.length > 5){
- category.set(str[3]);
- }
- context.write(category, one);
- }
- }
- public static class Reduce extends Reducer<Text, IntWritable,
- Text, IntWritable> {
- public void reduce(Text key, Iterable<IntWritable> values,
- Context context)
- throws IOException, InterruptedException {
- int sum = 0;
- for (IntWritable val : values) {
- sum += val.get();
- }
- context.write(key, new IntWritable(sum));
- }
- }
- public static void main(String[] args) throws Exception {
- Configuration conf = new Configuration();
- @SuppressWarnings("deprecation")
- Job job = new Job(conf, "categories");
- job.setJarByClass(Top5_categories.class);
- job.setMapOutputKeyClass(Text.class);
- job.setMapOutputValueClass(IntWritable.class);
- //job.setNumReduceTasks(0);
- job.setOutputKeyClass(Text.class);
- job.setOutputValueClass(IntWritable.class);
- job.setMapperClass(Map.class);
- job.setReducerClass(Reduce.class);
- job.setInputFormatClass(TextInputFormat.class);
- job.setOutputFormatClass(TextOutputFormat.class);
- FileInputFormat.addInputPath(job, new Path(args[0]));
- FileOutputFormat.setOutputPath(job, new Path(args[1]));
- Path out=new Path(args[1]);
- out.getFileSystem(conf).delete(out);
- job.waitForCompletion(true);
- }
- }
- public static void main(String[] args) throws Exception {
- Configuration conf = new Configuration();
- Job job = new Job(conf, "word count");
- job.setJarByClass(WordCount.class);
- job.setMapperClass(TokenizerMapper.class);
- job.setCombinerClass(IntSumReducer.class);
- job.setReducerClass(IntSumReducer.class);
- job.setOutputKeyClass(Text.class);
- job.setOutputValueClass(IntWritable.class);
- FileInputFormat.addInputPath(job, new Path(args[0]));
- FileOutputFormat.setOutputPath(job, new Path(args[1]));
- System.exit(job.waitForCompletion(true) ? 0 : 1);
- }
- }
- Add jar files
- Right click on project name (Top5) / properties / java build path / libraries / add external jar files
- Export jar file
- Right click on project name (Top5) / Export / java / jar files / select your jar file / select main class
- Create a wordlist on notepad or take any wordlist or text file with appropriate data
- open winscp
- login with web shell client username and password
- set host name 127.0.0.1 and port 2222
- Copy the jar file you exported (Top5.jar) in web shell
- Change permissions in winscp - select the file (Top5) right click/ properties/ chose all read write execute permissions
- go to web shell and check if Top5_categories.jar file is uploaded
- Command - ls
- go to ambari
- create a new folder - Top 5
- inside Top 5 create a new folder - Input
- Change permissions to all folders to read write and execute - select folder and click on permissions
- Then upload your wordlist in Input folder and give all permissions
- go to web shell and type command -
- hadoop jar Top5_categories.jar /Top5/input /Top5/output
- Note - hadoop jar Top5_categories.jar(is the name of your jar file you uploaded)
- New Output folder will be created with the output of your wordlist
- Now go in the Top5/Output folder
- Select and open the part-r-00000 file. This file shows the output of the Top5
- Practical 9
- Top 10 rating of youtube videos
- Start Virtual box and hadoop sandbox
- Go to the given virtualbox link
- Login in web shell client
- Open ambari
- Go to files view
- Create program in eclipse
- Top 10 rating of youtube videos Code -
- package ratedvid;
- import java.io.IOException;
- import org.apache.hadoop.fs.Path;
- import org.apache.hadoop.conf.*;
- import org.apache.hadoop.io.*;
- import org.apache.hadoop.mapreduce.*;
- import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
- import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
- import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
- import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
- public class VideoRating {
- public static class Map extends Mapper<LongWritable, Text, Text,
- FloatWritable> {
- private Text video_name = new Text();
- private FloatWritable rating = new FloatWritable();
- public void map(LongWritable key, Text value, Context context )
- throws IOException, InterruptedException {
- String line = value.toString();
- String str[]=line.split("\t");
- if(str.length > 7){
- video_name.set(str[0]);
- if(str[6].matches("\\d+.+")){ //this regular expression
- float f=Float.parseFloat(str[6]); //typecasting string to float
- rating.set(f);
- }
- }
- context.write(video_name, rating);
- }
- }
- public static class Reduce extends Reducer<Text, FloatWritable,
- Text, FloatWritable> {
- public void reduce(Text key, Iterable<FloatWritable> values,
- Context context)
- throws IOException, InterruptedException {
- float sum = 0;
- int l=0;
- for (FloatWritable val : values) {
- l+=1; //counts number of values are there for that key
- sum += val.get();
- }
- sum=sum/l; //takes the average of the sum
- context.write(key, new FloatWritable(sum));
- }
- }
- public static void main(String[] args) throws Exception {
- Configuration conf = new Configuration();
- @SuppressWarnings("deprecation")
- Job job = new Job(conf, "videorating");
- job.setJarByClass(VideoRating.class);
- job.setMapOutputKeyClass(Text.class);
- job.setMapOutputValueClass(FloatWritable.class);
- //job.setNumReduceTasks(0);
- job.setOutputKeyClass(Text.class);
- job.setOutputValueClass(FloatWritable.class);
- job.setMapperClass(Map.class);
- job.setReducerClass(Reduce.class);
- job.setInputFormatClass(TextInputFormat.class);
- job.setOutputFormatClass(TextOutputFormat.class);
- FileInputFormat.addInputPath(job, new Path(args[0]));
- FileOutputFormat.setOutputPath(job, new Path(args[1]));
- Path out=new Path(args[1]);
- out.getFileSystem(conf).delete(out);
- job.waitForCompletion(true);
- }
- }
- Add jar files
- Right click on project name (Top10) / properties / java build path / libraries / add external jar files
- Export jar file
- Right click on project name (Top10) / Export / java / jar files / select your jar file / select main class
- Create a wordlist on notepad or take any wordlist
- open winscp
- login with web shell client username and password
- set host name 127.0.0.1 and port 2222
- Copy the jar file you exported (Top10.jar) in web shell
- Change permissions in winscp - select the file (Top10) right click/ properties/ chose all read write execute permissions
- go to web shell and check if Top10.jar file is uploaded
- Command - ls
- go to ambari
- create a new folder - Top10
- inside Top10 create a new folder - Input
- Change permissions to all folders to read write and execute - select folder and click on permissions
- Then upload your wordlist in Input folder and give all permissions
- go to web shell and type command -
- hadoop jar Top10.jar /Top10/input /Top10/output
- Note - hadoop jar Top10.jar(is the name of your jar file you uploaded)
- New Output folder will be created with the output of your wordlist
- Now go in the Top10/Output folder
- Select and open the part-r-00000 file. This file shows the output of the Top10
Add Comment
Please, Sign In to add comment