python 自然语言分析第六周作业
2017-04-23 15:16
447 查看
"""写一个程序按长度对词排序。定义一个辅助函数 cmp_len,它在词长上使用 cmp 比较函数""" def cmp_len(a,b): if len(a)>=len(b): s=a if len(a)<len(b): s=b return s words=['I','love','NPL'] def cmp(words): words.sort(key=len) return words cmp(words) """一个函数处理一个词链表(含重复项),返回一个按照频率递减排序的词链表(没 有重复项)。例如:如果输入链表中包含词table的 10个实例, chair的 9个实例, 那 么在输出链表中table会出现在chair前面""" word=["1","2","3","1","2","3","2","3","3"] def get_feq(word): dic={} for i in range(len(word)): dic[word[i]]=word.count(word[i]) return sorted(dic.keys(),reverse=True) get_feq(word) """◑从 Python的标准库的operator模块导入itemgetter()函数( 即from operator i mport itemgetter)。 创建一个包含几个词的链表words。现在尝试调用: sorted(w ords, key=itemgetter(1))和sorted(words, key=itemgetter(-1))。解释item getter()正在做什么""" from operator import itemgetter words=["I","LOVE","YOU"] sorted(words, key=itemgetter(1)) sorted(words, key=itemgetter(-1)) #获取对象的第几个值 """写一个程序实现发现一种n×n的四方联词的蛮力算法:纵横字谜,它的第n行的词 与第 n列的词相同。 讨论见http://itre.cis.upenn.edu/ myl/languagelog/archives/002679.ht ml""" s="The moment you think about giving up,think of the reason why you held on so long" word=["The","moment","you","think","about","giving","up"] def zonghengzimi(word): #生成以word为基础的,10X10的10阶乘 import pandas as pd import numpy as np import random raw=[] for i in range(100): raw.append(chr(random.randint(97, 122))) data=pd.DataFrame(np.array(raw).reshape(10,10)) print (len(data)) print (data ) #生成了10X10的阶乘随机字母,需要把七个词语放进去,并把一部分改成空白元素进行填写 for i in range(len(word)): a=random.randint(1,10) b=random.randint(1,10) for j in range(len(word[i])): if a>b and b+len(word[i])<10: data.ix[a,b+j]=word[i][j] if a>b and b+len(word[i])>10: b=0 data.ix[a,b+j]=word[i][j] return data #这样就简单生成了一个纵横字谜,尝试在这些子母中找出相应的word当中的单词 zonghengzimi(word)
相关文章推荐
- Python自然语言分析(第四期):第2课书面作业
- Python自然语言分析(第四期):第1课书面作业
- 【备忘】最新Python自然语言分析视频课程下载
- [作业] 马哥2016全新Linux+Python高端运维班第六周作业
- R语言安装NLP自然语言分析包
- [舆情方向]自然语言情感倾向分析技术原型[第一期]
- python等缩进语言的词法分析实现
- 分析Python 3.1核心语言的变化
- Python自然语言工具包(NLTK)入门
- 自然语言分析之命名实体识别_Stanford Named Entity Recognizer (NER)简单实例
- Python中一些自然语言工具的使用的入门教程
- Python语言实践分析 -- With语句
- 动态语言Ruby、Python源码对比分析日记(一)
- Python语言实践分析 -- 闭包
- Linux内核分析作业第六周
- Python语言实践分析 -- 包导入
- 用Python实现一个细粒度hadoop作业监控分析工具
- 自然语言分析、文本分析、全文索引,搜索引擎相关资源汇总
- 自然语言分析的一些感想
- 超强教程:教你用Python语言分析引力波数据