使用哈希表(字典)的invertdindex实现

hashedindex的Python项目详细描述


TravisCIAppVeyorCodeCovPyPi

使用哈希表(python字典)实现快速简单的invertdindex。

支持Python3.5+

免费软件:BSD许可证

安装

安装hashindex的最简单方法是通过pypi

pip install hashedindex

功能

^ TT1}$提供了一个简单易用的倒排索引结构,它足够灵活,可以与各种用例一起工作。

基本用法:

importhashedindexindex=hashedindex.HashedIndex()index.add_term_occurrence('hello','document1.txt')index.add_term_occurrence('world','document1.txt')index.get_documents('hello')Counter({'document1.txt':1})index.items(){'hello':Counter({'document1.txt':1}),'world':Counter({'document1.txt':1})}example='The Quick Brown Fox Jumps Over The Lazy Dog'forterminexample.split():index.add_term_occurrence(term,'document2.txt')

hashedindex不限于字符串,任何哈希对象都可以被索引。

index.add_term_occurrence('foo',10)index.add_term_occurrence(('fire','fox'),90.2)index.items(){'foo':Counter({10:1}),('fire','fox'):Counter({90.2:1})}

文本分析

hashedindex模块附带了一个功能强大的textparser模块,其中包含要拆分的方法 将文本转换为标记。

fromhashedindeximporttextparserlist(textparser.word_tokenize("hello cruel world"))[('hello',),('cruel',),('world',)]

由于能够指定所需的任意数量的n-grams,令牌被包装在元组中:

list(textparser.word_tokenize("Life is about making an impact, not making an income.",ngrams=2))[(u'life',u'is'),(u'is',u'about'),(u'about',u'making'),(u'making',u'an'),(u'an',u'impact'),(u'impact',u'not'),(u'not',u'making'),(u'making',u'an'),(u'an',u'income')]

查看函数的docstring以获取有关如何使用stopwords的信息,指定 min_lengthignore_numeric术语。

与Numpy和Pandas集成

hashedindex背后的想法是提供一种非常快速和简单的生成方法 机器学习矩阵与额外使用的numpy,pandas和scikit学习。 例如:

fromhashedindeximporttextparserimporthashedindeximportnumpyasnpindex=hashedindex.HashedIndex()documents=['spam1.txt','ham1.txt','spam2.txt']fordocindocuments:withopen(doc,'r')asfp:fortermintextparser.word_tokenize(fp.read()):index.add_term_occurrence(term,doc)# You *probably* want to use scipy.sparse.csr_matrix for better performanceX=np.as_matrix(index.generate_feature_matrix(mode='tfidf'))y=[]fordocinindex.documents():y.append(1if'spam'indocelse0)y=np.asarray(doc)fromsklearn.svmimportSVCclassifier=SVC(kernel='linear')classifier.fit(X,y)

您还可以将功能矩阵扩展到更详细的pandas数据帧:

importpandasaspdX=index.generate_feature_matrix(mode='tfidf')df=pd.DataFrame(X,columns=index.terms(),index=index.documents())

代码中的所有方法都有很高的测试覆盖率,因此您可以确保所有方法都按预期工作。

报告错误

找到虫子了吗?很好,发现的虫子就是修复的虫子。打开一个问题或者更好,打开一个请求。

历史记录

0.5.0(2019-07-21)

  • 放弃对Python2.7和3.4的支持

0.1.0(2015-01-11)

  • pypi上的第一个版本。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
Java Hibernate@ManyToMany mapping只在一个方向上在数据库中添加记录   java将文件上载到tomcat服务器外部的文件夹   java将摄像头捕获的图像上传到服务器   java如何创建Rest API并为进程添加时间延迟?   springmodulesvalidation0中缺少java注释包。8a源文件   如何在java中打印SOAP头   Spring security中的java自定义消息,包括UserDetailsService实现和异常   java如何使用Htmlunit中的表单数据登录站点   web如何在WildFly上自动运行java文件   java如何从已经使用另一个方法传递的参数的方法中获取返回值?   java我在JFrame上有一个索引越界。setContentPane   java中的循环序列/系列打印   java maven 3 webapp没有要运行的测试吗?   java CORS不允许POST请求   java再次在派生类中的Jackson中添加字段,该字段在基类中被忽略   爪哇坑测试显示仆从由于超时而异常退出   java寻找第10001个素数   java jboss是否更改web应用程序上下文根?