ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

学术前沿趋势分析Task03

2021-01-18 22:30:46  阅读:209  来源: 互联网

标签:... code 匹配 data figures 学术前沿 Task03 趋势 pages


一、任务说明

  • 任务主题:论文代码统计,统计所有论文出现代码的相关统计;
  • 任务内容:使用正则表达式统计代码连接、页数和图表数据;
  • 任务成果:学习正则表达式统计;

二、数据处理步骤

在原始arxiv数据集中作者经常会在论文的commentsabstract字段中给出具体的代码链接,所以我们需要从这些字段里面找出代码的链接。

  • 确定数据出现的位置;
  • 使用正则表达式完成匹配;
  • 完成相关的统计;

三、正则表达式

正则表达式(regular expression)描述了一种字符串匹配的模式(pattern),可以用来检查一个串是否含有某种子串、将匹配的子串替换或者从某个串中取出符合某个条件的子串等。

3.1 普通字符:大写和小写字母、所有数字、所有标点符号和一些其他符号

字符描述
[ABC]匹配 […] 中的所有字符,例如 [aeiou] 匹配字符串 “google runoob taobao” 中所有的 e o u a 字母。
[^ABC]匹配除了 […] 中字符的所有字符,例如 [^aeiou] 匹配字符串 “google runoob taobao” 中除了 e o u a 字母的所有字母。
[A-Z][A-Z] 表示一个区间,匹配所有大写字母,[a-z] 表示所有小写字母。
.匹配除换行符(\n、\r)之外的任何单个字符,相等于 [^\n\r]。
[\s\S]匹配所有。\s 是匹配所有空白符,包括换行,\S 非空白符,包括换行。
\w匹配字母、数字、下划线。等价于 [A-Za-z0-9_]

3.2 特殊字符:有特殊含义的字符

