Calculate Words Similarity Using Wordnet in Java

This is my note of using WS4J calculate word similarity in Java. Step 1: Download Jars Download the following two jars and add them to your project library path. jawjaw-1.0.2.jar – https://code.google.com/p/jawjaw/downloads/list ws4j-1.0.1.jar – https://code.google.com/p/ws4j/downloads/list Step 2: Play With the Demo Program Code: package NLP;   import edu.cmu.lti.lexical_db.ILexicalDatabase; import edu.cmu.lti.lexical_db.NictWordNet; import edu.cmu.lti.ws4j.RelatednessCalculator; import edu.cmu.lti.ws4j.impl.HirstStOnge; import … Read more

GNU C “Nested Functions” Extension and Trampoline

Required background knowledge: activation record, static scoping & dynamic scoping.

Below is not standard C code. (case1)

foo (double a, double b){
  double square (double z) { return z * z; }    
  return square (a) + square (b);
}

Read more

R Write a Date Frame to CSV File

In R, we can easily write a date frame object to a csv file. By using the method write.csv, the csv file can be generated under the same directory of your R script. write.csv(dataFrame, file=”median.csv”)

Install R on Mac

Installing R on Mac is very easy. Just download the installation file here: http://cran.r-project.org/bin/macosx/. The bin files will be automatically added to the PATH.

Install R on Windows

Installing R on Windows is pretty straightforward. Just go to the page and download the installation file here. Remember you need to add the bin directory to your “Path” environment variable.

Get Argument Value from Command Line in R

Sometimes, you may want to run R Script by using command line. In this way, you can write a bash script and schedule a task everyday. In addition you can also pass different arguments to the R program. Create a R script “GetARgument.R”: args

Read File in R Line by Line

The R code below reads a file line by line. path <- "/path/to/the/file" print(path)   conn <- file(path,open="r") lines <- readLines(conn) for (i in 1:length(lines)){ print(lines[i]) } close(conn)path <- "/path/to/the/file" print(path) conn <- file(path,open="r") lines <- readLines(conn) for (i in 1:length(lines)){ print(lines[i]) } close(conn)

Retrieve a List from a Stream in Java 8

Method 1 String[] arr = { "ab", "bcd", "cdef", "defgh", "efhik", "fghijk", "ghijkl" }; Stream<String> stream = Stream.of(arr);String[] arr = { "ab", "bcd", "cdef", "defgh", "efhik", "fghijk", "ghijkl" }; Stream<String> stream = Stream.of(arr); Method 2 String[] arr = { "ab", "bcd", "cdef", "defgh", "efhik", "fghijk", "ghijkl" }; Stream<String> stream = Stream.of(arr);   ArrayList<String> list = … Read more

Concat Streams in Java 8

You may often need to concat or merge two streams. In the Stream class, there is a static method concat() that can be used for this purpose. Merge Two Streams String[] arr1 = { "a", "b", "c", "d" }; String[] arr2 = { "e", "f", "g" }; Stream<String> stream1 = Stream.of(arr1); Stream<String> stream2 = Stream.of(arr2); … Read more

Find the Second Largest Number in an Array

Problem Given an integer array, find the second largest number in the array. Solution The optimal time is linear to the length of the array N. We can iterate over the array, whenever the largest elements get updated, its current value becomes the second largest. public static int getSecondLargest(int[] arr){ int first = Integer.MIN_VALUE; int … Read more