defBagOfWords ( self ):
""" 返回字典,中的真题整理包含单词(键)及其频率(值)"""
returnself 。文本__bag_of_words
defWordFreq ( self ,分类 word ):
"""
如果word 在self 中,则返回单词 """ 的面试频率。__bag_of_words :
返回self 。精选__bag_of_words [单词]
其他:
返回0
文档类
classDocument( object):
""" 用于学习(训练)文档和测试文档。干货
如果应该训练分类器,中的真题整理
可选参数 lear__必须设置为 True。文本如果是分类测试文档,learn 必须是面试设置为 False。"""_vocabulary = BagOfWords ()
def__init__( self ,精选 词汇表):
self . __name = ""
self . __document_class = None
self 。_words_and_freq = BagOfWords ()
文档。干货_vocabulary = 词汇
DEFread_document (自,中的真题整理文件名,文本 学习=假):
“”,分类” A 读取原稿假定该文件是在 UTF-8 或在异 8859 任一编码的...(Latin-1 的)。
所述的字文档存储在词袋中,即 self._words_and_freq = BagOfWords() """
try:
text = open( filename , "r" , encoding = 'utf-8' ) 。read ()
除了UnicodeDecodeError:
text = open( filename, "r" , encoding = 'latin-1' ) 。阅读()
文本 = 文本。下()
词 = re 。拆分( r "\W" ,文本)
自我。_number_of_words = 0
forword inwords :
self 。_words_and_freq 。add_word ( word )
如果学习:
文档。_词汇。add_word (字)
DEF__add__(自,其他):
“”,“重载‘+’。操作员添加两个文件在于添加文件的 BagOfWords‘’”
RES = 文献(文献。_vocabulary )
水库。_words_and_freq = self 。_words_and_freq + 其他。_words_and_freq
返回资源
DEFvocabulary_length (自):
“”,“返回的词汇的长度”“”
返回len 个(文献。_vocabulary )
defWordsAndFreq ( self ):
""" 返回字典,包含
文档的 BagOfWords 属性中
包含的单词(键)及其频率(值)__"""returnself 。_words_and_freq 。BagOfWords ()
defWords ( self ):
""" 返回 Document 对象 """
d = self_的单词_。_words_and_freq 。BagOfWords ()
返回d 。键()
defWordFreq ( self , word ):
""" 返回单词 "word" 在文档 """
bow = self 中出现的次数。_words_and_freq 。BagOfWords ()
ifword inbow :
returnbow [ word ]
else:
return0
def__and__( self , other ):
""" 两个文档的交集。返回两个文档中出现的单词列表 """
intersection = []
words1 = self 。字()
的话 在其他。字():
如果字 在words1 :
交叉点 + = [字]
返回相交
类别/文件集
这是由一个类别/类的文档组成的类。我们使用术语类别而不是“类”,这样它就不会与 Python 类混淆:
class类别(文档):
def__init__(自我, 词汇):
文档。__init__(自我, 词汇)
自我。_number_of_docs = 0
defProbability ( self , word ):
""" 返回给定类 "self" """
voc_len = Document_的单词 "word" 的概率_。_词汇。len ()
SumN = 0
fori inrange( voc_len ):
SumN = Category 。_词汇。WordFreq (字)
N = 自我。_words_and_freq 。WordFreq ( word )
erg = 1 + N
erg /= voc_len + SumN
返回erg
DEF__add__(自,其他):
“”,“重载‘+’。操作员增加两个类别中的对象在于添加
了分类对象 BagOfWords‘’”
RES = 类别(自我。_vocabulary )
水库。_words_and_freq = self 。_words_and_freq + 其他。_words_and_freq
返回资源
defSetNumberOfDocs ( self , number ):
self 。_number_of_docs = 数字
defNumberOfDocuments ( self ):
返回self 。_number_of_docs
池类
池是类,在其中训练和保存文档类:
类池(对象):
def__init__(自我):
自我。__document_classes = { }
self 。__vocabulary = BagOfWords ()
defsum_words_in_class ( self , dclass ):
""" 一个 dclass 的所有不同单词出现在一个类中的次数 """
sum= 0
forword inself 。__词汇。Words ():
WaF = self 。__document_classes [ dclass ] 。WordsAndFreq ()
ifword inWaF :
sum+= WaF [ word ]
returnsum
deflearn ( self , directory , dclass_name ):
""" directory 是一个路径,其中可以找到名为 dclass_name 的类的文件 """
x = Category ( self . __vocabulary )
dir= os 。listdir ( directory )
forfile indir:
d = Document ( self . __vocabulary )
#print(directory + "/" + file)
d . 阅读文档(目录 + "/" + 文件, 学习 = True )
x = x + d
self 。__document_classes [ dclass_name ] = x
x 。SetNumberOfDocs ( len( dir))
defProbability ( self , doc , dclass = "" ):
"""计算给定文档 doc""" 类 dclass 的概率,
如果dclass :
sum_dclass = self 。sum_words_in_class ( dclass )
概率 = 0
d = 文献(自我。__vocabulary )
d 。read_document (文档)
对于j 在self 。__document_classes :
sum_j = self 。sum_words_in_class ( j )
prod = 1
fori ind 。Words ():
wf_dclass = 1 + self 。__document_classes [ dclass ] 。WordFreq ( i )
wf = 1 + self 。__document_classes [ j ] 。词频( i )
r = wf * sum_dclass / ( wf_dclass * sum_j )
prod *= r
prob += prod * self 。__document_classes [ j ] 。NumberOfDocuments () / 自我。__document_classes [ dclass ] 。NumberOfDocuments ()
ifprob != 0 :
return1 / prob
else:
return- 1
else:
prob_list = []
fordclass inself 。__document_classes :
prob = self 。概率(doc , dclass )
prob_list 。追加([ dclass , prob ])
prob_list 。sort ( key = lambdax : x [ 1 ], reverse = True )
返回prob_list
defDocumentIntersectionWithClasses ( self , doc_name ):
res = [ doc_name ]
fordc inself 。__document_classes :
d = 文献(自我。__vocabulary )
d 。read_document ( doc_name , learn = False )
o = self 。__document_classes [直流] & d
intersection_ratio = len( o ) / len( d . Words ())
res += ( dc , cross_ratio )
返回res
最后
Python 崛起并且风靡,因为优点多、应用领域广、被大牛们认可。学习 Python 门槛很低,但它的晋级路线很多,通过它你能进入机器学习、数据挖掘、大数据,CS 等更加高级的领域。Python 可以做网络应用,可以做科学计算,数据分析,可以做网络爬虫,可以做机器学习、自然语言处理、可以写游戏、可以做桌面应用…Python 可以做的很多,你需要学好基础,再选择明确的方向。这里给大家分享一份全套的 Python 学习资料,给那些想学习 Python 的小伙伴们一点帮助!
??Python 所有方向的学习路线??
Python 所有方向的技术点做的整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。
??Python 必备开发工具??
工欲善其事必先利其器。学习 Python 常用的开发软件都在这里了,给大家节省了很多时间。
??Python 全套学习视频??
我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了。
??实战案例??
学 python 就与学数学一样,是不能只看书不做题的,直接看步骤和答案会让人误以为自己全都掌握了,但是碰到生题的时候还是会一筹莫展。
因此在学习 python 的过程中一定要记得多动手写代码,教程只需要看一两遍即可。
??大厂面试真题??
我们学习 Python 必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有阿里大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。