Here is one way to implement the most cited person code with fixes:
import re
def get_most_cited_person(text):
"""
Main function that takes in text and returns the most cited person
"""
persons = extract_persons(text)
citations = count_citations(persons, text)
most_cited = find_most_cited(citations)
return most_cited
def extract_persons(text):
"""
Extracts all person names from text
Returns a list of person names
"""
names = re.findall(r'[A-Z][a-z]+,\s[A-Z][^\.]', text)
persons = [name.split(", ")[0] for name in names]
return persons
def count_citations(persons, text):
"""
Counts number of citations for each person
Returns a dictionary with person as key and citation count as value
"""
citations = {person:0 for person in persons}
for person in persons:
citations[person] = text.count(person)
return citations
def find_most_cited(citations):
"""
Finds the person with most citations
Returns the person name
"""
return max(citations, key=citations.get)
if __name__ == "__main__":
text = """
Smith, A. did this great work. As Smith, A. showed in his paper, this method is very useful.
Lee, B. also made good contributions, though Smith was cited more.
"""
most_cited = get_most_cited_person(text)
print(most_cited)
The key fixes:
- Added regex to extract names
- Pass text to count_citations to enable counting
- Used max() and key to find most cited easily
- Added a test text for demonstration
Let me know if you have any other questions!