特别字符描述
( )标记一个子表达式的开始和结束位置。子表达式可以获取供以后使用。要匹配这些字符,请使用 ( 和 )。
*匹配前面的子表达式零次或多次。要匹配 * 字符,请使用 *。
+匹配前面的子表达式一次或多次。要匹配 + 字符,请使用 +。
.匹配除换行符 \n 之外的任何单字符。要匹配 . ,请使用 . 。
[标记一个中括号表达式的开始。要匹配 [,请使用 [。
?匹配前面的子表达式零次或一次,或指明一个非贪婪限定符。要匹配 ? 字符,请使用 ?。
\将下一个字符标记为或特殊字符、或原义字符、或向后引用、或八进制转义符。例如, ‘n’ 匹配字符 ‘n’。’\n’ 匹配换行符。序列 ‘’ 匹配 “”,而 ‘(’ 则匹配 “(”。
^匹配输入字符串的开始位置,除非在方括号表达式中使用,当该符号在方括号表达式中使用时,表示不接受该方括号表达式中的字符集合。要匹配 ^ 字符本身,请使用 ^。
{标记限定符表达式的开始。要匹配 {,请使用 {。
|指明两项之间的一个选择。要匹配 |,请使用 |。

3.3 限定符

字符描述
*匹配前面的子表达式零次或多次。例如,zo* 能匹配 “z” 以及 “zoo”。* 等价于{0,}。
+匹配前面的子表达式一次或多次。例如,‘zo+’ 能匹配 “zo” 以及 “zoo”,但不能匹配 “z”。+ 等价于 {1,}。
?匹配前面的子表达式零次或一次。例如,“do(es)?” 可以匹配 “do” 、 “does” 中的 “does” 、 “doxy” 中的 “do” 。? 等价于 {0,1}。
{n}n 是一个非负整数。匹配确定的 n 次。例如,‘o{2}’ 不能匹配 “Bob” 中的 ‘o’,但是能匹配 “food” 中的两个 o。
{n,}n 是一个非负整数。至少匹配n 次。例如,‘o{2,}’ 不能匹配 “Bob” 中的 ‘o’,但能匹配 “foooood” 中的所有 o。‘o{1,}’ 等价于 ‘o+’。‘o{0,}’ 则等价于 ‘o*’。
{n,m}m 和 n 均为非负整数,其中n <= m。最少匹配 n 次且最多匹配 m 次。例如,“o{1,3}” 将匹配 “fooooood” 中的前三个 o。‘o{0,1}’ 等价于 ‘o?’。请注意在逗号和两个数之间不能有空格。

四、具体代码实现以及讲解

首先我们来统计论文页数,也就是在comments字段中抽取pagesfigures和个数,首先完成字段读取。

# 导入所需的package
import seaborn as sns #用于画图
from bs4 import BeautifulSoup #用于爬取arxiv的数据
import re #用于正则表达式,匹配字符串的模式
import requests #用于网络连接,发送网络请求,使用域名获取对应信息
import json #读取数据,我们的数据为json格式的
import pandas as pd #数据处理,数据分析
import matplotlib.pyplot as plt #画图工具
data  = [] #初始化
#使用with语句优势:1.自动关闭文件句柄;2.自动显示(处理)文件读取数据异常
with open("arxiv-metadata-oai-snapshot.json", 'r') as f: 
    for idx, line in enumerate(f): 
        d = json.loads(line)
        d = {'abstract': d['abstract'], 'categories': d['categories'], 'comments': d['comments']}
        data.append(d)
        
data = pd.DataFrame(data) #将list变为dataframe格式,方便使用pandas进行分析
data.head()
abstractcategoriescomments
0A fully differential calculation in perturba...hep-ph37 pages, 15 figures; published version
1We describe a new algorithm, the $(k,\ell)$-...math.CO cs.CGTo appear in Graphs and Combinatorics
2The evolution of Earth-Moon system is descri...physics.gen-ph23 pages, 3 figures
3We show that a determinant of Stirling cycle...math.CO11 pages
4In this paper we show how to compute the $\L...math.CA math.FANone

对pages进行抽取:

# 使用正则表达式匹配,XX pages
data['pages'] = data['comments'].apply(lambda x: re.findall('[1-9][0-9]* pages', str(x)))
data.head()
abstractcategoriescommentspages
0A fully differential calculation in perturba...hep-ph37 pages, 15 figures; published version[37 pages]
1We describe a new algorithm, the $(k,\ell)$-...math.CO cs.CGTo appear in Graphs and Combinatorics[]
2The evolution of Earth-Moon system is descri...physics.gen-ph23 pages, 3 figures[23 pages]
3We show that a determinant of Stirling cycle...math.CO11 pages[11 pages]
4In this paper we show how to compute the $\L...math.CA math.FANone[]
# 筛选出有pages的论文
data = data[data['pages'].apply(len) > 0]
data.head()
abstractcategoriescommentspages
0A fully differential calculation in perturba...hep-ph37 pages, 15 figures; published version[37 pages]
2The evolution of Earth-Moon system is descri...physics.gen-ph23 pages, 3 figures[23 pages]
3We show that a determinant of Stirling cycle...math.CO11 pages[11 pages]
5We study the two-particle wave function of p...cond-mat.mes-hall6 pages, 4 figures, accepted by PRA[6 pages]
6A rather non-standard quantum representation...gr-qc16 pages, no figures. Typos corrected to match...[16 pages]
# 由于匹配得到的是一个list,如['19 pages'],需要进行转换
data['pages'] = data['pages'].apply(lambda x: float(x[0].replace(' pages', '')))
data.head()
abstractcategoriescommentspages
0A fully differential calculation in perturba...hep-ph37 pages, 15 figures; published version37.0
2The evolution of Earth-Moon system is descri...physics.gen-ph23 pages, 3 figures23.0
3We show that a determinant of Stirling cycle...math.CO11 pages11.0
5We study the two-particle wave function of p...cond-mat.mes-hall6 pages, 4 figures, accepted by PRA6.0
6A rather non-standard quantum representation...gr-qc16 pages, no figures. Typos corrected to match...16.0

对pages进行统计:

data['pages'].describe().astype(int)
count    1089180
mean          17
std           22
min            1
25%            8
50%           13
75%           22
max        11232
Name: pages, dtype: int32

统计结果如下:论文平均的页数为17页,75%的论文在22页以内,最长的论文有11232页。

接下来按照分类统计论文页数,选取了论文的第一个类别的主要类别:

# 选择主要类别
data['categories'] = data['categories'].apply(lambda x: x.split(' ')[0])
data['categories'] = data['categories'].apply(lambda x: x.split('.')[0])
data.head()
abstractcategoriescommentspages
0A fully differential calculation in perturba...hep-ph37 pages, 15 figures; published version37.0
2The evolution of Earth-Moon system is descri...physics23 pages, 3 figures23.0
3We show that a determinant of Stirling cycle...math11 pages11.0
5We study the two-particle wave function of p...cond-mat6 pages, 4 figures, accepted by PRA6.0
6A rather non-standard quantum representation...gr-qc16 pages, no figures. Typos corrected to match...16.0
# 每类论文的平均页数
plt.figure(figsize=(12, 6))
data.groupby(['categories'])['pages'].mean().plot(kind='bar')

在这里插入图片描述

接下来对论文图表个数进行抽取:

data['figures'] = data['comments'].apply(lambda x: re.findall('[1-9][0-9]* figures', str(x)))
data = data[data['figures'].apply(len) > 0]
data['figures'] = data['figures'].apply(lambda x: float(x[0].replace(' figures', '')))
data.head()
abstractcategoriescommentspagesfigures
0A fully differential calculation in perturba...hep-ph37 pages, 15 figures; published version37.015.0
2The evolution of Earth-Moon system is descri...physics23 pages, 3 figures23.03.0
5We study the two-particle wave function of p...cond-mat6 pages, 4 figures, accepted by PRA6.04.0
9Partial cubes are isometric subgraphs of hyp...math36 pages, 17 figures36.017.0
15In this work, we evaluate the lifetimes of t...hep-ph17 pages, 3 figures and 1 table17.03.0

最后我们对论文的代码链接进行提取,为了简化任务我们只抽取github链接:

# 筛选包含github的论文
data_with_code = data[(data.comments.str.contains('github')==True)|(data.abstract.str.contains('github')==True)]
data_with_code = data_with_code.copy() # 加个copy就没有警告了
data_with_code['text'] = data_with_code['abstract'].fillna('') + data_with_code['comments'].fillna('')
data_with_code.head()
abstractcategoriescommentspagesfigurestext
253172Solar tomography has progressed rapidly in r...astro-ph21 pages, 6 figures, 5 tables21.06.0Solar tomography has progressed rapidly in r...
254226We describe a hybrid Fourier/direct space co...astro-ph10 pages, 6 figures. Submitted to Astronomy an...10.06.0We describe a hybrid Fourier/direct space co...
296182REBOUND is a new multi-purpose N-body code w...astro-ph10 pages, 9 figures, accepted by A&A, source c...10.09.0REBOUND is a new multi-purpose N-body code w...
300298This article proposes a way to improve the p...physics10 pages, 7 figures. CODE: https://github.com/...10.07.0This article proposes a way to improve the p...
311500The interaction of distinct units in physica...physicsPreprint. 24 pages, 4 figures, 2 tables. Sourc...24.04.0The interaction of distinct units in physica...
# 使用正则表达式匹配论文
pattern = '[a-zA-z]+://github[^\s]*'
data_with_code = data_with_code.copy()
data_with_code['code_flag'] = data_with_code['text'].str.findall(pattern).apply(len)
data_with_code.head()
abstractcategoriescommentspagesfigurestextcode_flag
253172Solar tomography has progressed rapidly in r...astro-ph21 pages, 6 figures, 5 tables21.06.0Solar tomography has progressed rapidly in r...0
254226We describe a hybrid Fourier/direct space co...astro-ph10 pages, 6 figures. Submitted to Astronomy an...10.06.0We describe a hybrid Fourier/direct space co...1
296182REBOUND is a new multi-purpose N-body code w...astro-ph10 pages, 9 figures, accepted by A&A, source c...10.09.0REBOUND is a new multi-purpose N-body code w...1
300298This article proposes a way to improve the p...physics10 pages, 7 figures. CODE: https://github.com/...10.07.0This article proposes a way to improve the p...2
311500The interaction of distinct units in physica...physicsPreprint. 24 pages, 4 figures, 2 tables. Sourc...24.04.0The interaction of distinct units in physica...1

并对论文按照类别进行绘图:

data_with_code = data_with_code[data_with_code['code_flag'] == 1]
plt.figure(figsize=(12, 6))
data_with_code.groupby(['categories'])['code_flag'].count().plot(kind='bar')

在这里插入图片描述

参考文献

https://github.com/datawhalechina/team-learning-data-mining/blob/master/AcademicTrends/Task3%20%E8%AE%BA%E6%96%87%E4%BB%A3%E7%A0%81%E7%BB%9F%E8%AE%A1.md

标签:...,code,匹配,data,figures,学术前沿,Task03,趋势,pages
来源: https://blog.csdn.net/weixin_42871941/article/details/112796165

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有