# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# -*- coding: utf-8 -*-

import os
import re
import csv

emails_by_task = {}
email_to_name = {}
DO_NOT_INCLUDE = ["bigbench@googlegroups.com"]


def get_name_for_email(line, email):
    # Match: My Name (myemail@gmail.com)
    match = re.search(r"([\w\s\.,<>/-]+)\s+\(?\[?'?`?" + re.escape(email), line)

    if not match:
        # Match: [Kevin Gimpel](https://home.ttic.edu/~kgimpel/) (kgimpel@ttic.edu)
        match = re.search(r"\[([\w\s\.,<>/-]+)\]\([^)]+\)\s+\(?'?`?" + re.escape(email), line)

    if match:
        name = match.group(1).strip()
        assert not ":" in name
        for i in range(3):
            # Do this a few times to deal with Oxford comma
            name = re.sub(r"^(and |, )", r"", name)
        name = re.sub(r"^\s*-\s*", r"", name)
        name = re.sub(r"\s*[,-]\s*$", r"", name)
        name = re.sub(r"<sup>.*</sup>", r"", name)
        return name
    else:
        return None


try:
    os.chdir("bigbench/benchmark_tasks")
except FileNotFoundError:
    raise ValueError("This script should be run from the root bigbench directory")


for root, dirs, files in os.walk("."):
    for fname in files:
        if fname == "README.md":
            path = os.path.join(root, fname)

            components = path.split("/")
            assert components[0] == "."
            task = components[1]

            with open(path, "r") as f:
                in_authors_section = False
                
                for line in f:
                    authors_match = re.search(r"Authors?|Contributors?", line, re.IGNORECASE)
                    emails_match = re.findall(r'[\w.+-]+@[\w-]+\.[\w.-]+', line)
                    whitespace_match = re.match(r"\w*", line)

                    if authors_match:
                        in_authors_section = True
                    elif in_authors_section and (emails_match or whitespace_match):
                        # Sometimes we see "Authors:" and then emails span
                        # several lines.  Stay in authors section as long as we
                        # keep seeing emails.
                        pass
                    else:
                        in_authors_section = False

                    if in_authors_section and emails_match:
                        if not task in emails_by_task:
                            emails_by_task[task] = list()
                        for email in emails_match:
                            if email in DO_NOT_INCLUDE:
                                continue
                            if not email in emails_by_task[task]:
                                emails_by_task[task].append(email)
                            name = get_name_for_email(line, email)
                            if name:
                                email_to_name[email] = name
                            else:
                                print(f"WARNING: Name not found for email {email}\n"
                                      f"LINE: {line.strip()}\n"
                                      f"FILE: {path}\n")
                    elif emails_match:
                        print(f"WARNING: Email found in non-authors section, skipping.\n"
                              f"LINE: {line.strip()}\n"
                              f"FILE: {path}\n")
                

# Save the results
csv_filename = "task_authors.csv"
with open(csv_filename, "w") as csv_file:
    writer = csv.DictWriter(
        csv_file,
        delimiter="\t",
        fieldnames=["task", "email", "name"])
    writer.writeheader()

    for task in emails_by_task:
        for email in emails_by_task[task]:
            try:
                name = email_to_name[email]
            except KeyError:
                name = ""
            writer.writerow({"task": task, "email": email, "name": name})


print(f"Results saved to {os.path.join(os.getcwd(), csv_filename)}\n")

@@ -2264,11 +2264,13 @@
 h(r%22
-%5Cw*
+%5E%5Cs*$
 %22, l
@@ -2410,14 +2410,37 @@
 tion
- and (
+:%0A                        if 
 emai
@@ -2467,17 +2467,16 @@
 ce_match
-)
 :%0A      
@@ -2493,16 +2493,20 @@
         
+ 
+   
 # Someti
@@ -2548,16 +2548,20 @@
 ls span%0A
+    
         
@@ -2656,16 +2656,20 @@
         
+ 
+   
 # keep s
@@ -2706,16 +2706,20 @@
         
+    
 pass%0A   
@@ -2727,38 +2727,503 @@
                 
+ 
-else:%0A
+   elif task in emails_by_task and len(emails_by_task%5Btask%5D) %3E 0:%0A                            # Stop after we're done with one authors section.%0A                            # Make sure we found some emails, rather than just%0A                            # saw the word %22authors%22 somewhere.%0A                            break%0A                        else:%0A                            # We saw %22authors%22 but found no emails, so keep%0A                            # looking.%0A    
                 

improve author email parsing