Skip to content

Commit 76b86c1

Browse files
authored
Merge pull request larymak#118 from HazardCodeBolt/plus-voc-log-helper
Plus voc log helper
2 parents 2338c43 + 9eb300d commit 76b86c1

File tree

3 files changed

+168
-0
lines changed

3 files changed

+168
-0
lines changed

vocabulary log helper/LogHelper.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import time
2+
import aiohttp
3+
import asyncio
4+
from json import dumps
5+
from random import choice
6+
from bs4 import BeautifulSoup as Beau_Soup
7+
8+
def get_syn_ant(word: str, page: str):
9+
soup = Beau_Soup(page,'html.parser')
10+
synonyms_list = list()
11+
antonyms_list = list()
12+
13+
# getting all the synonyms
14+
for a in soup.select('ul a.css-1kg1yv8'):
15+
synonyms_list.append(a.text)
16+
17+
for a in soup.select('ul.css-1gyuw4i'):
18+
synonyms_list.append(a.text)
19+
20+
for a in soup.select('ul.css-1n6g4vv'):
21+
synonyms_list.append(a.text)
22+
23+
# getting all the antonyms
24+
for a in soup.select('ul a.css-15bafsg'):
25+
antonyms_list.append(a.text)
26+
27+
# chosing random synonym and antonym then return with the word
28+
return {
29+
'word': word,
30+
'synonym' : choice(synonyms_list).strip() if synonyms_list else 'No synonym',
31+
'antonym': choice(antonyms_list).strip() if antonyms_list else 'No antonym'
32+
}
33+
34+
async def get_page(session, word: str):
35+
# get the HTML code of the word page
36+
url_to_get = f"https://www.thesaurus.com/browse/{word}"
37+
async with session.get(url_to_get) as response:
38+
result_data = await response.text()
39+
40+
return get_syn_ant(word, result_data)
41+
42+
43+
async def get_all_pages() :
44+
words_to_look_for = input("Enter the words to look for : ").split()
45+
words_to_look_for = [word.strip() for word in words_to_look_for if word != '']
46+
47+
tasks = list()
48+
async with aiohttp.ClientSession() as session:
49+
for word in words_to_look_for:
50+
task = asyncio.ensure_future(get_page(session, word))
51+
tasks.append(task)
52+
53+
return await asyncio.gather(*tasks)
54+
55+
56+
57+
if '__main__' == __name__:
58+
begin_time = time.time()
59+
60+
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
61+
syn_ant_for_words = asyncio.run(get_all_pages())
62+
for word_detail in syn_ant_for_words:
63+
print(dumps(word_detail, indent=4))
64+
65+
print("--- %s seconds ---" % (time.time() - begin_time))
66+
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import time
2+
import requests
3+
from json import dumps
4+
from typing import Dict
5+
from random import choice
6+
from bs4 import BeautifulSoup as Beau_Soup
7+
8+
9+
10+
def get_page(word: str) -> str:
11+
# get the HTML code of the word page
12+
word = word.strip()
13+
url_to_get = f"https://www.thesaurus.com/browse/{word}"
14+
response = requests.get(url=url_to_get)
15+
16+
return response.text
17+
18+
19+
def get_syn_ant(word: str) -> Dict[str, str]:
20+
page = get_page(word=word)
21+
soup = Beau_Soup(page,'html.parser')
22+
synonyms_list = list()
23+
antonyms_list = list()
24+
25+
# getting all the synonyms
26+
for a in soup.select('ul a.css-1kg1yv8'):
27+
synonyms_list.append(a.text)
28+
29+
for a in soup.select('ul.css-1gyuw4i'):
30+
synonyms_list.append(a.text)
31+
32+
for a in soup.select('ul.css-1n6g4vv'):
33+
synonyms_list.append(a.text)
34+
35+
# getting all the antonyms
36+
for a in soup.select('ul a.css-15bafsg'):
37+
antonyms_list.append(a.text)
38+
39+
# chosing random synonym and antonym then return with the word
40+
return {
41+
'word': word,
42+
'synonym' : choice(synonyms_list).strip() if synonyms_list else 'No synonym',
43+
'antonym': choice(antonyms_list).strip() if antonyms_list else 'No antonym'
44+
}
45+
46+
47+
48+
49+
def get_syn_ant_for_words() :
50+
words_to_look_for = input("Enter the words to look for : ").split()
51+
words_to_look_for = [word.strip() for word in words_to_look_for if word != '']
52+
53+
result = [get_syn_ant(word) for word in words_to_look_for]
54+
return result
55+
56+
57+
if '__main__' == __name__:
58+
begin_time = time.time()
59+
60+
syn_ant_for_words = get_syn_ant_for_words()
61+
for word_detail in syn_ant_for_words:
62+
print(dumps(word_detail, indent=4))
63+
64+
print("--- %s seconds ---" % (time.time() - begin_time))

vocabulary log helper/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Vocabulary Log Helper
2+
### This project is used for searching synonyms and antonyms for many words as fast as it can.
3+
4+
## What is vocabulary log?
5+
**vocabulary log** is a college assignment that require the student to search for the synonyms, antonyms of certain words and make a statement using that word, I am a first year college student and I had a hard time doing this assignment because the time it can take, so I decided to make a code that can help me with the assignment.
6+
7+
## How does the code work?
8+
This code work as following :<br>
9+
1. Ask the user to enter the words he want to look for.
10+
2. request the pages from [Thesaurus website](https://www.thesaurus.com/).
11+
3. scrape the pages and get the synonyms and antonyms for the word.
12+
4. print out the found data as a **json** data.
13+
14+
## Features I want to add to this project:
15+
- Output the data as an excel file
16+
- Add a GUI for better user experience
17+
18+
19+
## Files in project:
20+
- `LogHelper.py` : this file is an asynchronous version of the code, I made this version so I can make the part of the code take less time.
21+
- `LogHelperSync.py` : this is the original version of the code, it is synchronous and take much more time for much more words.
22+
23+
## Requirement to run this code:
24+
- **aiohttp** module for asynchronous requests, to install run this command in the terminal
25+
26+
```bash
27+
pip install aiohttp
28+
```
29+
- **typing** defines a standard notation for Python function and variable type annotations.
30+
31+
```bash
32+
pip install typing
33+
```
34+
- **Beautiful Soup** is a library that makes it easy to scrape information from web pages.
35+
36+
```bash
37+
pip install BeautifulSoup
38+
```

0 commit comments

Comments
 (0)