Tuesday, 14 April 2015

Clustering Algorithm With And Without Overlapping Code Size Less than 80 Lines

Clustering Algorithm With And Without Overlapping Code Size Less than 80 Lines 


#include<stdio.h>

typedef struct cluster{
int total,cr;
int element[10];
}cluster;

void main()
{
int i,j,k,l,n,ch,flag,count;
float c[10][10],th;
cluster c1[10];
count=0;
flag=0;
printf("\n Enter choice 1.Overlaping 2.Non-overlaping");
scanf("%d",&ch);
printf("\n Enter the number of documents");
scanf("%d",&n);
printf("\n Enter the threshold");
scanf("%f",&th);
printf("\n Enter the elements");
for(i=0;i<10;i++)
{
c[i][i]=1;
}
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
printf("\n Enter document:%d and document:%d",i,j);
scanf("%f",&c[i][j]);

}
}
c1[count].element[1]=1;
c1[count].total=1;
c1[count++].cr=1;
for(i=2;i<=n;i++)
{ flag=0;
for(j=0;j<count;j++)
{
k=c1[j].cr;
if(c[k][i]>=th)
{
c1[j].total++;
l=c1[j].total;
c1[j].element[l]=i;
flag=1;
if(ch==2)
{
break;
}
else if(ch==1)
{
}
}
}
if(flag==0)
{
c1[count].total=1;
c1[count].element[1]=i;
c1[count++].cr=i;
}
}
for(i=0;i<count;i++)
{
printf("\n Number of clsuters:%d",c1[i].total);
printf("\n Clusters cr:%d",c1[i].cr);
printf("\n The elements of the clsuter");
for(j=1;j<=c1[i].total;j++)
{
printf("\t%d",c1[i].element[j]);
}
printf("\n");
}
}

Saturday, 11 April 2015

Inverted Index Code (Short Version)

Inverted Index Code (Short Version)

Inverted Index Algorithm Code (Short Version)


#include<stdio.h>
#include<string.h>

void main()
{
FILE *fptr1,*fptr2,*fptr3,*fptr4,*fptr5;
int count=1,len=0;
char ch[40],keyword[40],word[40],loc[40];
int flag=0;
// part 1 building the index
fptr1=fopen("temp.txt","r");
fptr2=fopen("temp1.txt","w");
while(fscanf(fptr1,"%s",ch)!=EOF)
{
fprintf(fptr2,"\n%s\t%d",ch,count);
len=strlen(ch);
count=count+len+1;

}
fclose(fptr1);
fclose(fptr2);
//part 2 the searching function
flag=0;
fptr3=fopen("temp1.txt","r");
printf("\n Enter a keyword to search");
scanf("%s",keyword);
while(fscanf(fptr3,"%s\t%s",word,loc)!=EOF)
{

if(strcmp(keyword,word)==0)
{
flag=1;
}
else
{
flag=0;
}
if(flag==1)
{
printf("Keyword found");
printf("\nkeyword:%slocation:%s",word,loc);
break;
}

}
if(flag==0)
{
printf("Keyword not found");
}
fclose(fptr3);
}

Inverted Index Code (Short Version)

Inverted Index Algorithm Code (Short Version)


#include<stdio.h>
#include<string.h>

void main()
{
FILE *fptr1,*fptr2,*fptr3,*fptr4,*fptr5;
int count=1,len=0;
char ch[40],keyword[40],word[40],loc[40];
int flag=0;
// part 1 building the index
fptr1=fopen("temp.txt","r");
fptr2=fopen("temp1.txt","w");
while(fscanf(fptr1,"%s",ch)!=EOF)
{
fprintf(fptr2,"\n%s\t%d",ch,count);
len=strlen(ch);
count=count+len+1;

}
fclose(fptr1);
fclose(fptr2);
//part 2 the searching function
flag=0;
fptr3=fopen("temp1.txt","r");
printf("\n Enter a keyword to search");
scanf("%s",keyword);
while(fscanf(fptr3,"%s\t%s",word,loc)!=EOF)
{

if(strcmp(keyword,word)==0)
{
flag=1;
}
else
{
flag=0;
}
if(flag==1)
{
printf("Keyword found");
printf("\nkeyword:%slocation:%s",word,loc);
break;
}

}
if(flag==0)
{
printf("Keyword not found");
}
fclose(fptr3);
}

Friday, 10 April 2015

Histogram code in java

Histogram Code In Java



Histograb.java file


