Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated README and added an optional argument #58

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,16 @@ r.get_random_words()
r.word_of_the_day()
```

## Advance Usage
## Advanced Usage

1. To generate single random word we can use these optional parameters
1. To use a personal API key, we can pass the optional parameter api_key when creating the RandomWords object

```python
r = RandomWords(api_key="YOUR_PERSONAL_API_KEY_HERE")

```

2. To generate single random word we can use these optional parameters

- `hasDictionaryDef (string)` - Only return words with dictionary definitions (optional)
- `includePartOfSpeech (string)` - CSV part-of-speech values to include (optional)
Expand All @@ -57,14 +64,15 @@ r.word_of_the_day()
- `maxDictionaryCount (integer)` - Maximum dictionary count (optional)
- `minLength (integer)` - Minimum word length (optional)
- `maxLength (integer)` - Maximum word length (optional)
- `length (integer)` - Exact word length (optional)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This parameter doesn't comply with API spec provided by wordnik API. I think if any dev wanted to get specific word by length, then one can use minLength = maxLength while calling the API.

Plus, I am planning to extend this library by providing various sources, so we can add this optional parameter if providing random word using local dictionary. I still have to do research how, we can pull word from local dictionary present in OS though

Copy link
Author

@RapidCompiler RapidCompiler Mar 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand that the Wordnik API doesn't support the parameter length. But as mentioned in Issue #22 by Erik Boesen, the function call for a word of a specific length is verbose and inefficient. So, what I've done is, I've accepted the optional parameter length from the user, set the max_length and min_length parameters internally to the value given in the parameter by the user and then deleted the length parameter from the kwargs before passing it to the other functions for exception handling. So basically, my change reduces unnecessary verbosity at the user's end because otherwise if I wanted to get a 5 letter word, my call would have to be this

rw.get_random_word(min_length=5, max_length=5)

Now, from my commit, it would change to

rw.get_random_word(length=5)

This is both logical and more efficient for the user calling the function.


```python
r.get_random_word(hasDictionaryDef="true", includePartOfSpeech="noun,verb", minCorpusCount=1, maxCorpusCount=10, minDictionaryCount=1, maxDictionaryCount=10, minLength=5, maxLength=10)

# Output: pediophobia
```

2. To generate list of random word we can use these optional parameters
3. To generate list of random word we can use these optional parameters

- `hasDictionaryDef (string)` - Only return words with dictionary definitions (optional)
- `includePartOfSpeech (string)` - CSV part-of-speech values to include (optional)
Expand All @@ -75,6 +83,7 @@ r.word_of_the_day()
- `maxDictionaryCount (integer)` - Maximum dictionary count (optional)
- `minLength (integer)` - Minimum word length (optional)
- `maxLength (integer)` - Maximum word length (optional)
- `length (integer)` - Exact word length (optional)
- `sortBy (string)` - Attribute to sort by `alpha` or `count` (optional)
- `sortOrder (string)` - Sort direction by `asc` or `desc` (optional)
- `limit (integer)` - Maximum number of results to return (optional)
Expand All @@ -85,7 +94,7 @@ r.word_of_the_day()
# Output: ['ambivert', 'calcspar', 'deaness', 'entrete', 'gades', 'monkeydom', 'outclimbed', 'outdared', 'pistoleers', 'redbugs', 'snake-line', 'subrules', 'subtrends', 'torenia', 'unhides']
```

3. To get word of the day we can use these optional parameters
4. To get word of the day we can use these optional parameters

- `date (string)` - Fetches by date in yyyy-MM-dd (optional)

Expand Down
23 changes: 19 additions & 4 deletions random_word/random_word.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,16 @@ def get_random_word(self, **kwargs):
maxDictionaryCount, int: Maximum dictionary count (optional)
minLength, int: Minimum word length (optional)
maxLength, int: Maximum word length (optional)

length, int: Exact word length (optional)
Returns: String, Random words
"""
params = locals()
try:
if params["kwargs"]["length"]:
params["kwargs"]["minLength"] = params["kwargs"]["maxLength"] = params["kwargs"]["length"]
del params["kwargs"]["length"]
except:
pass

url = "https://api.wordnik.com/v4/words.json/randomWord?"
allParams = [
Expand All @@ -61,9 +68,9 @@ def get_random_word(self, **kwargs):
"minDictionaryCount",
"maxDictionaryCount",
"minLength",
"maxLength",
"maxLength"
]
params = locals()

payload = params["kwargs"]
check_payload_items(payload, allParams)
payload["api_key"] = self.__api_key
Expand Down Expand Up @@ -102,12 +109,20 @@ def get_random_words(self, **kwargs):
maxDictionaryCount, int: Maximum dictionary count (optional)
minLength, int: Minimum word length (optional)
maxLength, int: Maximum word length (optional)
length, int: Exact word length (optional)
sortBy, str: Attribute to sort by (optional)
sortOrder, str: Sort direction (optional)
limit, int: Maximum number of results to return (optional)

Returns: list[Random Words]
"""
params = locals()
try:
if params["kwargs"]["length"]:
params["kwargs"]["minLength"] = params["kwargs"]["maxLength"] = params["kwargs"]["length"]
del params["kwargs"]["length"]
except:
pass

url = "https://api.wordnik.com/v4/words.json/randomWords?"
allParams = [
Expand All @@ -124,7 +139,7 @@ def get_random_words(self, **kwargs):
"sortOrder",
"limit",
]
params = locals()

payload = params["kwargs"]
check_payload_items(payload, allParams)
payload["api_key"] = self.__api_key
Expand Down