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