python爬虫学习第二十九天
来源:互联网 发布:java安全等级设置 编辑:程序博客网 时间:2024/05/16 23:46
今天的主要内容:马尔可夫模型
Google的page rank算法就是基于马尔可夫模型,把网站做为节点,入站/出站链接 做为节点之间的连线。连接某一个节点的“可能性”(likelihood)表示一个网站的相对关注度
练习,用一定长度的马尔科夫链构成句子
这里用字典来模拟马尔科夫链,字典为两层,每遇到一个新单词在第一层建立一个字典,然后查看它的后一个单词,如果后一个单词存在于它前一个单词的二级字典中,则不用为其在二级字典中建立字段,若二级字典中找不到这个单词,则为它建立二级字典并复制为0,结束后把当前单词的后一个单词对应的字段值加1。还有随机生成马尔科夫链的所用的两个函数:wordListSum()以及getRandomText()。阅读后不难理解。我自己写的时候在“randomIndex-=value”这一句上出了点问题,一开始我写的是”randomIndex-=1”,导致马尔科夫链取回的值是None,后来重新理了下逻辑知道了为什么错。举个例子说明,比如我单词word_c的二级字典为 {word_b : 5, word_d : 2},如果遍历,每次减1,那么当randomIndex>2时永远取不到值,因为每个单词出现了多次。
from urllib.request import urlopenimport randomdef wordListSum(wordList): sum = 0 for word,value in wordList.items(): sum+=value return sum passdef getRandomText(wordList): sum = wordListSum(wordList) randomIndex = random.randint(1,sum) for word,value in wordList.items(): **randomIndex-=value** if randomIndex<=0: return word passdef wordListDict(text): text = text.replace("\n"," ") text = text.replace("\"","") punctuation = [',', '.', ';',':'] for symble in punctuation: text = text.replace(symble," "+symble+" ") words = text.split(" ") words = [word for word in words if word!=""] wordDict = {} for i in range(len(words)): if words[i-1] not in wordDict: wordDict[words[i-1]] = {} if words[i] not in wordDict[words[i-1]]: wordDict[words[i-1]][words[i]] = 0 wordDict[words[i-1]][words[i]]+=1 return wordDict passhtml = urlopen("http://pythonscraping.com/files/inaugurationSpeech.txt")text = str(html.read(),"utf-8")wordDict = wordListDict(text)length = 200chain = ""currentWord = "I"for i in range(0,length): chain+=" "+currentWord currentWord = getRandomText(wordDict[currentWord]) passprint(chain)input()
今天先到这,最近每天都要做一点算法题,所以这部分稍时间紧一点~
阅读全文
0 0
- python爬虫学习第二十九天
- python第二十九天的学习记录
- python爬虫学习第九天
- python爬虫学习第十九天
- python爬虫学习第三十九天
- python爬虫学习第二十天
- python爬虫学习第二十五天
- python爬虫学习第二十八天
- 学习python的第二十九天-函数,变量,global语句
- 第二十九天
- python爬虫学习第二十七天(补昨天)
- python学习---第九天
- 安卓学习第二十九天:最后一些控件
- Java学习总结第二十九天Java泛型应用
- 实习日记->第二十九天
- 拓胜第二十九天
- 实习篇---第二十九天
- 毕业设计第二十九天
- 【SikuliX】基于图像识别的Web自动化测试
- hdu 6166 Senior Pan(最短路)
- 【JZOJ5272】【GDOI2018模拟8.14】神奇的重复序列
- Android中字符串String的拆分-split
- kali sudo apt install 无法定位软件包
- python爬虫学习第二十九天
- 十年研发经验工程师的嵌入式学习书籍大推荐
- wordpress必备插件Maintenance Mode开启网站维护模式
- HDU 1370 Biorhythms (中国剩余定理, 简单套用)
- 正则表达式
- zTree插件setting配置详解
- docker(2)搭建私有仓库
- JAVA 非递归快速排序
- cookie,session,实现服务器记忆用户登录状态功能