Skip to content

Commit 34869ce

Browse files
authored
feat: google scholar toolkit support author assignment (#1124)
1 parent 36b3713 commit 34869ce

File tree

1 file changed

+36
-7
lines changed

1 file changed

+36
-7
lines changed

camel/toolkits/google_scholar_toolkit.py

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# limitations under the License.
1313
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
1414
import re
15-
from typing import List, Optional
15+
from typing import Any, Dict, List, Optional
1616

1717
from camel.toolkits import FunctionTool
1818
from camel.toolkits.base import BaseToolkit
@@ -28,6 +28,8 @@ class GoogleScholarToolkit(BaseToolkit):
2828
is_author_name (bool): Flag to indicate if the identifier is a name.
2929
(default: :obj:`False`)
3030
scholarly (module): The scholarly module for querying Google Scholar.
31+
author (Optional[Dict[str, Any]]): Cached author details, allowing
32+
manual assignment if desired.
3133
"""
3234

3335
def __init__(
@@ -46,6 +48,35 @@ def __init__(
4648
self.scholarly = scholarly
4749
self.author_identifier = author_identifier
4850
self.is_author_name = is_author_name
51+
self._author: Optional[Dict[str, Any]] = None
52+
53+
@property
54+
def author(self) -> Dict[str, Any]:
55+
r"""Getter for the author attribute, fetching details if not cached.
56+
57+
Returns:
58+
Dict[str, Any]: A dictionary containing author details. If no data
59+
is available, returns an empty dictionary.
60+
"""
61+
if self._author is None:
62+
self.get_author_detailed_info()
63+
return self._author or {}
64+
65+
@author.setter
66+
def author(self, value: Optional[Dict[str, Any]]) -> None:
67+
r"""Sets or overrides the cached author information.
68+
69+
Args:
70+
value (Optional[Dict[str, Any]]): A dictionary containing author
71+
details to cache or `None` to clear the cached data.
72+
73+
Raises:
74+
ValueError: If `value` is not a dictionary or `None`.
75+
"""
76+
if value is None or isinstance(value, dict):
77+
self._author = value
78+
else:
79+
raise ValueError("Author must be a dictionary or None.")
4980

5081
def _extract_author_id(self) -> Optional[str]:
5182
r"""Extracts the author ID from a Google Scholar URL if provided.
@@ -73,8 +104,8 @@ def get_author_detailed_info(
73104
author_id = self._extract_author_id()
74105
first_author_result = self.scholarly.search_author_id(id=author_id)
75106

76-
author = self.scholarly.fill(first_author_result)
77-
return author
107+
self._author = self.scholarly.fill(first_author_result)
108+
return self._author # type: ignore[return-value]
78109

79110
def get_author_publications(
80111
self,
@@ -84,9 +115,8 @@ def get_author_publications(
84115
Returns:
85116
List[str]: A list of publication titles authored by the author.
86117
"""
87-
author = self.get_author_detailed_info()
88118
publication_titles = [
89-
pub['bib']['title'] for pub in author['publications']
119+
pub['bib']['title'] for pub in self.author['publications']
90120
]
91121
return publication_titles
92122

@@ -105,8 +135,7 @@ def get_publication_by_title(
105135
Optional[dict]: A dictionary containing detailed information about
106136
the publication if found; otherwise, `None`.
107137
"""
108-
author = self.get_author_detailed_info()
109-
publications = author['publications']
138+
publications = self.author['publications']
110139
for publication in publications:
111140
if publication['bib']['title'] == publication_title:
112141
return self.scholarly.fill(publication)

0 commit comments

Comments
 (0)