Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions vocabulary log helper/LogHelper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import time
import aiohttp
import asyncio
from json import dumps
from random import choice
from bs4 import BeautifulSoup as Beau_Soup

def get_syn_ant(word: str, page: str):
soup = Beau_Soup(page,'html.parser')
synonyms_list = list()
antonyms_list = list()

# getting all the synonyms
for a in soup.select('ul a.css-1kg1yv8'):
synonyms_list.append(a.text)

for a in soup.select('ul.css-1gyuw4i'):
synonyms_list.append(a.text)

for a in soup.select('ul.css-1n6g4vv'):
synonyms_list.append(a.text)

# getting all the antonyms
for a in soup.select('ul a.css-15bafsg'):
antonyms_list.append(a.text)

# chosing random synonym and antonym then return with the word
return {
'word': word,
'synonym' : choice(synonyms_list).strip() if synonyms_list else 'No synonym',
'antonym': choice(antonyms_list).strip() if antonyms_list else 'No antonym'
}

async def get_page(session, word: str):
# get the HTML code of the word page
url_to_get = f"https://www.thesaurus.com/browse/{word}"
async with session.get(url_to_get) as response:
result_data = await response.text()

return get_syn_ant(word, result_data)


async def get_all_pages() :
words_to_look_for = input("Enter the words to look for : ").split()
words_to_look_for = [word.strip() for word in words_to_look_for if word != '']

tasks = list()
async with aiohttp.ClientSession() as session:
for word in words_to_look_for:
task = asyncio.ensure_future(get_page(session, word))
tasks.append(task)

return await asyncio.gather(*tasks)



if '__main__' == __name__:
begin_time = time.time()

asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
syn_ant_for_words = asyncio.run(get_all_pages())
for word_detail in syn_ant_for_words:
print(dumps(word_detail, indent=4))

print("--- %s seconds ---" % (time.time() - begin_time))

64 changes: 64 additions & 0 deletions vocabulary log helper/LogHelperSync.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import time
import requests
from json import dumps
from typing import Dict
from random import choice
from bs4 import BeautifulSoup as Beau_Soup



def get_page(word: str) -> str:
# get the HTML code of the word page
word = word.strip()
url_to_get = f"https://www.thesaurus.com/browse/{word}"
response = requests.get(url=url_to_get)

return response.text


def get_syn_ant(word: str) -> Dict[str, str]:
page = get_page(word=word)
soup = Beau_Soup(page,'html.parser')
synonyms_list = list()
antonyms_list = list()

# getting all the synonyms
for a in soup.select('ul a.css-1kg1yv8'):
synonyms_list.append(a.text)

for a in soup.select('ul.css-1gyuw4i'):
synonyms_list.append(a.text)

for a in soup.select('ul.css-1n6g4vv'):
synonyms_list.append(a.text)

# getting all the antonyms
for a in soup.select('ul a.css-15bafsg'):
antonyms_list.append(a.text)

# chosing random synonym and antonym then return with the word
return {
'word': word,
'synonym' : choice(synonyms_list).strip() if synonyms_list else 'No synonym',
'antonym': choice(antonyms_list).strip() if antonyms_list else 'No antonym'
}




def get_syn_ant_for_words() :
words_to_look_for = input("Enter the words to look for : ").split()
words_to_look_for = [word.strip() for word in words_to_look_for if word != '']

result = [get_syn_ant(word) for word in words_to_look_for]
return result


if '__main__' == __name__:
begin_time = time.time()

syn_ant_for_words = get_syn_ant_for_words()
for word_detail in syn_ant_for_words:
print(dumps(word_detail, indent=4))

print("--- %s seconds ---" % (time.time() - begin_time))
38 changes: 38 additions & 0 deletions vocabulary log helper/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Vocabulary Log Helper
### This project is used for searching synonyms and antonyms for many words as fast as it can.

## What is vocabulary log?
**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.

## How does the code work?
This code work as following :<br>
1. Ask the user to enter the words he want to look for.
2. request the pages from [Thesaurus website](https://www.thesaurus.com/).
3. scrape the pages and get the synonyms and antonyms for the word.
4. print out the found data as a **json** data.

## Features I want to add to this project:
- Output the data as an excel file
- Add a GUI for better user experience


## Files in project:
- `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.
- `LogHelperSync.py` : this is the original version of the code, it is synchronous and take much more time for much more words.

## Requirement to run this code:
- **aiohttp** module for asynchronous requests, to install run this command in the terminal

```bash
pip install aiohttp
```
- **typing** defines a standard notation for Python function and variable type annotations.

```bash
pip install typing
```
- **Beautiful Soup** is a library that makes it easy to scrape information from web pages.

```bash
pip install BeautifulSoup
```