0

I'm working on a script that will scrape any answers to my questions using DuckDuckGo! I tried doing this using DuckDuckGo's API, and it kind of works as well but the result gives a ton of info.. Is there any method to limit it's sentences? Like to 3 sentences or 4 senteces? this is my script till now:

word = input("Enter the Word: ")
query = f"what is {word}?"

r = requests.get("https://api.duckduckgo.com",
    params = {
        "q": query,
        "format": "json"
    })

data = r.json()

print(data["Abstract"])

What Output I get:

Enter the Word: Artificial Intelligence
Artificial intelligence is intelligence demonstrated by machines, as opposed to natural intelligence displayed by animals including humans. Leading AI textbooks define the field as the study of "intelligent agents": any system that perceives its environment and takes actions that maximize its chance of achieving its goals. Some popular accounts use the term "artificial intelligence" to describe machines that mimic "cognitive" functions that humans associate with the human mind, such as "learning" and "problem solving", however, this definition is rejected by major AI researchers. AI applications include advanced web search engines, recommendation systems, understanding human speech, self-driving cars, automated decision-making and competing at the highest level in strategic game systems. As machines become increasingly capable, tasks considered to require "intelligence" are often removed from the definition of AI, a phenomenon known as the AI effect.

I want it to Just print a starting few sentences(i.e: Only the first 3 or 4 sentences.)

1 Answers1

0

For example, if you want to limit RelatedTopics count, you can use something like this:

def limit_count_topicks(n):
    for i in range(len(data['RelatedTopics'])):
        if i == n:
            break
        print(data['RelatedTopics'][i])

limit_count_topicks(2)

Output only 2 related topicks:

{'FirstURL': 'https://duckduckgo.com/Happiness', 'Icon': {'Height': '', 'URL': '/i/a62c4a70.png', 'Width': ''}, 'Result': '<a href="https://duckduckgo.com/Happiness">Happiness</a>The term happiness is used in the context of mental or emotional states, including positive or...', 'Text': 'Happiness The term happiness is used in the context of mental or emotional states, including positive or...'}
{'FirstURL': 'https://duckduckgo.com/Happy!_(TV_series)', 'Icon': {'Height': '', 'URL': '', 'Width': ''}, 'Result': '<a href="https://duckduckgo.com/Happy!_(TV_series)">Happy! (TV series)</a>Happy! is an American live-action/adult animated black comedy/action-drama television series...', 'Text': 'Happy! (TV series) Happy! is an American live-action/adult animated black comedy/action-drama television series...'}
Andrew
  • 176
  • 2
  • 8
  • Not to limit the Topic count, but the sentences, of a paragraph. For Example, take a look at this paragraph "Paragraphs are the building blocks of papers. Many students define paragraphs in terms of length: a paragraph is a group of at least five sentences, a paragraph is half a page long, etc. In reality, though, the unity and coherence of ideas among sentences is what constitutes a paragraph. A paragraph is defined as “a group of sentences or a single sentence that forms a unit." Now I want it to give me the starting 3 or 4 sentences. – Artificial Intellegence Nov 25 '21 at 17:36
  • Show me exapmle of output – Andrew Nov 25 '21 at 21:07
  • I've added the output in my questions now! You can see it :D – Artificial Intellegence Nov 26 '21 at 10:32