《用Python进行自然语言处理》代码笔记(三):第三章 加工原料文本

来源:互联网 发布:手机淘宝怎么换支付宝 编辑:程序博客网 时间:2024/04/25 08:36
#!/usr/bin/env python# -*- coding: utf-8 -*-# @Author  : Peidong# @Site    : # @File    : eg3.py# @Software: PyCharm"""加工原料文本"""# 从网络上访问文本import nltkfrom urllib.request import urlopenurl = "http://www.gutenberg.org/files/2554/2554.txt"raw = urlopen(url).read()print(type(raw))print(len(raw))print(raw[:75])# 分词tokens = nltk.word_tokenize(str(raw))print(type(tokens))print(len(tokens))print(tokens[:10])# 读取本地文件f = open('账号密码.txt')raw = f.read()print(raw)# 字符串操作示例a = [1, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 1]b = [' ' * 2 * (7-i) + 'very'*i for i in a]for line in b:    print(b)a = u'\u0061'print(a)# 查看文本中两个或;两个以上的元音序列,并得出其相对频率import nltkimport rewsj = sorted(set(nltk.corpus.treebank.words()))fd = nltk.FreqDist(vs for word in wsj for vs in re.findall(r'[aeiou]{2,}', word))print(fd.items())# 将字符串分割成不同的字母a = 'hello python'b = '?'.join(a)print(b)import networkx as nxfrom matplotlib import pyplotfrom nltk.corpus import wordnet as wndef traverse(graph, start, node):    graph.depth[node.name] = node.shortest_path_distance(start)    for child in node.hyponyms():        graph.add_edge(node.name, child.name)        traverse(graph, start, child)def hyponym_graph(start):    G = nx.Graph()    G.depth = {}    traverse(G, start, start)    return Gdef graph_draw(graph):    nx.draw_graphviz(graph, node_size=[16 * graph.degree(n) for n in graph], node_color=[graph.depth[n] for n in graph], with_labels=False)pyplot.show()dog = wn.synset('dog.n.01')graph = hyponym_graph(dog)graph_draw(graph)
0 0
原创粉丝点击