Thursday, September 1, 2011

String Allignment with Edit Operations

A common way to measure the distance between two strings is using Levenshtein distance. Levenshtein distance is the minimum number of deletions, insertions, and substitutions needed to transform one string into another. Finding the distance between two strings is useful in certain applications such as spell checking (a word processor will suggest dictionary words that are close to your misspelled word). See wikipedia for more details and an example of Levenshtein distance calculation.
Another related and also important operation is to find the minimum edit alignment; that is, once the minimum edit distance between the two strings is found, output the sequence of operations that can be used to change the one string into the other. For example, if we let C mean "correct", S mean "substitution", D mean "deletion" and I mean "insertion", then the edit alignment between the characters in "construction" and "distortions" would be ISSCCISSCCCCD. Here is an explanation of the alignment:
  • I: Insert a "c" ->cdistortions
  • S: Substitute "d" for "o" ->coistortions
  • S: Substitute "i" for "n" -> constortions
  • CC: Leave the "st" alone
  • I: Insert "r" -> constrortion
  • S: Substitute "u" for "o" -> contrurtion
  • S: Substitute "c" for "r" -> constructions
  • CCCC: Leave "tion" alone
  • D: delete "s" -> construction
This sort of an allignment operation is commonly used in grading the output of machine translation and speech recognition systems, though the operations are done at the word level instead of the character level. When I needed a quick piece of code to do the alignment in my own application, though, there was none to be found on the internet! So I made my own and am posting it here. The logic for determining the proper string of edit operations is taken from a StackOverflow answer here. And now the code:
	/**
	 * @return List of Operations representing allignment between list1 and
	 * list2. The allignment represents operations to change list2 into list1.
	 */
	
	public static List levenshteinAllignment(Object[] list1, Object[] list2) {
		int[][] distanceMatrix = getDistanceMatrix(list1, list2);
		List ops= new ArrayList(
				list1.length > list2.length ? list1.length : list2.length);
		//think of distance chart as going from bottom left to top right;
		//current position coordinates; start at top right.
		int row = list1.length;
		int col = list2.length;
		//could have moved to current position from three others; store their scores here.
		int diag;
		int left;
		int below;
		int current;
		
		while (row != 0 || col != 0) {
			diag = getVal(row-1,col-1,distanceMatrix);
			left = getVal(row,col-1,distanceMatrix);
			below = getVal(row-1,col,distanceMatrix);
			current = distanceMatrix[row][col];

//			   if the value in the diagonal cell (going up+left) is smaller or equal to the
//			      values found in the other two cells
				
//			   AND 
//			      if this is same or 1 minus the value of the current cell 
			if(diag <= left && diag <= below && (diag == current || diag == current - 1)){
//				   then  "take the diagonal cell"
//		         if the value of the diagonal cell is one less than the current cell:
				if(diag == current - 1)
//		            Add a SUBSTITUTION operation (from the letters corresponding to
//		            the _current_ cell)
					ops.add(new Operation(Operation.Type.SUBSTITUTION,list1[row-1],list2[col-1]));
				else
//			        otherwise: do not add an operation this was a no-operation.
					ops.add(new Operation(Operation.Type.CORRECT,list1[row-1]));
				//move diagonally
				row--;
				col--;
			}
//
//		    elseif the value in the cell to the left is smaller or equal to the value of
//		  		the cell below current cell
//		   	AND 
//	       	if this value is same or 1 minus the value of the current cell 
			else if(left < below && (left == current || left == current - 1)){
//		        add an INSERTION of the cell to the left
				ops.add(new Operation(Operation.Type.INSERTION,list2[col-1]));
				//move left
				col--;
			}
//		   	else
			else{
//		       take the cell below, add
//		       Add a DELETION operation
				ops.add(new Operation(Operation.Type.DELETION,list1[row-1]));
				//move down
				row--;
			}
		}
		Collections.reverse(ops);
		return ops;
	}
	private static int getVal(int row, int col, int[][] distanceMatrix){
		if(row < 0 || row > distanceMatrix.length)
			return Integer.MAX_VALUE;
		if(col < 0 || col > distanceMatrix.length)
			return Integer.MAX_VALUE;
		else return distanceMatrix[row][col];
	}
	public static class Operation{
		private Type type;
		private Object object1;
		private Object object2;
		public enum Type{
			CORRECT,SUBSTITUTION,DELETION,INSERTION
		}
		public Operation(Type t, Object o1){
			type = t;
			object1 = o1;
			object2 = null;
		}
		public Operation(Type t, Object o1, Object o2){
			type = t;
			object1 = o1;
			object2 = o2;
		}
		@Override
		public String toString(){
			if(type == Type.SUBSTITUTION)
				return "Substitution: " + object1.toString() + " -> " + object2.toString();
			if(type == Type.CORRECT)
				return "Correct:      " + object1.toString();
			if(type == Type.DELETION)
				return "Deletion:     " + object1.toString();
			if(type == Type.INSERTION)
				return "Insertion:    " + object1.toString();
			return null;
		}
	}

	/**
	 * 
	 * @param array of objects to compare
	 * @param array of objects to compare
	 * @return Levenshtein distance between arrays.
	 * This method uses the equals(Object o) method to compare the
	 * objects in the two arrays, returning the Levenshtein distance between them.
	 */
	public static int levenshteinDistance(Object[] list1, Object[] list2) {

		int[][] distance = getDistanceMatrix(list1, list2);
		return distance[list1.length][list2.length];
	}

	/**
	 * 
	 * @param list1
	 * @param list2
	 * @return A completely filled distance matrix; movement from [i-1][j]
	 *         represents insertion, from [i][j-1] represents deletion, and from
	 *         [i-1][j-1] represents substitution or no operation.
	 */
	private static int[][] getDistanceMatrix(Object[] list1, Object[] list2) {
		int[][] distanceMatrix = new int[list1.length + 1][list2.length + 1];

		for (int i = 0; i <= list1.length; i++)
			distanceMatrix[i][0] = i;
		for (int j = 0; j <= list2.length; j++)
			distanceMatrix[0][j] = j;

		for (int i = 1; i <= list1.length; i++)
			for (int j = 1; j <= list2.length; j++)
				distanceMatrix[i][j] = minimum(distanceMatrix[i - 1][j] + 1,// insertion
						distanceMatrix[i][j - 1] + 1,// deletion
						distanceMatrix[i - 1][j - 1]// substitution or correct
								+ ((list1[i - 1].equals(list2[j - 1])) ? 0 : 1));
		return distanceMatrix;
	}

	/**
	 * Same as Math.min, but returns the minimum of three arguments instead of
	 * two.
	 */
	private static int minimum(int a, int b, int c) {
		return Math.min(Math.min(a, b), c);
	}

