Last time, I wrote about using cosine similarity as a way to tell how alike or different two trigram language models were. This post describes a second distance-like measure, from Cavnar and Trenkle's 1994 paper, "N-Gram-Based Text Categorization", which can be found on the web. The way Cavnar and Trenkle (C&T) compute the distance between models is not via cosine similarity, but by using the ranks of the sorted trigram frequencies.
Here's a simple example of C&T's method: suppose you have two lists of trigrams, and we want to make a distance measure that will tell us how alike the lists are.
list X: RAN CAT CAT THE THE THE RED
list Y: THE CAT RAN RED RAN CAT RED
We compute the frequencies of the trigrams in the lists, sort the lists in descending frequency, rank the resulting lists, and compute the sum of the differences of the ranks. As worked out:
X freqs RAN=1 CAT=2 THE=3 RED=1
sort down by freq THE CAT RAN RED
rank 1 2 3 4
Y freqs THE=1 CAT=2 RAN=2 RED=2
sort down by freq CAT RAN RED THE
rank 1 2 3 4
rank diffs THE(1-4) CAT(2-1) RAN(3-2) RED(4-3)
absolute diffs 3 1 1 1
sum of abs diffs 6 = 3+1+1+1
Here, lists X and Y are trigrams. We compute the differences between the ranks of the lists as sorted in descending order by frequence, then take the absolute values of these differences, and sum these absolute values. The resulting sum (6 in this example) is a measure of the mismatch between the two lists.
Cavnar and Trenkle call this measure of mismatch an "out-of-place measure". In this posting, I call the measure of mismatch the rank distance between the two lists.
When an element is in both lists, the magnitude of the rank differences for that element tells us how far out of place that element is. When an element is not in both lists, we arbitrarily set the rank difference to N (the length of the lists). The values of rank distance vary from 0 (both lists have the same elements with the same frequency rankings) to N*N (the two lists share no common elements).
For two trigram models (from a reference text and a sample text), we can use the Cavnar & Trenkle out-of-place measure as a distance-like way to test the similarity between two models -- lower rank distance implies greater similarity.
The process described above is not exactly what Cavnar & Trenkle proposed. C&T collect frequencies for all N-grams (N=1,2,3,4,5), not just trigrams and take
the top 400 most-frequent N-grams in descending frequency order as their language models. I've tested
a simplified variant of C&T (only use trigrams, take the top 300
for a language model) against the Python Recipes code, and the
quality of classification results are similar, when used just for
language detection.
For reference, the C&T 1994 paper is available from
http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.162.2994
No comments:
Post a Comment