Saturday, 28 November 2015
Counting syllables, words, lines and Flesch Score for a given text.
To see the source code clicke
here.
Monday, 23 November 2015
Counting number of words in given text example 1
Here is an example of counting words in a given text. I am using my own rules for determining what is a word and my definition of word might not match the generally accepted definition but you can change the logic in the code to make it meet your definition of word.
Sunday, 15 November 2015
What is hashcode?
A hashcode is a number generated from any object. This is what allows objects to be stored/retrieved quickly in a Hashtable.
Imagine the following simple example:
On the table in front of you you have nine boxes, each marked with a number 1 to 9. You also have a pile of wildly different objects to store in these boxes, but once they are in there you need to be able to find them as quickly as possible.
What you need is a way of instantly deciding which box you have put each object in. It works like an index; you decide to find the cabbage so you look up which box the cabbage is in, then go straight to that box to get it.
Now imagine that you don't want to bother with the index, you want to be able to find out immediately from the object which box it lives in.
In the example, let's use a really simple way of doing this - the number of letters in the name of the object. So the cabbage goes in box 7, the pea goes in box 3, the rocket in box 6, the banjo in box 5 and so on. What about the rhinoceros, though? It has 10 characters, so we'll change our algorithm a little and "wrap round" so that 10-letter objects go in box 1, 11 letters in box 2 and so on. That should cover any object.
Sometimes a box will have more than one object in it, but if you are looking for a rocket, it's still much quicker to compare a peanut and a rocket, than to check a whole pile of cabbages, peas , banjos and rhinoceroses.
That's a hash code. A way of getting a number from an object so it can be stored in a Hashtable. In Java a hash code can be any integer, and each object type is responsible for generating its own. Lookup the "hashCode" method of Object.
References:
1. http://www.coderanch.com/t/321515/java/java/HashCode
2. http://www.coderanch.com/u/27/Frank-Carver
Friday, 13 November 2015
Wednesday, 11 November 2015
Abstract classes, interfaces, inheritance, polymorphism, overriding exampe 1 using UnfoldingMaps and processing library
Abstract classes, interfaces, inheritance, polymorphism, overriding exampe 1 using UnfoldingMaps and processing library.
Click
here
for the code.
Sunday, 8 November 2015
Dynamic binding and Polymorphism example 1
To see an example of dynamic binding and polymorphism click this link http://jagkanjava.blogspot.se/p/dynamic-binding-and-polymorphism.html
Sunday, 27 September 2015
jsoup cookbook
jsoup cookbook
Introduction
Input
- Parse a document from a String
- Parsing a body fragment
- Load a Document from a URL
- Load a Document from a File
Extracting data
- Use DOM methods to navigate a document
- Use selector-syntax to find elements
- Extract attributes, text, and HTML from elements
- Working with URLs
- Example program: list links
Modifying data
Cleaning HTML
Web-scraping with Java
Web-scraping with Java
To scrape our webpage, we'll use the HTML Parser "jsoup".
First, make a new directory for your Java code. Then, go to the jsoup download page and download the "jar" file called "core library.
This library includes the packages:
org.jsouporg.jsoup.helperorg.jsoup.nodesorg.jsoup.selectorg.jsoup.parserorg.jsoup.safetyorg.jsoup.examples
You can get at these but unzipping the file if you like (jars are zip files with a different name and one extra file inside). However, don't do this for the moment -- we'll use it as a zipped jar so we can get used to that instead.
Now download this class into the same directory: Scraper.java. Open it up and have a look at it. It demonstrates a few things.
First, how to get a page:
Document doc = null;
try {
doc = Jsoup.connect("http://www.geog.leeds.ac.uk/.../table.html").get(); // URL shortened!
} catch (IOException ioe) {
ioe.printStackTrace();
}
If you'd download the page to your harddrive in order to experiment without hitting the page online (which seems polite) you'd do this:
File input = new File("c:/pages/table.html");
Document doc = null;
try {
doc = Jsoup.parse(input, "UTF-8", "");
} catch (IOException ioe) {
ioe.printStackTrace();
}
Secondly, how to get an element with a specific id:
Element table = doc.getElementById("datatable");
Third, how to get all the elements with a specific tag and loop through them:
Elements rows = table.getElementsByTag("TR");
for (Element row : rows) {
// Do something with the "row" variable.
}
And finally, how to get the text inside an element:
Elements tds = row.getElementsByTag("TD");
for (int i = 0; i < tds.size(); i++) {
System.out.println(tds.get(i).text()); // Though our file uses every second element.
}
So, let's run the class. Making sure that the class and the jar file are in the same directory, we can ask the compiler to look inside the jar file for classes it needs, thus:
javac -cp .;jsoup-1.7.3.jar *.java
And likewise the JVM:
java -cp .;jsoup-1.7.3.jar Scraper
Give it a go -- it should scrape our table.html from the first part of the practical.
The jsoup library (homepage) is beautifully written, and comes with a very clear cookbook of how to do stuff, along with detailed API docs. The cookbook sometimes lacks a list of packages to import (just import everything if in doubt), but otherwise is a great starting point.
If your data is in JSON, you can get the JSON data as a String using:
String json = Jsoup.connect(url).ignoreContentType(true).execute().body();
and then parse it (split it into components) using a JSON library like the standard one or gson.
For scraping Twitter, you need twitter4j, and for most things a Twitter developer's key. See also the Developers' site. Similar libraries exist for other social media sites.
http://www.geog.leeds.ac.uk/courses/other/programming/practicals/general/web/scraping-intro/2.html
Friday, 25 September 2015
Wednesday, 16 September 2015
GSON, JSON, Serialization and Deserialization Example 1:
GSON, JSON, Serialization and Deserialization Example 1:
Friday, 21 August 2015
Polymorphism Interfaces and design of classes
To read the post on Polymorphism Interfaces and design of classes click here
Sunday, 9 August 2015
Method equals for objects and two String objects: The difference - class Object
Method equals for objects and two String objects: The difference - class Object
To read the post click here.
Friday, 31 July 2015
Saturday, 25 July 2015
Subscribe to:
Comments (Atom)