Files
1article/parse.py
T
2025-07-20 01:55:49 -04:00

119 lines
2.4 KiB
Python

import csv
import json
import re
import matplotlib.pyplot as plt
import numpy as np
from collections import Counter
from pprint import pprint
stop_words = [
"of",
"and",
"the",
"we",
"in",
"to",
"what",
"a",
"on",
"an",
"do",
"for",
"with",
"is",
"it",
"that",
"this",
"as",
"by",
"are",
"using",
"from",
"how",
"has",
"be",
"or",
"can",
"our",
"at",
"why",
"when",
"introduction",
"your",
"through",
"et",
"al.",
"&",
"",
]
file_path = "data.csv"
data = []
def parse_csv(file_path):
with open(file_path, "r") as file:
reader = csv.reader(file)
for row in reader:
data.append(row)
def extract_journal_titles(data):
journal_titles = []
for row in data:
if "(edited volume)" not in row[3]:
journal_titles.append(row[3])
count_journal_titles = Counter(journal_titles).most_common(20)
pprint(count_journal_titles)
print("Total journals: " + str(len(set(journal_titles))))
def extract_title_keywords(data):
title_keywords = []
re_words = []
for row in data:
raw_words = row[2].split(" ")
row_words = []
for word in raw_words:
re_word = re.sub(r"[^a-zA-Z\-]", "", word.lower())
if re_word not in stop_words:
re_words.append(re_word)
row_words.append(re_word)
title_keywords = [(word, row_words) for word in re_words]
return title_keywords
def print_title_keywords(data):
title_keywords = extract_title_keywords(data)
title_keywords = [word[0] for word in title_keywords]
count_title_words = Counter(title_keywords).most_common(20)
pprint(count_title_words)
def histogram(data):
dates = []
for row in data:
dates.append(int(row[4]))
years = range(min(dates), max(dates) + 2)
plt.hist(dates, bins=years)
plt.show()
def names(data):
names = []
for row in data:
name_parts = row[1].split(" ")
for name in name_parts:
if name not in stop_words:
names.append(name)
count_names = Counter(names).most_common(20)
pprint(count_names)
if __name__ == "__main__":
parse_csv(file_path)
data = data[1:]
extract_journal_titles(data)
print_title_keywords(data)
names(data)
histogram(data)