Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LSA - Latent Semantic Analysis - How to code it in PHP?

I would like to implement Latent Semantic Analysis (LSA) in PHP in order to find out topics/tags for texts.

Here is what I think I have to do. Is this correct? How can I code it in PHP? How do I determine which words to chose?

I don't want to use any external libraries. I've already an implementation for the Singular Value Decomposition (SVD).

  1. Extract all words from the given text.
  2. Weight the words/phrases, e.g. with tf–idf. If weighting is too complex, just take the number of occurrences.
  3. Build up a matrix: The columns are some documents from the database (the more the better?), the rows are all unique words, the values are the numbers of occurrences or the weight.
  4. Do the Singular Value Decomposition (SVD).
  5. Use the values in the matrix S (SVD) to do the dimension reduction (how?).

I hope you can help me. Thank you very much in advance!

like image 352
caw Avatar asked Jun 18 '09 20:06

caw


People also ask

How is knowledge represented in latent semantic analysis?

In LSA, knowledge is represented as vectors in a high-dimensional 'semantic space. ' Concepts in the input text are located in the semantic space and neighbors are activated.

Is LSA word embedding?

Latent Semantic Analysis (LSA) is a bag of words method of embedding documents into a vector space. Each word in our vocabulary relates to a unique dimension in our vector space. For each document, we go through the vocabulary, and assign that document a score for each word. This gives the document a vector embedding.

Where is latent semantic analysis used?

LSA is primarily used for concept searching and automated document categorization. However, it's also found use in software engineering (to understand source code), publishing (text summarization), search engine optimization, and other applications.


1 Answers

LSA links:

  • Landauer (co-creator) article on LSA
  • the R-project lsa user guide

Here is the complete algorithm. If you have SVD, you are most of the way there. The papers above explain it better than I do.

Assumptions:

  • your SVD function will give the singular values and singular vectors in descending order. If not, you have to do more acrobatics.

M: corpus matrix, w (words) by d (documents) (w rows, d columns). These can be raw counts, or tfidf or whatever. Stopwords may or may not be eliminated, and stemming may happen (Landauer says keep stopwords and don't stem, but yes to tfidf).

U,Sigma,V = singular_value_decomposition(M)

U:  w x w
Sigma:  min(w,d) length vector, or w * d matrix with diagonal filled in the first min(w,d) spots with the singular values
V:  d x d matrix

Thus U * Sigma * V = M  
#  you might have to do some transposes depending on how your SVD code 
#  returns U and V.  verify this so that you don't go crazy :)

Then the reductionality.... the actual LSA paper suggests a good approximation for the basis is to keep enough vectors such that their singular values are more than 50% of the total of the singular values.

More succintly... (pseudocode)

Let s1 = sum(Sigma).  
total = 0
for ii in range(len(Sigma)):
    val = Sigma[ii]
    total += val
    if total > .5 * s1:
        return ii

This will return the rank of the new basis, which was min(d,w) before, and we'll now approximate with {ii}.

(here, ' -> prime, not transpose)

We create new matrices: U',Sigma', V', with sizes w x ii, ii x ii, and ii x d.

That's the essence of the LSA algorithm.

This resultant matrix U' * Sigma' * V' can be used for 'improved' cosine similarity searching, or you can pick the top 3 words for each document in it, for example. Whether this yeilds more than a simple tf-idf is a matter of some debate.

To me, LSA performs poorly in real world data sets because of polysemy, and data sets with too many topics. It's mathematical / probabilistic basis is unsound (it assumes normal-ish (Gaussian) distributions, which don't makes sense for word counts).

Your mileage will definitely vary.

Tagging using LSA (one method!)

  1. Construct the U' Sigma' V' dimensionally reduced matrices using SVD and a reduction heuristic

  2. By hand, look over the U' matrix, and come up with terms that describe each "topic". For example, if the the biggest parts of that vector were "Bronx, Yankees, Manhattan," then "New York City" might be a good term for it. Keep these in a associative array, or list. This step should be reasonable since the number of vectors will be finite.

  3. Assuming you have a vector (v1) of words for a document, then v1 * t(U') will give the strongest 'topics' for that document. Select the 3 highest, then give their "topics" as computed in the previous step.

like image 130
Gregg Lind Avatar answered Oct 25 '22 10:10

Gregg Lind