For word level alignment, you can call it on Strings using the split function like so:
for(Operation o : levenshteinAllignment(
			"What My house gleams with the light of the moon and your face"
					.split(" "),
			"Your house with the light of the the moon and my face"
					.split(" "))
		)
			System.out.println(o);
And the output would be:
Deletion: What
Substitution: My -> Your
Correct: house
Deletion: gleams
Correct: with
Correct: the
Correct: light
Correct: of
Correct: the
Insertion: the
Correct: moon
Correct: and
Substitution: your -> my
Correct: face
Feel free to use and edit this as you like. Many applications disregard the strings that are correct and only output the edit operations, and that should be an easy edit.

Wednesday, August 17, 2011

Distributional Statistics of Log Values in Linear Domain

Quite a long title, sorry. Basically, I had a bunch of log values (and they needed to stay logarithmic to avoid underflow) and I wanted to compute distributional statistics on them, like mean, variance and kurtosis. I wasn't sure if it would be valid to compute these kinds of statistics on the numbers as is, so I created a class to do all of the calculations in linear space. I'll post the result here, though there may be bugs (tell me if you find some!).

One thing to remember is that if the statistics map onto a negative number in linear space then it will be impossible to take the logarithm; therefore, these are invalid operations and you have to consider this before trying to retrieve any numbers from this program. I hope someone finds this useful!

