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

前端 未结 4 1640
青春惊慌失措
青春惊慌失措 2020-12-05 03:56

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 correc

4条回答
  •  执笔经年
    2020-12-05 04:11

    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.

提交回复
热议问题