Pages

Tuesday, October 11, 2011

ai-class, ml-class and db-class


I enrolled in all three courses of Stanford which were a part of a new online initiative by them, and today when I saw the lectures and worked on the quizzes I realize how exciting the courses are. Firstly you can watch the lectures whenever you want and wherever you want + you can watch them again and again until you get the concept completely and lastly I like the concept of me choosing to take the course rather that it being thrust upon me which happens in almost all universities in India. 

There are three courses which are being offered and I have opted to take all of them and they are pretty interesting!

  1. Introduction to Artificial Intelligence (http://www.ai-class.com)
  2. Introduction to Databases (http://www.db-class.org)
  3. Machine Learning (http://www.ml-class.org)

I hope I will have a good time and will learn a lot and I am happy that some initiatives are being taken towards Online Education. I myself took up a small initiative sometime during the start of this year. Here is the link http://www.unacademy.in

Thursday, August 18, 2011

Java NoClassDefFoundError

In your Java Web project make sure you add the jar files to WEB-INF/lib and make sure that you remove the external jar files from your build path. Instead of adding them as external jar files just add them to the lib folder and save yourself the ClassDefNotFound errors which are very irritating.

Sunday, August 14, 2011

Using jQuery inline form labels

At first I thought that I would be requiring jQuery to use the inline labeling of the forms but then I figured out the HTML5 provides a simple way to do the same using a simple placeholder tag.

Code:
input type="text" placeholder="Streaming keywords" 

Thursday, May 26, 2011

Stemming of words: Porter Stemmer Algorithm

Searching for some libraries which could transform words to their root forms. So that when I encounter words like filling, filled, fill, they are all treated as the same. 


Definition of stemming: stemming is the process for reducing inflected (or sometimes derived) words to their stem, base or root form – generally a written word form


A stemmer for English, for example, should identify the string "cats" (and possibly "catlike", "catty" etc.) as based on the root "cat", and "stemmer", "stemming", "stemmed" as based on "stem". A stemming algorithm reduces the words "fishing", "fished", "fish", and "fisher" to the root word, "fish".


Something about Porter Stemmer Algorithm: 
A later stemmer was written by Martin Porter and was published in the July 1980 issue of the journal Program. This stemmer was very widely used and became the de-facto standard algorithm used for English stemming. Dr. Porter received the Tony Kent Strix award in 2000 for his work on stemming and information retrieval.


Martin Porter released an official free-software implementation of the algorithm.





public class ASimpleStemmer {

public static void main(String[] args) {
Stemmer s = new Stemmer(); // See the official implementation, link given above.
s.add("abominable".toCharArray(), 10);
s.stem();
System.out.println(s.toString()); //Output: abomin

s.add("abominate".toCharArray(), 9);
s.stem();
System.out.println(s.toString()); //Output: abomin
}
}









Wednesday, May 18, 2011

Example of ConcurrentModificationException


private static void refineMap(BayesianResult bayes, HashMap terms) {
for (Map.Entry entry : terms.entrySet()) {
String word = entry.getKey();
Token t = entry.getValue();
if ((t.getClean_count()+t.getSens_count()) < 10) {
terms.remove(word);
continue;
}
}
}

Here I am modifying the HashMap ( terms.remove(word) ) while iteration is already being done on it. This throws an exception Exception in thread "main" java.util.ConcurrentModificationException


Monday, May 16, 2011

Jsoup Java Library for HTML parsing, Implementation and Examples


What is the issue that it resolves? 
You have HTML in a Java String or to fetch the webpage, and you want to parse that HTML to get at its contents, or to make sure it's well formed, or to modify it. 

Fetching the URL from the web:
Method:  Jsoup.connect(String url);

This method basically returns a Document (org.jsoup.nodes.Document) http://jsoup.org/apidocs/org/jsoup/nodes/Document.html , which is basically the HTML document transformed into this Class. The text, body, head, title etc of the Document can be accessed. Let us cite the various usage of the same. 

Getting the Title of the Document : 
Document doc = Jsoup.connect("http://example.com/").get();
String title = doc.title();

Here it returned a String which was the title of the Document, in most cases this is not the return Value. When we need to access attributes in body or say in the header the method returns an object of type Element (org.jsoup.nodes.Element).

Getting the text from an HTML file :

Document doc = Jsoup.parse(url);
String title = doc.title();
String text = doc.text(); 

Retrieving the metadata (description and keywords):

  Document d =  Jsoup.connect("http://in.yahoo.com/").get();
Element meta = d.select("meta[name=description]").first();
System.out.println(meta.attr("content"));
meta = d.select("meta[name=keywords]").first();
System.out.println(meta.attr("content"));

Download the jsoup jar (version 1.5.2)

Wednesday, February 2, 2011

Thinking Recursively



List of some of the best resources to learn Recursion, Backtracking and most importantly to start thinking recursively.

1. Recursion Basics, Solving Problems recursively, raise to the power example and mechanics of what is going to happen, Choosing a subset
Video Lecture

2. Thinking Recursively, Permute Code, More Examples, Tree of Recursive calls
Video Lecture

3. Testing with different cases, subsets, subset stratergy, Exhaustive Recursion, Recursive BackTracking, Anagram Finder, N Queens
Video Lecture

4. Backtracking Pseudocode, Sudoku Solver, Sudoku Code, Cryptarithmetic
Video Lecture

Reference: http://see.stanford.edu/see/courseInfo.aspx?coll=11f4f422-5670-4b4c-889c-008262e09e4e (Stanford Engineering Everywhere)




Tuesday, February 1, 2011

Print Permutations of a String

void permutation(string, string);
int main() {
permutation("","ABC");
}
void permutation(string sofar, string remaining) {
if (remaining=="") {
//Print the string sofar<<"\n";< p="">
return;
}
for (int i=0;i
permutation(sofar+remaining[i],remaining.substr(0,i)+remaining.substr(i+1));
}
}

Recursive Approach for Reversing a String


Recursive Approach for Reversing a String

Simple Approach: Take the last character + reverse(rest of the string)
Base Case: When we encounter a single character, return that character.

string reverse(string str) {
if (str.length()==1)
return str;
return str[str.length()-1]+reverse(str.substr(0,str.length()-1));
}

Monday, January 24, 2011

Codechef Puzzle: NUMGAME


So I encounter this interesting problem NUMGAME at Codechef. This is in the list of easy problems.
At first I was lost after looking the problem and could not think of how to solve it but when I came to thinking about it, it seems that it is quite easy.

Hints: (Obvious but important)

1) An odd number is always formed by the multiplication of two odd numbers.
2) If you substract an odd number from an odd number then it gives you an even number.

Sunday, January 23, 2011

GCD Recursive Method




The greatest common divisor (g.c.d.) of two nonnegative integers is the largest integer that divides evenly into both. In the third century B.C., the Greek mathematician Euclid discovered that the greatest common divisor of x and y can always be computed as follows:

If x is evenly divisible by y, then y is the greatest common divisor. Otherwise, the greatest common divisor of x and y is always equal to the greatest common divisor of y and the remainder of x divided by y


private static int gcdmethod(int i, int j) {

if (i%j==0) {
return j;
}
return gcdmethod(j,i%j);
}

}

Sunday, January 2, 2011

Scan Line Polygon Fill Algorithm

This video covers the concepts of Scan Line Polygon Filling. Takes into account the special cases and ways to resolve them.


Reference:
Computer Graphics, Hearn, Baker