package edu.jhu.clsp.ws11.rerank.utils;

import java.util.Arrays;

/**
 * This class returns distributional statistics given a list of numbers. The numbers are assumed to
 * be in logarithmic space, and all of the computation is done on numbers converted from log to linear
 * space; the results are returned again in log space.
 * @author Nate Glenn
 *
 */
public class LogDistributionalStats {
	private double[] numbers;
	private int N;//number of numbers input
	private double logN;//log(N)
	private double min; 
	private double median;
	private double max;
	private double mean;
	private double avgAbsDeviation = 0;
	private double standardDeviation = 0;
	private double variance = 0;
	private double skew = 0;
	private double kurtosis = 0;
	private double sum;
	/**
	 * Compute statistics on nums. If norm is true, then compute statistics after normalizing
	 * the array, except for min, mean, and max.
	 * 
	 */
	public LogDistributionalStats(double[] nums, boolean norm){
		N = nums.length;
		//must make new array so as to avoid overwriting the input.
		numbers = new double[N];
		for(int i = 0; i < numbers.length; i++)
			numbers[i] = nums[i];
		logN = Math.log(N);
		//compute sum, mean, min, and max before normalization (if done at all)
		sum = sumAsLinear();
		mean = sum - logN;
		Arrays.sort(numbers);
		min = numbers[0];
		max = numbers[N-1];
		if(norm)
			ArrayUtils.minusAll(numbers,sum);
		
		double deviation;
		if(N > 1){
			for(double d : numbers){
				deviation = LogMath.linearDifference(mean, d);
				avgAbsDeviation = LogMath.addAsLinear(avgAbsDeviation, deviation);
				variance += deviation*2;
				skew += deviation*3;
				kurtosis += deviation*4;
			}
			variance -= Math.log(N-1);
			standardDeviation = variance/2;
			skew -= logN+variance+standardDeviation;
			//don't do negative 3 calculation here.
			kurtosis = kurtosis-(logN + 2*variance);
		}
		else{
			for(double d : numbers){
				deviation = LogMath.linearDifference(mean, d);
				avgAbsDeviation = LogMath.addAsLinear(avgAbsDeviation, deviation);
			}
			variance = Double.NaN;
			standardDeviation = Double.NaN;
			skew = Double.NaN;
			kurtosis = Double.NaN;
		}
		avgAbsDeviation -= logN;
		int mid = N/2;
		if(N % 2 == 0)
			median = LogMath.addAsLinear(numbers[mid-1], numbers[mid]) - Math.log(2);
		else
			median = numbers[mid];
	}
	
	/**
	 * 
	 * @param nums
	 * @return Linear space sum of all numbers in nums
	 */
	private double sumAsLinear() {
		double total = 0;
		for(double d : numbers)
			total = LogMath.addAsLinear(total, d);
		return total;
	}

	public double getMin() {
		return min;
	}

	public double getMax() {
		return max;
	}

	public double getMean() {
		return mean;
	}

	public double getStandardDeviation() {
		return standardDeviation;
	}
	
	public double getVariance() {
		return variance;
	}
	
	public double getSkew() {
		return skew;
	}

	public double getSum() {
		return sum;
	}
	
	/**
	 * Kurtosis is not calculated with any linear combinations (subtracting three)
	 * This is because it is often impossible to convert this to log space, since
	 * the final product is so often negative. If you want the minus three back again, you can
	 * try to minus it yourself and handle any exceptions (use LogMath.minusAsLinear()).
	*/
	public double getKurtosis() {
		return kurtosis;
	}

	public double getMedian() {
		return median;
	}
	
}


Friday, July 29, 2011

Parsing in a Cognitive Modeller