import java.applet.Applet;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.image.PixelGrabber;


@SuppressWarnings("serial")
public class HistoGrab extends Applet {

Dimension d;
Image img;
int iw,ih;
int pixels[];
int w,h;
int hist[]=new int[256];
int max_hist=0;

public void init()
{
d=getSize();
w=d.width;
h=d.height;

img=getImage(getDocumentBase(),getParameter("img"));
MediaTracker t=new MediaTracker(this);
t.addImage(img,0);
try {
t.waitForID(0);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

iw=img.getWidth(null);
ih=img.getHeight(null);

pixels=new int[iw*ih];

PixelGrabber pg=new PixelGrabber(img,0,0,iw,ih,pixels,0,iw);
try {
pg.grabPixels();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}



for(int i=0;i<iw*ih;i++)
{
int p=pixels[i];
int r=0xff &(p>>16);
int g=0xff &(p>>8);
int b=0xff &(p);

int y=(int)(.33*r+.56*g+.11*b);
hist[y]++;
}

for(int i=0;i<256;i++)
{
if(hist[i]>max_hist)
max_hist=hist[i];


}


}

public void update(){}

public void paint(Graphics g)
{
g.drawImage(img,0,0,null);
System.out.println(w);
int x=(w-256)/2;
int lasty=h-h*hist[0]/max_hist;
for(int i=0;i<256;i++,x++)
{
int y=h-h*hist[i]/max_hist;
g.setColor(new Color(i,i,i));
g.fillRect(x, y, 1, h);
g.setColor(Color.red);
g.drawLine(x-1,lasty,x,y);
lasty=y;


}

}

}


The html file


<html>
<head></head>
<body>
<applet code=HistoGrab.class width=314 height=400>
<param name=img value=green.jpg>
</applet>
</body>
</html>


Web Crawler Code in Java


Web Crawler Code in Java



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class dp {

public static void main(String args[]) throws InterruptedException,IOException
{

try{
String s="https://www.google.co.in";
int page=1;

Set <String> set=new  HashSet <String>();
Queue <String> q=new  LinkedList <String>();

q.add(s);
System.out.println(s);
System.out.println("==================");

while(!q.isEmpty())
{
if(page!=10)
{
String link=q.remove();
URL my_url=new URL(link);
BufferedReader br=new BufferedReader(new InputStreamReader(my_url.openStream()));
String str="";
String strTemp="";
StringTokenizer strtok;

while(null!=(strTemp=br.readLine()))
{
str=str+strTemp;

}

strtok=new StringTokenizer(str,"\"",false);
while(strtok.hasMoreTokens())
{
strTemp=strtok.nextToken();
String regexp="https?://(\\w+\\.)*(\\w+)";
Pattern pattern=Pattern.compile(regexp);
Matcher matcher=pattern.matcher(strTemp);
if(matcher.find())
{
String w=matcher.group();
if(!(set.contains(w)))
{
set.add(w);
q.add(w);
System.out.println(w);

}


}


}

System.out.println("================");
set.clear();
page++;

}
else
break;


}






}
catch(Exception ex)
{
ex.printStackTrace();


}



}


}

Saturday, 4 April 2015

DOM PARSER (Simplest Code Ever)

DOM PARSER (Simplest Code Ever)




import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


public class MyDomParser {

public static void main(String args[]) throws SAXException, IOException
{
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder=factory.newDocumentBuilder();
Document doc=builder.parse("people.xml");
NodeList personlist=doc.getElementsByTagName("person");
for(int i=0;i<personlist.getLength();i++)
{
Node p=personlist.item(i);
if(p.getNodeType()==Node.ELEMENT_NODE)
{
Element person=(Element) p;
String id=person.getAttribute("id");
NodeList namelist=person.getChildNodes();
for(int j=0;j<namelist.getLength();j++)
{
Node n=namelist.item(j);
if(n.getNodeType()==Node.ELEMENT_NODE)
{
Element name=(Element) n;
System.out.println("person"+id+":"+name.getTagName()+"="+name.getTextContent());

}

}

}
}

} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}

}


XML File (people.xml)

<?xml version="1.0"?>
<people>
<person id="1">
<lastname>pawar</lastname>
<firstname>ashutosh</firstname>
</person>
<person id="2">
<lastname>khedkar</lastname>
<firstname>amol</firstname>
</person>
</people>


Friday, 27 March 2015


Friday, 13 March 2015

Socket Program in Java[Client Server Program]

