dongxian6715 2009-06-18 20:10
浏览 126
已采纳

LSA - 潜在语义分析 - 如何用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!

  • 写回答

4条回答 默认 最新

  • doukanhua0752 2009-06-24 15:17
    关注

    LSA links:

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退
  • ¥20 win系统的PYQT程序生成的数据如何放入云服务器阿里云window版?