A colleague of mine recently expressed interest in the research I am doing for an honors thesis. "Syntactic parsing in a cognitive modeling system" isn't exactly a crystal clear expression of my work, after all. I embarrassed myself by throwing out a few cursory statements before trailing off and ending with a "it's kind of hard to explain". Why can't I explain my own work? Partly because I don't explain it very often and therefore I'm bad at it. Another reason is that I have met with opposition to it in the past. Fellow linguists are immediately fascinated with the idea of modeling language use inside of a digital brain; computer scientists remain relatively unimpressed. "There are plenty of blazing fast parsers out there, so why build one just to act like a human? Besides, you don't even know if the computer is doing the same thing we are!" I'm going to use this and the next post to organize my thoughts and try to explain how parsing works in a cognitive modeling system, and why we would even try it in the first place.
This post will address cognitive modeling, and the next will move on to parsing.
Lets pretend that some crazy scientist manages to create an unstable time portal, like Will Robinson's, and that before getting to use it himself a rather valuable computer gets sucked through instead, sending it back to sometime in the 1950s. There, a curious electrical engineer finds it, and, realizing the novelty of it, shares his discovery with the scientists of his academic community. Though the origin of the machine is certainly not surmisable, they will try their darndest to figure out how it works.
They go about this by studying two aspects of the machine: the hardware and the software. When they look inside, they observe the mass of wires, chips, resistors, capacitors, and a myriad of other tiny gadgets all somehow integrated into one perfect system. They measure voltage across different points in the circuitry and observe the flow of power from the battery into the other parts. They analyze samples of it to discover what material it is made of, and run a whole bunch of other tests that are not entirely clear to non-engineers.
Observing the software, on the other hand, is much less complicated because all they have to do is turn it on. Let's just say Will Robinson's computer was a futuristic Mac of some sort. Then turning it on greets the scientists with a welcome screen and then Will's desktop:
The scientists are intrigued by the fact that the computer can do all sorts of complicated things, like play music and videos, compress files, typeset documents and manage large spreadsheets seemingly without doing any work. Putting it in perspective, their own computers look like this. They can surmise basically that there is one central system called OS-X that runs everything else, and that each of the functions run in separate programs. They learn that each of the programs run in a window, that programs are made up of more basic functions such as file management and that certain things are impossible, like creating files with "?" in the name.
They also tinker around with the hardware to see how each piece affects the software. Through this they uncover the difference between the RAM and a hard drive, the basic purpose of the CPU, and also that the USB ports transmit information.
After years of studying the Mac, they attempt to create machines which imitate its functions. One person creates a crude screen, another makes some memory with pathetic storage size, and others draft intricate blueprints explaining how the programs function within the machine. They don't all agree on the underlying mechanisms, so they split and pursue different theories. The end product is several schools of research attempting to build the machine by studying different aspects of it. Will they ever make an actual Macintosh themselves? Not likely, with 1950s technology. How can they even tell if they've gotten it right? They can experiment with their own machine and see if it acts somewhat like the Mac.

The scientists study the hardware and software inside of the futuristic Mac.
Now, what does all of this have to do with cognitive modeling?
Wait, what is cognitive modeling? To explain that, we first need a few definitions:
  • Cognition: the excercise of human intelligence, including both deliberation and action, and performance of the wide variety of tasks that humans participate in.
  • Cognitive Science: the study of minds as information processors, including how information is processed, represented, and transformed.
