update
This commit is contained in:
@@ -1037,40 +1037,21 @@ function chunktext(text::T, headers) where {T<:AbstractString}
|
||||
return result
|
||||
end
|
||||
|
||||
"""
|
||||
Search wikipedia.
|
||||
|
||||
Args:
|
||||
query (string): The query to search for
|
||||
|
||||
Returns:
|
||||
string: The search result text from wikipedia
|
||||
```jldoctest
|
||||
julia> using HTTP, JSON3
|
||||
julia> result = wikisearch("AMD")
|
||||
"Advanced Micro Devices, Inc., commonly abbreviated as AMD, is an ..."
|
||||
```
|
||||
"""
|
||||
function wikisearch(phrase::T) where {T<:AbstractString}
|
||||
url = "https://en.wikipedia.org/w/api.php?action=query&format=json&prop=extracts&titles=$(replace(phrase, " " => "%20"))&exintro=1&explaintext=1"
|
||||
response = HTTP.get(url)
|
||||
json_data = JSON3.read(String(response.body))
|
||||
page_id = first(keys(json_data["query"]["pages"]))
|
||||
if page_id == "-1"
|
||||
return "Sorry, I couldn't find any Wikipedia page for the given phrase."
|
||||
end
|
||||
|
||||
result = nothing
|
||||
try
|
||||
result = json_data["query"]["pages"][page_id]["extract"]
|
||||
catch
|
||||
result = "No info available."
|
||||
end
|
||||
if result == ""
|
||||
result = "No info available."
|
||||
end
|
||||
return result
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user