Files
1article/parse.py
T
2024-12-14 01:24:38 -05:00

78 lines
2.1 KiB
Python

import csv
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']
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)
def extract_title_keywords(data):
title_keywords = []
re_words = []
for row in data:
raw_words = row[2].split(' ')
for word in raw_words:
re_word = re.sub(r'[^a-zA-Z\-]', '', word)
re_words.append((re_word, row[2]))
title_keywords = [(word.lower(), title) for (word, title) in re_words if word.lower() not in stop_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 dash_viz(data):
title_keywords = extract_title_keywords(data)
word_output = {}
for line in title_keywords:
if line[0] not in word_output:
word_output[line[0]] = [line[1]]
else:
word_output[line[0]].append(line[1])
pprint(word_output)
if __name__ == '__main__':
parse_csv(file_path)
data = data[1:]
extract_journal_titles(data)
print_title_keywords(data)
dash_viz(data)
histogram(data)