Cognitive science attempt to explain cognition, or human intelligence, by discovering (or theorizing about) the processes and structures underlying it at the lowest level, analyzing the brain as if it were a computer. We study the mind in the same way those scientists studied the Mac: either by observing the actual brain (EEG, fMRI) or observing behavior (eye tracking, reaction time), or in some cases even observing what happens when certain parts break or are damaged by sickness. They've found some interesting things. For example, look at the Wikipedia article to see what what we know about different types of memory.
Cognitive scientists study and imitate the physical and behavioral aspects of the human mind.
Data from these types of experiments contribute to our understanding of the mind, but we still do not completely understand the complex processes that make humans what they are. We can't try our hand at building a human, like the scientists did the Mac, either. Besides technological concerns, there are also ethical ones. Instead, scientists create computer models to simulate human activity. Although there have been many models which simulate single aspects of cognition such as hand-eye coordination or reading, general cognitive frameworks, which model human behavior overall, have also been created.
These computational cognitive models are extremely useful to researchers because they provide a universal testing ground in which mini theories about cognition can be tested. They form what Allen Newell called Unified Theories of Cognition (UTC). The name basically means that if a researcher has a theory of how one cognitive activity works, then it should fit within the larger, unified framework already tried and tested by the scientific community. Once the mini theory is implemented within the larger framework, experiments can be run using the resulting model, which is guaranteed to exhibit the properties of the larger framework. This has the benefit of constraining the variables in one's own model, making it both easier to design and scientifically more sound.
There are several other reasons that these models are useful:
  • You don't have to pay the model to take your experiment, nor do you have to pay a technician to scan its brain while it carries out various tasks. Though initial programming and maintenance cost time and money, these models will carry out an infinite number of tasks for free. 
  • Because we can step through the execution of a program, we can see exactly what the model is "thinking" at all times (akin to "think aloud protocol"), allowing true introspection into the nature of the model and the theoretical consequences of its acceptance.
  • Even if a model is worked out meticulously by hand, humans are error prone. Running the program on a computer guarantees accurate evaluation of the model.
  • Models shared by the community provide baseline results, making work from different researchers comparable and making it easier to measure the advancement of the field.
  • Models can be shared with other researchers easily via the internet.
There are several such available frameworks, including Soar, Allen Newell's creation, and ActR, which is more popular and seems to draw more government funding.
Like the 1950s scientists, researchers in cognition have split into different schools which study different aspects of the mind. The main split is between symbolic and subsymbolic models.

  • Symbolic models focus on the abstract symbol-processing capabilities of humans; we can combine physical patterns into structures and can also produce new expressions by manipulating other structures, e.g. art, language, music.
  • Subsymbolic models focus on the neural properties of the brain; the most widely known is connectionism, which models complex behavior through connections between simple nodes.
And research thrives and will continue to thrive for a long time, advancing artificial intelligence, medicine, economics, and other sciences related to human behavior. Have we made a working human? No. Do we think we understand them? Yes, but not perfectly. Cognitive models are based on years and years of research and empirical backing. It's safe to say that human behavior can be modeled and even predicted at some level.
Here are some of the modeling projects that use a general cognitive framework:

A bunch using the COGENT framework; medical diagnoses, mental rotation, towers of Hanoi.
Learning to Play Mario using SOAR.
Pilot modeling using SOAR. Modeling pilot behavior for better mission planning and design.
Simulated students using ACT-R. The authors evaluate different instructional methods on simulated students.
Eagle Eye. Oops! That's not real. Maybe some day.

Language is an extremely complex phenomenon, but it too must be simulated in some way if we are to confirm the validity of our models. More on that next time.

Sunday, July 24, 2011

How to say "p.s. you suck" in Japanese

This post will focus on two very interesting vocabulary items in Japanese; I call them "p.s. you suck" suffixes, though I'm sure there is a better word to describe them.
Adding a め to a noun insults the noun it is suffixed to:

  • 馬鹿め!["baka-me", idiot!]
  • この小娘め!["kono komusume-me!", stupid girl!]
  • 太郎め! ["Tarou-me!", dang Tarou!]

