Created
June 27, 2022 16:10
-
-
Save rafaelpezzuto/9711a88a401ef1e07f1c7d2002b20d40 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import csv\n", | |
"import os\n", | |
"\n", | |
"from scielo_scholarly_data import standardizer" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"DIR_DATA_RESULTS = 'data/results/'\n", | |
"\n", | |
"FILE_ISSN_TO_ALL = 'data/bases/base_issnl2all_v0.6.csv'\n", | |
"\n", | |
"FILE_JOURNALS = 'data/bases/journals.csv'\n", | |
"\n", | |
"missing_journals = {\n", | |
" '0379-3962': {'issns': set(['0379-3962']), 'title': 'TECNOLOGIA EN MARCHA', 'collection': set(['cri'])},\n", | |
" '2683-2623': {'issns': set(['2683-2623']), 'title': 'VERTICE UNIVERSITARIO', 'collection': set(['mex'])}\n", | |
"}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def load_issn_to_attrs(path):\n", | |
" '''\n", | |
" Gera um dicionário que mapeia código ISSN a diversos atributos\n", | |
"\n", | |
" Params\n", | |
" ------\n", | |
" path: str\n", | |
" Caminho do arquivo que contém códigos ISSN e diversos atributos\n", | |
"\n", | |
" Returns\n", | |
" -------\n", | |
" dict\n", | |
" Dicionário que mapeia código ISSN a título e outros atributos\n", | |
" '''\n", | |
" issn_to_attrs = {}\n", | |
"\n", | |
" with open(path) as fin:\n", | |
" csv_reader = csv.DictReader(fin, delimiter='|')\n", | |
"\n", | |
" for item in csv_reader:\n", | |
" issnl = item['ISSNL']\n", | |
" issns = sorted(set([issnl] + item['ISSNs'].split('#')))\n", | |
"\n", | |
" tmp_main_title = [ti for ti in item['MAIN_TITLE'].split('#') if len(ti) > 0]\n", | |
" tmp_abbrev_title = [ti for ti in item['MAIN_ABBREV_TITLE'].split('#') if len(ti) > 0]\n", | |
" tmp_other_titles = [ti for ti in item['OTHER_TITLEs'].split('#') if len(ti) > 0]\n", | |
"\n", | |
" if len(tmp_main_title) > 0:\n", | |
" title = tmp_main_title[0]\n", | |
" elif len(tmp_abbrev_title) > 0:\n", | |
" title = tmp_abbrev_title[0]\n", | |
" elif len(tmp_other_titles) > 0:\n", | |
" title = tmp_other_titles[0]\n", | |
" else:\n", | |
" print(f'{issnl} não possui título válido')\n", | |
" continue\n", | |
"\n", | |
" for i in issns:\n", | |
" if i not in issn_to_attrs:\n", | |
" if i in issn_to_attrs:\n", | |
" print(f'{i} já está no dicionário issn_to_attrs')\n", | |
" continue\n", | |
"\n", | |
" issn_to_attrs[i] = {\n", | |
" 'main_issn': issnl,\n", | |
" 'main_title': title,\n", | |
" 'issns': issns,\n", | |
" }\n", | |
"\n", | |
" return issn_to_attrs\n", | |
"\n", | |
"\n", | |
"def load_scielo_journals(path):\n", | |
" '''\n", | |
" Gera dicionário de periódicos SciELO\n", | |
"\n", | |
" Params\n", | |
" ------\n", | |
" path: str\n", | |
" Caminho do arquivo de periódicos SciELO\n", | |
"\n", | |
" Returns\n", | |
" -------\n", | |
" dict\n", | |
" Dicionário contendo os periódicos SciELO no formato:\n", | |
" {\n", | |
" '0000-0000': {\n", | |
" 'title': 'Nome do periódico',\n", | |
" 'issns': {\n", | |
" '0000-0000',\n", | |
" '0001-0001',\n", | |
" },\n", | |
" 'collection': {\n", | |
" 'scl',\n", | |
" 'ssp',\n", | |
" }\n", | |
" }\n", | |
" }\n", | |
" '''\n", | |
" scielo_journals = {}\n", | |
" \n", | |
" with open(path) as fin:\n", | |
" creader = csv.DictReader(fin, delimiter=',', fieldnames=['issn', 'title', 'issns'])\n", | |
" for row in creader:\n", | |
" j_issn = standardizer.journal_issn(row['issn'])\n", | |
" j_title = standardizer.journal_title_for_deduplication(row['title'].lower()).upper()\n", | |
" \n", | |
" if j_issn not in scielo_journals:\n", | |
" scielo_journals[j_issn] = {\n", | |
" 'title': j_title,\n", | |
" 'issns': set([j_issn]),\n", | |
" }\n", | |
"\n", | |
" return scielo_journals\n", | |
"\n", | |
"\n", | |
"def _enrich_scielo_journals(scielo_journals, issn_to_attrs):\n", | |
" '''\n", | |
" Adiciona códigos ISSN em periódicos SciELO usando dados da base de títulos\n", | |
" \n", | |
" Params\n", | |
" ------\n", | |
" scielo_journals: dict\n", | |
" issn_to_attrs: dict\n", | |
" '''\n", | |
" for i in scielo_journals:\n", | |
" for ei in issn_to_attrs.get(i, {}).get('issns', []):\n", | |
" scielo_journals[i]['issns'].add(ei)\n", | |
"\n", | |
"\n", | |
"def _extract_scielo_issns(scielo_journals):\n", | |
" '''\n", | |
" Extrai set de códigos ISSN pertencentes a periódicos SciELO\n", | |
"\n", | |
" Params\n", | |
" ------\n", | |
" scielo_journals: dict\n", | |
"\n", | |
" Returns\n", | |
" -------\n", | |
" scielo_issns: Set\n", | |
" '''\n", | |
" scielo_issns = set()\n", | |
" \n", | |
" for j_attrs in scielo_journals.values():\n", | |
" for i in j_attrs['issns']:\n", | |
" scielo_issns.add(i)\n", | |
"\n", | |
" return scielo_issns" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"scielo_journals = load_scielo_journals(FILE_JOURNALS)\n", | |
"issn_to_attrs = load_issn_to_attrs(FILE_ISSN_TO_ALL)\n", | |
"for mj in missing_journals:\n", | |
" if mj not in issn_to_attrs:\n", | |
" issn_to_attrs[mj] = {'main_issn': mj, 'issns': missing_journals[mj]['issns'], 'main_title': missing_journals[mj]['title']}\n", | |
"_enrich_scielo_journals(scielo_journals, issn_to_attrs)\n", | |
"scielo_issns = _extract_scielo_issns(scielo_journals)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"issns = os.listdir(DIR_DATA_RESULTS)\n", | |
"\n", | |
"with open(os.path.join(DIR_DATA_RESULTS, 'citations_received_cited_forms_all.csv'), 'w') as fout:\n", | |
" fout.write('ISSN chave,Ano chave,Título citado,Título padronizado,Frequência\\n')\n", | |
" \n", | |
" for issn in issns:\n", | |
" if standardizer.journal_issn(issn) and os.path.isdir(os.path.join(DIR_DATA_RESULTS, issn)):\n", | |
" for f in [f for f in os.listdir(os.path.join(DIR_DATA_RESULTS, issn)) if '_cited_forms' in f]:\n", | |
" f_path = os.path.join(DIR_DATA_RESULTS, issn, f)\n", | |
" f_year = int(f_path.split('.')[-2].split('_')[-1])\n", | |
"\n", | |
" with open(f_path) as fin:\n", | |
" for row in csv.DictReader(fin):\n", | |
" fout.write(','.join([\n", | |
" issn,\n", | |
" str(f_year),\n", | |
" row['Título citado'],\n", | |
" row['Título padronizado'],\n", | |
" row['Frequência']\n", | |
" ]) + '\\n')\n", | |
"\n", | |
"with open(os.path.join(DIR_DATA_RESULTS, 'citations_received_standardized_all.csv'), 'w') as fout:\n", | |
" fout.write('ISSN chave,Ano chave,Código da coleção citante,ISSN,Código do documento citante,Código da referência citada,Ano da referência citada,Título citado,Volume citado,Código do método de correção\\n')\n", | |
"\n", | |
" for issn in issns:\n", | |
" if standardizer.journal_issn(issn) and os.path.isdir(os.path.join(DIR_DATA_RESULTS, issn)):\n", | |
" for f in [f for f in os.listdir(os.path.join(DIR_DATA_RESULTS, issn)) if '_standardized' in f]:\n", | |
" f_path = os.path.join(DIR_DATA_RESULTS, issn, f)\n", | |
" f_year = int(f_path.split('.')[-2].split('_')[-1])\n", | |
"\n", | |
" with open(f_path) as fin:\n", | |
" for row in csv.DictReader(fin):\n", | |
" fout.write(','.join([\n", | |
" issn,\n", | |
" str(f_year),\n", | |
" row['Código da coleção citante'],\n", | |
" row['ISSN'],\n", | |
" row['Código do documento citante'],\n", | |
" row['Código da referência citada'],\n", | |
" row['Ano da referência citada'],\n", | |
" row['Título citado'],\n", | |
" row['Volume citado'],\n", | |
" row['Código do método de correção'],\n", | |
" ]) + '\\n')\n", | |
"\n", | |
"with open(os.path.join(DIR_DATA_RESULTS, 'citations_received_count_all.csv'), 'w') as fout:\n", | |
" fout.write('ISSN chave,Ano chave,ISSN,Título,Citações recebidas\\n')\n", | |
"\n", | |
" for issn in issns:\n", | |
" if standardizer.journal_issn(issn) and os.path.isdir(os.path.join(DIR_DATA_RESULTS, issn)):\n", | |
" for f in [f for f in os.listdir(os.path.join(DIR_DATA_RESULTS, issn)) if '_received_count' in f]:\n", | |
" f_path = os.path.join(DIR_DATA_RESULTS, issn, f)\n", | |
" f_year = int(f_path.split('.')[-2].split('_')[-1])\n", | |
"\n", | |
" with open(f_path) as fin:\n", | |
" for row in csv.DictReader(fin):\n", | |
" fout.write(','.join([\n", | |
" issn,\n", | |
" str(f_year),\n", | |
" row['ISSN'],\n", | |
" row['Título'],\n", | |
" row['Citações recebidas'],\n", | |
" ]) + '\\n')\n", | |
"\n", | |
"with open(os.path.join(DIR_DATA_RESULTS, 'citations_granted_count_all.csv'), 'w') as fout:\n", | |
" fout.write('ISSN chave,Ano chave,ISSN,Título,Citações concedidas\\n')\n", | |
"\n", | |
" for issn in issns:\n", | |
" if standardizer.journal_issn(issn) and os.path.isdir(os.path.join(DIR_DATA_RESULTS, issn)):\n", | |
" for f in [f for f in os.listdir(os.path.join(DIR_DATA_RESULTS, issn)) if '_granted_count' in f]:\n", | |
" f_path = os.path.join(DIR_DATA_RESULTS, issn, f)\n", | |
" f_year = int(f_path.split('.')[-2].split('_')[-1])\n", | |
"\n", | |
" with open(f_path) as fin:\n", | |
" for row in csv.DictReader(fin):\n", | |
" fout.write(','.join([\n", | |
" issn,\n", | |
" str(f_year),\n", | |
" row['ISSN'],\n", | |
" row['Título'],\n", | |
" row['Citações concedidas'],\n", | |
" ]) + '\\n')\n", | |
"\n", | |
"with open(os.path.join(DIR_DATA_RESULTS, 'citations_received_citing_pids_all.csv'), 'w') as fout:\n", | |
" fout.write('ISSN chave,Ano chave,Código de documento citante,Coleção de documento citante,Ano de documento citado\\n')\n", | |
"\n", | |
" for issn in issns:\n", | |
" if standardizer.journal_issn(issn) and os.path.isdir(os.path.join(DIR_DATA_RESULTS, issn)):\n", | |
" for f in [f for f in os.listdir(os.path.join(DIR_DATA_RESULTS, issn)) if '_received_citing_pids' in f]:\n", | |
" f_path = os.path.join(DIR_DATA_RESULTS, issn, f)\n", | |
" f_year = int(f_path.split('.')[-2].split('_')[-1])\n", | |
"\n", | |
" with open(f_path) as fin:\n", | |
" for row in csv.DictReader(fin):\n", | |
" fout.write(','.join([\n", | |
" issn,\n", | |
" str(f_year),\n", | |
" row['Código de documento citante'],\n", | |
" row['Coleção de documento citante'],\n", | |
" row['Ano de documento citado'],\n", | |
" ]) + '\\n')" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3.9.7 ('scielo-bibliometrics')", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.9.7" | |
}, | |
"orig_nbformat": 4, | |
"vscode": { | |
"interpreter": { | |
"hash": "420b81cf9fd95b777b31ce10675349dd3490f0a93d23fb97ad6befdeb4be28ed" | |
} | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment