سلام و درود امیدوارم حالتون عالی باشه!
اینجا قراره که باهم با کتابخانه wikipedia آشنا بشیم و یک دستیار شخصی ساده بسازیم :)
این کتابخونه چیه و به چه دردی میخوره؟
این کتابخونه به برنامه نویس ها اجازه میده که به اطلاعات موجود در wikipedia به راحتی دسترسی داشته باشن. تا بتوانند با ارسال یک جمله یا کلمه به خلاصه اطلاعات یا لیست مقالات یا یک صفحه کامل از اون مقاله مربوط به اون موضوع رو بدست بیارن.
خب حالا که باش آشنا شدیم بهتر کار رو شروع کنیم. برای نصب این دستور را وارد کنید:
pip install wikipedia
الان دیگه وقتش شده دست به کد بشیم پس لِس بو :)
خب اول یه متغییر تعریف میکنیم برای جمله یا کلمه:
text = 'Oppenheimer'
اینجا من میخوام یسری اطلاعات راجب Oppenheimer بدست بیارم. قدم بعدی ارسال به ویکی پدیاست و ریختن جواب در یک متغییر:
summary = wikipedia.summary(text)
با استفاده از summary به خلاصه اطلاعات دست پیدا میکنیم.
و حالا وقتش شده اطلاعات رو مرتب کنیم یعنی تبدیل به چندین خط بشه خب خط باچی معلوم میشه؟ با .
lines = summary.split('. ')
خب حالا خط هارو هم جدا کردیم و ریختیم تو متغییر الان باید از اون خط های بدست اومده چنتاش رو به صورت تصادفی انتخاب کنیم! چون اطلاعاتی که بدست میاد یکم زیاد هست باز هم که فرقش رو نشونتون میدم در آخر.
selected_lines = random.sample(lines, min(2, len(lines)))
result = '. '.join(selected_lines) print(result)
اینجا عدد 2 تعداد خط رندومی هست که خواستم و در یک متغییر دیگه خط هارو میچسبوینم بهم که دنباله دار بشه.
و خب خروجی این است!
With his students, he also made contributions to the theory of neutron stars and black holes, quantum field theory, and the interactions of cosmic rays.
In 1942, Oppenheimer was recruited to work on the Manhattan Project, and in 1943 he was appointed director of the project's Los Alamos Laboratory in New Mexico, tasked with developing the first nuclear weapons. In August 1945, the weapons were used against Japan in the bombings of Hiroshima and Nagasaki, the only use of nuclear weapons in an armed conflict.
اما اگر خود متغییر lines را در خروجی چاپ کنیم شامل اطلاعات زیادی هست خودتون ببینید :)
Robert Oppenheimer (born Julius Robert Oppenheimer; OP-ən-hy-mər; April 22, 1904 – February 18, 1967) was an American theoretical physicist', 'He was director of the Manhattan Project\'s Los Alamos Laboratory during World War II and is often called the "father of the atomic bomb".\nBorn in New York City, Oppenheimer earned a bachelor of arts degree in chemistry from Harvard University in 1925 and a doctorate in physics from the University of Göttingen in
Germany in 1927, where he studied under Max Born', 'After research at other institutions, he joined the physics department at the University of California,
Berkeley, where he became a full professor in 1936', 'He made significant contributions to theoretical physics, including achievements in quantum mechanics
and nuclear physics such as the Born–Oppenheimer approximation for molecular wave functions, work on the theory of electrons and positrons, the Oppenheimer–Phillips process in nuclear fusion, and early work on quantum tunneling', "With his students, he also made contributions to the theory of neutron stars and
black holes, quantum field theory, and the interactions of cosmic rays.\nIn 1942, Oppenheimer was recruited to work on the Manhattan Project, and in 1943 he was appointed director of the project's Los Alamos Laboratory in New Mexico, tasked with developing the first nuclear weapons", "His leadership and scientific expertise were instrumental in the project's success", 'On July 16, 1945, he was present at the first test of the atomic bomb, Trinity', 'In August 1945, the weapons were used against Japan in the bombings of Hiroshima and Nagasaki, the only use of nuclear weapons in an armed conflict.\nIn 1947, Oppenheimer became the director of the Institute for Advanced Study in Princeton, New Jersey, and chaired the influential General Advisory Committee of the newly created U.S', 'Atomic Energy Commission', 'He lobbied for international control of nuclear power to avert nuclear proliferation and a nuclear arms race with the
Soviet Union', 'He opposed the development of the hydrogen bomb during a 1949–1950 governmental debate on the question and subsequently took positions on defense-related issues that provoked the ire of some U.S', 'government and military factions', "During the second Red Scare, Oppenheimer's stances, together with his past associations with the Communist Party USA, led to the revocation of his security clearance, following a 1954 security hearing", "This effectively ended his access to the government's atomic secrets and his career as a nuclear physicist", 'Although stripped of his direct political influence, Oppenheimer nevertheless continued to lecture, write, and work in physics', 'In 1963, as a gesture of political rehabilitation, he was given the Enrico Fermi Award', 'He died four years later, of throat cancer', 'In 2022, the federal government vacated the 1954 revocation of his security clearance.
شما میتونید این کد رو با کتابخونه های دیگه ترکیب کنید که ورودی صدا از کاربر بگیرید و در خروجی هم متن رو توسط کتابخانه pyttsx3 با صدای مرد یا زن بخوانید!
امیدوارم خوشتون اومده باشه :)