The verb ["yagaru"] attaches to the base II or connective form of verbs and adds the same feeling as the め suffix:

  • 覚えてやがれ!["oboeteyagare!", remember this you fool!]
  • 罠にかかりやがって... ["wana ni kakariyagatte...", getting caught in a trap, you're so stupid.]
  • 何を言いやがるんだ! ["nani o iiyagarunda!", what the heck are you saying?!]

Both of these forms are used when yelling at or cursing some entity.
I personally don't use them a lot, but they are very common in anime, probably because of the dramatic nature. Here's an example for やがる. It's the last word the villain says (besides きら, a sparkly sound akin to "bwing" or "bling"). Note that the subtitle should be "Remember this!", not "I'll remember this!".

Thursday, July 21, 2011

Japanese Stackexchange

Stackoverflow is one of those sites born of an idea so golden you can hardly believe nobody has ever done it before. Using a simple system of reputation, priveleges and badges, users are encouraged to post answers to on-topic questions for the benefit of the asker and the internet community at large. I go there whenever I need help with a random programming problem. Questions are usually answered quickly because people want the points, and I don't have to feel bad about bothering anyone because asking a question gives someone the opportunity to build their reputation. Started in 2008, the site was immediately a phenomenal success and is still alive and well today. Though stackoverflow is focused on computer programming, the launch of stackexchange allowed the growth of other sites on a variety of content. area 51is sort of a sandbox for people to propose new sites and build communities to nourish and govern those sites.
Recently I was excited to find that a Japanese stackexchange beta site was ready to open. On opening day I was hooked! I answered a large portion of the questions that were answered that day. In fact, I was so hooked that I had to stop posting... I think that part of the success of stack exchange is that the design creates motivation to answer questions in a manner similar to addicting online games; reputation points aren't worth anything in real life, but somehow racking 'em up is such a satisfying experience that one keeps going back for more. I consider harvesting the power of addictive point systems for the good of mankind to be a praiseworthy accomplishment, but I must refrain myself from getting caught up in it. I will, however, not stop myself from asking questions, because however fun that may be it is not as addicting.
Anyway, I just wanted to put in a good word for the new Japanese stackexchange site. If it doesn't get enough traffic it will be shut down, so tell everyone you know who wants to learn Japanese to start asking questions! Or, if you know Japanese, start answering.  The best way to learn is to teach. Plus you get this cool banner:


profile for Nate Glenn at Japanese Language and Usage, Q&A for students, teachers, and linguists wanting to discuss the finer points of the Japanese language

Friday, June 10, 2011

Some Quốc Ngữ History

I had the privelege of studying Vietnamese for 2 semesters at my university. During the course of my studies, I had to learn the system of writing Vietnamese known as Quốc Ngữ. Seeing as how the older writing system (Chữ Nôm), which employed Chinese characters, was used up until the beginning of the 20th century, I could not understand why the spelling of such a new system was so irregular. Recently, however, I began reading An Introduction to Vietnamese Literature (Duran and Huan, 1985), which contains a section on the evolution of the quoc ngu system.
Quốc Ngữ was put together by Spanish, Italian and Portuguese missionaries, using spelling rules they were already familiar with. G and gh for example. In Italian, g is pronounced hard unless it is gi or ge, in which case it makes a j sound; Ghe and ghi are used to make a hard g in those cases. This spelling carried over to Vietnamese spelling rules; gi now makes a z or y sound, depending on the dialect, while gh is used for a hard g sound before i and e, e.g. ghế, ghi.
This and many other irregularities were taken notice of by Vietnamese and French linguists early on in the adoption of quốc Ngữ. Several times reforms were put forward for consideration, but all of them came to nothing. For example, in 1902, at the First International Congress of Far Eastern Studies in Hanoi, Vietnam's first Transliteration Board suggested the following improvments:

  • g should be kept for the series ga, ge, go, gó, gu, gú; gi should be replaced by j;
  • the k sound should always be spelled k, in order to avoid having ca and co on the one hand and ke and ki on the other.
  • â, the sound of which is close to o, should be replaced by hooked a;
  • y should be substituted for i in ly, ky, my, etc.;
  • ch should be written as č or c, as h should be reserved for aspiration in kh, th, etc. e.g. co for cho ("market"), khac for khach ("guest, foreigner");
  • d should be replaced by z;
  • n should be used in place of nh (ban for banh ["cake, bread"]);
  • w should be used to denote the semi-vowel (kwa rather than qua ["to pass"]);
  • s should signify a retroflex s;
  • x, the sound of which is close to that of the palatal sibilant, should be replaced by ç.


    All of the changes made sense phonemically (provided that the change for n occurred only word finally), but none of them caught on. Supposedly force of habit, along with the difficulty of altering fonts for printing, were the cause.
    I wonder how much Vietnamese has changed since then. I also wonder how the petrification of quoc ngu irregularities may have affected modern Vietnamese; spelling, when internalized by a native speaker, can affect pronunciation (sometimes by overcorrection). My teacher was adamant that I pronounce khach with a ch sound at the end.
    The book is on google here.