首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >找不到python、Stemmer

问找不到python、Stemmer
EN

Stack Overflow用户
提问于 2018-04-04 00:05:32
回答 3查看 9.2K关注 0票数 3

我从github得到了这段代码,这段代码可以在64位的windows机器上执行。

下面是我得到的错误:

回溯(最近一次调用):导入词干分析器中的文件"new.py",第2行

ModuleNotFoundError:没有名为“”stemmer“”的模块“

代码语言:javascript
复制
import math
import stemmer

def irange(sequence):
   return zip(range(len(sequence)), sequence)

class CosineScore(object):
    def __init__(self,all_docs):
      self.documents = all_docs #list all docs [doc1,doc2..]
       self.ndocs = len(all_docs)
    self.posting_list = {} #term frequency list, don't care about term position
     #term => {docId => freq}
       self.pstemmer = stemmer.PorterStemmer()

    self._term_indexer()

def _term_indexer(self):
    #Create term frequency dict
    #Run each word through stemmer
    for doc_id,document in irange(self.documents):
        for word in document.split(' '):
            s_word = self.pstemmer.stem(word)
            if self.posting_list.has_key(s_word):
                doc_id_mapping = self.posting_list[s_word]
                if doc_id_mapping.has_key(doc_id):
                    doc_id_mapping[doc_id] += 1
                else:
                    doc_id_mapping[doc_id] = 1
            else:
                self.posting_list[s_word] = {doc_id: 1}

def _term_frequency(self,term):
    if self.posting_list.has_key(term):
        return self.posting_list[term]
    else:
        return -1

def _listToString(self,arg):
    if isinstance(arg,basestring):
        return arg.split(' ')

def __qTermFrequency(self,term,bWords):
    count =0
    for i,bWordsObj in irange(bWords):
        if bWordsObj == term:
            count = count +1
    return count

def _docListWeights(self) :

    all_terms = self.posting_list.keys()
    doclist_weights = [0.0] * self.ndocs 

    #for all terms in the corpus
    for i,term in irange(all_terms):
        #for all docs in corpus that contain this term
        docs = self.posting_list[term].keys()
        for j,doc_id in irange(docs):
            tf = self.posting_list[term][doc_id]
            tfSquared = (tf * tf)
            doclist_weights[doc_id] += tfSquared 

        for k in range(self.ndocs):
            doclist_weights[k] = math.sqrt(doclist_weights[k])
    return doclist_weights

def compute(self,query,mIDF=0):
    '''
    dft - document term frequency
    idf - inverse document frequency
    wTQ - weights for each query term
    mIDF - max tf normalization
    '''

    scores = [0.0] * self.ndocs
    bWords = self._listToString(query)
    normalizationFactor = self._docListWeights() 

    for qterm in bWords:
        term = self.pstemmer.stem(qterm)
        #calculate WT
        #dft =  __qTermFrequency(queryTerm,bWords)
        #wTQ = math.log10(int(N)/dft) 

        term_posting_doclist = []
        if self._term_frequency(term) != -1:
            #Find all documents with this query term 

            term_posting_doclist = self.posting_list[term].keys()
            #total_term_frequency_in_corpus = sum(self.posting_list[term].values())

            if(mIDF!=0):
                dft = mIDF
            else:
                dft = len(term_posting_doclist) 

            _wTQ = float(self.ndocs)/float(dft)
            wTQ = math.log10(float(_wTQ)) #idf

        #cosinescore algorithm
        for doc_id in term_posting_doclist:
            if normalizationFactor[doc_id] != 0:
                #wFTD = termDocFrequencyList/ normalizationFactor(doc_id) 
                wFTD = self.posting_list[term][doc_id] / float(normalizationFactor[doc_id])    
            else:
                wFTD = 0.0

            scores[doc_id] +=  (wTQ * wFTD)
    return scores

if __name__ == "__main__":
    docs = [  "mallya","mallya mallya in hawaii", "sunil" ]
    q = "hawaii mallya"
    cs = CosineScore(docs)
    print (cs.compute(q))
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-04-04 00:19:07

它很可能是nltk,您可以使用以下命令安装它:

代码语言:javascript
复制
pip install nltk

将import stemmer更改为import nltk.stem as stemmer

然后运行代码。请注意,此代码是Python2.7中的代码,如果您有Python3,则不会运行此代码

票数 3
EN

Stack Overflow用户

发布于 2019-10-21 17:02:11

Stemmer是一个软件包,可以通过pip安装为PyStemmer。它只用在非常粗糙的"is real word“过滤器中。

代码语言:javascript
复制
pip install PyStemmer

现在这个版本可能还有其他一些问题。

票数 2
EN

Stack Overflow用户

发布于 2019-05-15 14:43:33

使用:

代码语言:javascript
复制
pip install stemmer

在命令提示符下,如果该命令不起作用,请执行以下操作。

首先,手动从以下位置下载文本挖掘包:https://pypi.python.org/pypi/textmining/1.0

  • Unzip it ( textmining-1.0

  • type textmining-1.0.zip)您将在anconda提示符中获得一个名为C:\ProgramData\Anaconda3

  • Copy
  1. 的文件夹,然后查看此目录的活动环境位置:C:\ProgramData\Anaconda3
  2. Copy paste unzipped -1.0 conda info in this directory
    1. 将该文件夹转换为python 3:为此,请将其粘贴到anaconda提示符中并运行

2to3 -- textmining-1.0

  • After -dir= textmining-1.0_v3 -W -n目录将文件夹转换为python 3将textmining-1.0重命名为textmining-1.0_v3

最后,通过在anaconda提示符中键入以下代码来安装相同的程序

cd文本挖掘-1.0_v3

如下所示

C:\Users\user>cd文本挖掘-1.0_v3

键入以下代码python setup.py install

C:\Users\user \textmining-1.0_v3>python setup.py安装

现在,您将成功地消除error

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49634536

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档