python - gensim.similarities.SparseMatrixSimilarity 得到分割错误

标签 python c++ segmentation-fault gensim

我想获得一个文档与其他文档的相似度。我用的是gensim。程序可以正常运行,但在一些步骤后退出并出现 Segmentation fault。

下面是我的代码:

from gensim import corpora, models, similarities
docs = [['Looking', 'for', 'the', 'meanings', 'of', 'words'],
        ['phrases'],
        ['and', 'expressions'],
        ['We', 'provide', 'hundreds', 'of', 'thousands', 'of', 'definitions'],
        ['synonyms'],
        ['antonyms'],
        ['and', 'pronunciations', 'for', 'English', 'and', 'other', 'languages'],
        ['derived', 'from', 'our', 'language', 'research', 'and', 'expert', 'analysis'],
        ['We', 'also', 'offer', 'a', 'unique', 'set', 'of', 'examples', 'of', 'real', 'usage'],
        ['as', 'well', 'as', 'guides', 'to:']]
dictionary = corpora.Dictionary(docs)
corpus = [dictionary.doc2bow(text) for text in docs]
nf=len(dictionary.dfs)
index = similarities.SparseMatrixSimilarity(corpus, num_features=nf)
phrases = [['This',
            'section',
            'gives',
            'guidelines',
            'on',
            'writing',
            'in',
            'everyday',
            'situations'],
           ['from',
            'applying',
            'for',
            'a',
            'job',
            'to',
            'composing',
            'letters',
            'of',
            'complaint',
            'or',
            'making',
            'an',
            'insurance',
            'claim'],
           ['There',
            'are',
            'plenty',
            'of',
            'sample',
            'documents',
            'to',
            'help',
            'you',
            'get',
            'it',
            'right',
            'every',
            'time'],
           ['create',
            'a',
            'good',
            'impression'],
           ['and',
            'increase',
            'the',
            'likelihood',
            'of',
            'achieving',
            'your',
            'desired',
            'outcome']]
phrase2word=[dictionary.doc2bow(text,allow_update=True) for text in phrases]
sims=index[phrase2word]

在get sims之前可以正常运行,但是不能get sims,使用gdb得到如下信息:

Program received signal SIGSEGV, Segmentation fault. 0x00007fffd881d809 in csr_tocsc (n_row=5, n_col=39, Ap=0x4a4eb10, Aj=0x9fc6ec0, Ax=0x1be4a00, Bp=0xa15f6a0, Bi=0x9f3ee80, Bx=0x9f85f60) at scipy/sparse/sparsetools/csr.h:411 411
scipy/sparse/sparsetools/csr.h: 没有那个文件或目录.

最佳答案

我已经从github得到了答案

主要原因是num_features要和dictionary.dfs一致

关于python - gensim.similarities.SparseMatrixSimilarity 得到分割错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48661163/

相关文章:

Python Pandas 错误栏未绘制以及如何自定义索引

c - 两个不相关的 C 代码放在一起时出现段错误

c - C 链表新手的段错误帮助

c++ - 用于测量 GC 事件的 V8 垃圾收集器回调

C++:使用 libcurl 和流

c++ - 在 C++ 中保持对象持久化?

python - 将 NumPy 数组矢量化重新标记为连续数字并检索回来

python - 在PyQt中取消后台任务(终止QThread)

python - 如何使用 R 或 python 获取两个节点之间的链接

c++ - 从给定视点将 3D 点云光线转换到 2D 图像