Description:Client program accepts a number from the user and passes it to the server, the server performs as simple mathematical operation like addition of the number with digit 5.


Video Tutorial:



Source Code://CLASS CLI


import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;


public class cli {

public static void main(String args[]) throws UnknownHostException, IOException
{
int number,temp;
Socket s=new Socket("127.0.0.1",1001);
Scanner sc=new Scanner(System.in);
Scanner sc1=new Scanner(s.getInputStream());
System.out.println("Enter any number");
number=sc.nextInt();
PrintStream p=new PrintStream(s.getOutputStream());
p.println(number);
temp=sc1.nextInt();
System.out.println(temp);
}
}
//Class Ser

import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;


public class ser {

public static void main(String args[]) throws IOException
{
int n1,n2;
ServerSocket s1=new ServerSocket(1001);
Socket ss=s1.accept();
Scanner sc=new Scanner(ss.getInputStream());
n1=sc.nextInt();
PrintStream p=new PrintStream(ss.getOutputStream());
n2=n1+5;
p.println(n2);
}

}

Conflation Algorithm Code [Commented]

#include<stdio.h>
#include<string.h>

void main()

{

FILE *fptr1,*fptr2,*fptr3,*fptr4,*fptr5,*fptr6,*fptr7;

char ch[30],stop[30],temp[30],temp1[30];
int flag=0,i=0,j=0;


/*===============CONFLATION ALGORITHM PART -1 REMOVAL OF STOP WORDS FROM A FILE===========================*/

//SOURCE FILE INPUT.TXT CONTAINS NORMAL WORDS AS WELL AS STOP WORDS
//THE FILE STOP.TXT CONTAINS A LIST OF STOP WORDS 
//THE STOP WORDS FROM FIRST FILE ARE REMOVES AND REMAINING WORDS ARE STORED IN THE FILE NAMED OUTPUT1.TXT

fptr1=fopen("input.txt","r");

fptr3=fopen("output1.txt","w");
while(fscanf(fptr1,"%s",ch)!=EOF)
{
fptr2=fopen("stop.txt","r");
while(fscanf(fptr2,"%s",stop)!=EOF)
{
if(strcmp(ch,stop)==0)
{
flag=1; //WHEN FLAG IS SET TO 1 MEANS THAT STOP WORD HAS BEEN FOUND
}
}

if(flag==1)

{
flag=0; //FLAG NEEDS TO BE RESET TO VALUE 1 
}

else if(flag==0)

{
fprintf(fptr3,"%s\n",ch); //FLAG IS 0 MEANING THE WORD IS NOT A STOP WORD
}
}

fclose(fptr1);

fclose(fptr2);
fclose(fptr3);
/*==============================CONFLATION ALGOORITHM PART 2 , SUFFIX STRIPPING==============================================*/

fptr4=fopen("output1.txt","r");

fptr5=fopen("output2.txt","w");
while(fscanf(fptr4,"%s",temp)!=EOF)
{
i=0;
i=strlen(temp);
i--;

//REMOVING THE PLURAL WORDS S


if(temp[i]=='s')

{
temp[i]='\0';

}

//REMOVING ED TYPE
else if(temp[i]=='d')
{
if(temp[--i]=='e')
{
temp[i]='\0';
temp[++i]='\0';
}

}

//REMOVING LY
else if(temp[i]=='y')
{
if(temp[--i]=='l')
{

temp[i]='\0';

temp[++i]='\0';

}

}

//REMOVING ING


else if(temp[i]=='g')

{
if(temp[--i]=='n')
{
if(temp[--i]=='i')
{
temp[i]='\0';
temp[++i]='\0';
temp[++i]='\0';

}

}

}


fprintf(fptr5,"%s\n",temp);


}

fclose(fptr5);


/*====================CONFLATION ALGORITHM STEP 3 , DETECTION OF EQUIVALENT STEMS======================*/

flag=0;
fptr6=fopen("output2.txt","r");
while(fscanf(fptr6,"%s",temp)!=EOF)
{
fptr7=fopen("output3.txt","r+");
while(fscanf(fptr7,"%s",temp1)!=EOF)
{

if(strcmp(temp,temp1)==0)

{
flag=1;

}

}
if(flag==1)
{
flag=0;
}
else if(flag==0)
{
fprintf(fptr7,"\n%s",temp);
printf("%s",temp);

}

fclose(fptr7);
}
fclose(fptr6);
//fclose(fptr7);

}