当前位置: 首页>>代码示例>>Python>>正文


Python Document.setTotalTfIdf方法代码示例

本文整理汇总了Python中document.Document.setTotalTfIdf方法的典型用法代码示例。如果您正苦于以下问题:Python Document.setTotalTfIdf方法的具体用法?Python Document.setTotalTfIdf怎么用?Python Document.setTotalTfIdf使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在document.Document的用法示例。


在下文中一共展示了Document.setTotalTfIdf方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: handleQuery

# 需要导入模块: from document import Document [as 别名]
# 或者: from document.Document import setTotalTfIdf [as 别名]
def handleQuery(queryStr):
  queryTerms = queryStr.split()
  stopwordsInQuery = []
  for term in queryTerms:
    if isInvalidTerm(term):
      print("Warning: Invalid query, \"" + term + "\" is not a valid query term")
      queryTerms.remove(term)
    elif term in stopwords:
      stopwordsInQuery.append(term)
  
  if len(queryTerms) == 0:
    print("Error: Invalid query, no valid query terms")
    return
  elif len(stopwordsInQuery) == len(queryTerms):
    print("Error: Invalid query, all terms are stopwords")
    return
  elif len(stopwordsInQuery) > 0:
    print("Warning: The following terms were ignored because they are stopwords:")
    printList(stopwordsInQuery)
    print()
    queryTerms = diff(queryTerms, stopwordsInQuery)

  # Calculate IDFs
  idfs = {}
  numDocuments = float(len(documents))
  # print("numDocuments: " + str(numDocuments))
  for word in allWords:
    # print("DocFreq: " + str(getDocumentFreq(word)))
    idf = math.log10(numDocuments / getDocumentFreq(word))
    idfs.update({word: idf})

  # print("IDF's:")
  # printDict(idfs)

  # Calculate tf-idfs
  for document in documents:
    # print("Doc #" + str(document.id))
    for term in document.terms:
      term.setTfIdf(idfs.get(term.text))

  # Create query as document
  queryDocument = Document(-1, "Query", queryStr, queryTerms, "query")
  for term in queryDocument.terms:
    # print("Query term")
    idfForQueryTerm = idfs.get(term.text)
    if idfForQueryTerm:
      term.setTfIdf(idfForQueryTerm)
    else:
      term.setTfIdf(0)

  for document in documents:
    # print("Doc #" + str(document.id))
    document.setTotalTfIdf()

  # print("Query Doc")
  queryDocument.setTotalTfIdf()
  
  for document in documents:
    # print("Doc #" + str(document.id))
    for term in document.terms:
      term.setSimilarity(document.totalTfIdf)

  for term in queryDocument.terms:
    # print("Query term")
    term.setSimilarity(queryDocument.totalTfIdf)

  for document in documents:
    # print("Doc #" + str(document.id))
    document.setTotalSimilarity(queryDocument.terms)
    document.addTitleBonus(queryDocument.terms)
    # print(document)

  printTopDocuments()
开发者ID:kellenschmidt,项目名称:webcrawler,代码行数:75,代码来源:crawler.py


注:本文中的document.Document.setTotalTfIdf方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。