准备步骤

安装python3,下载CSDB,任意选择位置git clone ...

环境配置
  1. pycharm中,command+,选择interpretercreate virtualenv,选择python版本,location自己定(之后安装的库都是在这个文件夹里),和程序文件所在位置无关。
  2. 在终端中,定位到virtualenv所在位置,source bin/activate,激活,可看到(lenv)前缀。可用pip命令安装各种库。
  3. 回到pycharm中,仍然选择刚才创建的virtualenv,可看到多了刚才安装的库。
db文件

sqlite3 filename打开,有.help.table等命令。

word_level_analyzer.py

文件前加:# coding:utf-8
在函数cut_qts_to_words中,有如下变量:

 char_counter = Counter()  # 字频统计
 author_counter = Counter()  # 每个作者的写诗篇数
 vocab = set()  # 词汇库
 word_counter = Counter()  # 词频统计
 genre_counter = defaultdict(Counter)  # 针对每个词性的Counter
  • 源文件格式为num+author+poet,用list区分即可,统计作者、汉子个数;
  • thulac库中的lex_analyzer.cut分割词语和对应的词性,记录下来,边统计边输出进展。
    分词后输出文档如下(只选取了一首诗,诗的末尾不能有转折,否则程序会出错):qts_words_list
  • gensim中的Word2Vec得到词向量;
  • 打印结果,按序输出语句为:print_counter(author_counter.most_common(10))counter格式能自动选取频率最高的打印出来。
construct_poets_network.py
  • 调用utils.py函数读取作者和诗歌内容。由于不用进行词法分析,速度快。
  • 用CSDB来确定作者(def get_alter_names)。打开.db文件:
conn = sqlite3.connect(db_file)
cursor = conn.cursor()

识别作者有两个问题:

  1. 重名
    解决方法:首先用诗人名字在.db的BLOG_MAIN中查找:
cursor.execute('SELECT c_personid, c_birthyear, c_deathyear FROM BIOG_MAIN WHERE c_name_chn LIKE?', (author_pattern,))
# 记录该人的生卒年和ID信息
person_info_list = cursor.fetchall()

如果候选人列表中的某人生卒年和唐朝重合,则确定此人就是要找的人。
2. 别称
由于有的作者别称也是日常用词,因此删除这些别称。

  • def get_refer_relations计算关系
 for 从CSDB中统计出的诗人
     for 每一首从qts中统计出诗歌
          if该诗歌作者不在CSDB中跳过
          查找该诗人的名字(包括所有别称)

保存在reference_relations.pkl中。

visualize_poets_network.py

三个函数:

  • 得到排名靠前的关系def get_concerned_relations_by_range
  • 通过已有的不同时期唐诗人名单,计算之间的关系get_concerned_relations_by_authors
  • 画图generate_html_page
//得到link数据 
filtered_authors.add(refered_by)
 filtered_authors.add(refered)
 count = math.sqrt(count)
 line_width = min_link_width + width_slope * (count - min_refer_count)
 links_text += links_item_format % (refered_by, refered, line_width)
//处理节点数据
//和已经写好的`html`头尾合并

展示图如下:

盛唐诗人关系图

posted on 2017-04-23 11:25  yingtaomj  阅读(224)  评论(0编辑  收藏  举报