【问题标题】:How to make the basic inverted index program more pythonic如何让基本的倒排索引程序更 Pythonic
【发布时间】:2014-07-01 23:50:45
【问题描述】:

我有如下的倒排索引代码。但是我对它不太满意,想知道如何使它更紧凑和 pythonic

class invertedIndex(object):


  def __init__(self,docs):
     self.docs,self.termList,self.docLists=docs,[],[]

     for index,doc in enumerate(docs):

        for term in doc.split(" "):
            if term in self.termList:
                i=self.termList.index(term)
                if index not in self.docLists[i]:
                    self.docLists[i].append(index)

            else:
                self.termList.append(term)
                self.docLists.append([index])  

  def search(self,term):
        try:
            i=self.termList.index(term)
            return self.docLists[i]
        except:
            return "No results"





docs=["new home sales top forecasts june june june",
                     "home sales rise in july june",
                     "increase in home sales in july",
                     "july new home sales rise"]

i=invertedIndex(docs)
print invertedIndex.search("sales")

【问题讨论】:

    标签: python machine-learning nlp inverted-index


    【解决方案1】:

    将文档索引存储在 Python set 中,并使用 dict 来引用每个术语的“文档集”。

    from collections import defaultdict
    
    class invertedIndex(object):
    
      def __init__(self,docs):
          self.docSets = defaultdict(set)
          for index, doc in enumerate(docs):
              for term in doc.split():
                  self.docSets[term].add(index)
    
      def search(self,term):
            return self.docSets[term]
    
    docs=["new home sales top forecasts june june june",
                         "home sales rise in july june",
                         "increase in home sales in july",
                         "july new home sales rise"]
    
    i=invertedIndex(docs)
    print i.search("sales") # outputs: set([0, 1, 2, 3])
    

    set 有点像列表,但它是无序的,不能包含重复的条目。

    defaultdict 基本上是一个dict,当没有可用数据(在本例中为空集)时,它具有默认类型。

    【讨论】:

    • defaultdict(set) 是一种简洁的方式
    【解决方案2】:

    此解决方案与@Peter Gibson 的解决方案几乎相同。在这个版本中,索引数据,不涉及委托docSets对象。这使得代码更短更清晰。

    代码还保留了文档的原始顺序...这是一个错误,我更喜欢 Peter 的 set() 实现。

    另请注意,对不存在的术语(如ix['garbage'])的引用会隐式修改索引。如果唯一的API是search,这很好,但这种情况值得注意。

    来源

    class InvertedIndex(dict):
        def __init__(self, docs):
            self.docs = docs
    
            for doc_index,doc in enumerate(docs):
                for term in doc.split(" "):
                    self[term].append(doc_index)
    
        def __missing__(self, term):
            # operate like defaultdict(list)
            self[term] = []
            return self[term]
    
        def search(self, term):
            return self.get(term) or 'No results'
    
    
    docs=["new home sales top forecasts june june june",
          "home sales rise in july june",
          "increase in home sales in july",
          "july new home sales rise",
          'beer',
          ]
    
    ix = InvertedIndex(docs)
    print ix.__dict__
    print
    print 'sales:',ix.search("sales")
    print 'whiskey:', ix.search('whiskey')
    print 'beer:', ix.search('beer')
    
    print '\nTEST OF KEY SETTING'
    print ix['garbage']
    print 'garbage' in ix
    print ix.search('garbage')
    

    输出

    {'docs': ['new home sales top forecasts june june june', 'home sales rise in july june', 'increase in home sales in july', 'july new home sales rise', 'beer']}
    
    sales: [0, 1, 2, 3]
    whiskey: No results
    beer: [4]
    
    TEST OF KEY SETTING
    []
    True
    No results
    

    【讨论】:

    • 子类化 dict 是一个不错的解决方案 - 您可以免费获得 __str__, __repr__, __eq__ 等内容
    • 谢谢你们两个。接下来我正在尝试尽可能简洁地编写向量空间模型,并将使用这些方法
    • 我有一个关于 shavenwarthog 的后续问题。使用这种方法,我如何限制从此类外部设置键。例如,如果有人做了 ix["garbage"],它将向 ix 对象添加一个空列表作为 {"garbage"=[]}。这是不可取的
    • @sarathjoseph,嗯,是的。我知道这是不可取的,但整体用例是什么?我以为 API 是 ix.search('garbage'),它按预期提供了 "No results"。我已经更新了代码和输出来说明。
    • 搜索的行为与预期的一样。这应该不是太大的问题,我可能会让事情正常进行。但是有没有办法让 ix 对象除了条款之外没有其他键。
    猜你喜欢
    • 2012-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-23
    • 1970-01-01
    相关资源
    最近更新 更多