<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Solution of 5.9.1, Bee Checklist
First of all, we import the two modules we'll need to read the csv file, and to use regular expressions
Step1:  Then, we read the file, and store the columns Scientific Name and Taxon Author in two lists
Step2:  How many species?
Step3:  Pick one of the authors element to use for testing. Choose one that is quite complicated, such as the 38th element
Step4:  Now we need to build a regular expression. After some twiddling, you should end up with something like this, which captures the authors in one group, and the year in another group
Step5:  Test the expression
Step6:  Now we write a function that uses the regular expression to extract an author list (useful when there are multiple authors), and the year
Step7:  Let's see the output of this function
Step8:  Finally, let's build two dictionaries
Step9:  For example, these are all the authors
Step10:  What is the name of the author with most entries in the database?
We use the following strategy
Step11:  An the winner is
Step12:  Which year of publication is most represented in the database?
We use the same strategy to find that the golden year of bee publication is
<ASSISTANT_TASK:>
Python Code:
import csv
import re
Explanation: Solution of 5.9.1, Bee Checklist
First of all, we import the two modules we'll need to read the csv file, and to use regular expressions:
End of explanation
with open('../data/bee_list.txt') as f:
    csvr = csv.DictReader(f, delimiter = '\t')
    species = []
    authors = []
    for r in csvr:
        species.append(r['Scientific Name'])
        authors.append(r['Taxon Author'])
Explanation: Then, we read the file, and store the columns Scientific Name and Taxon Author in two lists:
End of explanation
len(species)
len(authors)
Explanation: How many species?
End of explanation
au = authors[37]
au
Explanation: Pick one of the authors element to use for testing. Choose one that is quite complicated, such as the 38th element:
End of explanation
my_reg = re.compile(r'\(?([\w\s,\.\-\&]*),\s(\d{4})\)?')
# Translation
# \(? -> open parenthesis (or not)
# ([\w\s,\.\-\&]+) -> the first group is the list of authors
#                     which can contain \w (word character)
#                     \s (space) \. (dot) \- (dash) \& (ampersand)
# ,\s -> followed by comma and space
# (\d{4}) -> the second group is the year, 4 digits
#  \)? -> potentially, close parenthesis
Explanation: Now we need to build a regular expression. After some twiddling, you should end up with something like this, which captures the authors in one group, and the year in another group:
End of explanation
re.findall(my_reg,au)
Explanation: Test the expression
End of explanation
def extract_list_au_year(au):
    tmp = re.match(my_reg, au)
    authorlist = tmp.group(1)
    year = tmp.group(2)
    # split authors into a list using re.split
    authorlist = re.split(', | \& ', authorlist)
    # Translation: either separate using ', ' or ' & '
    return [authorlist, year]
Explanation: Now we write a function that uses the regular expression to extract an author list (useful when there are multiple authors), and the year
End of explanation
extract_list_au_year(au)
Explanation: Let's see the output of this function:
End of explanation
dict_years = {}
dict_authors = {}
for au in authors:
    tmp = extract_list_au_year(au)
    for aunum in tmp[0]:
        if aunum in dict_authors.keys():
            dict_authors[aunum] = dict_authors[aunum] + 1 
        else:
            dict_authors[aunum] =  1 
    if tmp[1] in dict_years.keys():
        dict_years[tmp[1]] = dict_years[tmp[1]] + 1 
    else:
        dict_years[tmp[1]] =  1 
Explanation: Finally, let's build two dictionaries:
- one tracking the number of times each year is mentioned in the database;
- one traking the number of times each author is mentioned
End of explanation
dict_authors
Explanation: For example, these are all the authors:
End of explanation
max_value_author = max(dict_authors.values())
max_value_author
which_index = list(dict_authors.values()).index(max_value_author)
which_index
Explanation: What is the name of the author with most entries in the database?
We use the following strategy:
- we find the maximum value in the dictionary
- we use the function index to find to which entry is it associated
- we find the corresponding author
End of explanation
list(dict_authors.keys())[which_index]
Explanation: An the winner is:
End of explanation
max_value_year = max(dict_years.values())
which_index = list(dict_years.values()).index(max_value_year)
list(dict_years.keys())[which_index]
Explanation: Which year of publication is most represented in the database?
We use the same strategy to find that the golden year of bee publication is:
End of explanation
<END_TASK>