Skip to content

Commit

Permalink
Merge pull request #34 from SekouDiaoNlp/dev
Browse files Browse the repository at this point in the history
Implemented complete CLI with ability to save the Lexical Entries to a json file.
  • Loading branch information
SekouDiaoNlp committed Apr 30, 2021
2 parents 32a0ba5 + c7d648a commit dfc93a9
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 67 deletions.
27 changes: 13 additions & 14 deletions pylexique/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
import sys
import click
import json
import joblib
import logging
from pylexique import Lexique383, LexItem
from pprint import pprint
from collections import defaultdict


@click.command(context_settings=dict(help_option_names=["-h", "--help"]))
@click.argument('words', nargs=-1)
@click.option('-o', '--output',
default=None,
help=("Path of the filename for storing the lexical entries"),
help="Path of the json filename for storing the lexical entries",
type=click.STRING)
def main(words, output):
"""Pylexique is a Python wrapper around Lexique83.
Expand All @@ -36,23 +36,22 @@ def main(words, output):
logger.setLevel(logging.INFO)

LEXIQUE = Lexique383()
results = {}
results = defaultdict(list)
for word in words:
results[word] = LEXIQUE.lexique[word]
results[word].append(LEXIQUE.lexique[word])

for i, element in enumerate(results[word]):
if isinstance(element, LexItem):
results[word][i] = element.to_dict()
continue
for index, item in enumerate(element):
results[word][i][index] = item.to_dict()
if output:
with open(output, 'w', encoding='utf-8') as file:
json.dump(results, file, indent=4)
pprint('The Lexical Items have been successfully saved to {0} by pylexique.cli.main.'.format(output))
print('The Lexical Items have been successfully saved to {0} by pylexique.cli.main.'.format(output))
else:
if isinstance(words, tuple):
for word in words:
if isinstance(results[word], list):
for elmt in results[word]:
pprint(elmt.to_dict())
print('\n\n')
else:
pprint(results[word].to_dict())
print('\n\n')
print(json.dumps(results, indent=4))
return


Expand Down
6 changes: 1 addition & 5 deletions pylexique/pylexique.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@

from collections import OrderedDict, defaultdict
import pkg_resources

# import faster_than_csv as csv
from dataclasses import dataclass
from typing import ClassVar
from time import time
from .utils import set_save_folder

_RESOURCE_PACKAGE = __name__

Expand Down Expand Up @@ -161,9 +160,6 @@ def _convert_entries(row_fields):
try:
value = int(value)
except ValueError:
print(
"the value {} is of the wrong type for the attribute '{}'. Keeping value as string.\n".format(
value, attr))
errors[row_fields[0]].append({attr: value})
value = value
converted_row_fields.append(value)
Expand Down
45 changes: 0 additions & 45 deletions pylexique/utils.py
Original file line number Diff line number Diff line change
@@ -1,49 +1,4 @@
from dataclasses import dataclass
from pprint import pprint
from inspect import getmembers
from types import FunctionType


def set_save_folder(folder):
"""
Sets the folder in which the LexicalItems will be downloaded and saved.
:param folder: string.
Folder path.
:return: string.
Folder path.
"""
if not folder:
folder = os.path.join(os.path.expanduser("~"), 'Documents', 'pylexique')
else:
folder = os.path.join(folder, 'pylexique')
return folder

def attributes(obj):
disallowed_names = {
name for name, value in getmembers(type(obj))
if isinstance(value, FunctionType)}
return {
name: getattr(obj, name) for name in dir(obj)
if name[0:1] != '_' and name not in disallowed_names and hasattr(obj, name)}

def show_attributes(object):
temp = vars(object)
for item in temp:
print(item, ':', temp[item])

def print_attributes(obj):
pprint(attributes(obj))


def vdir(obj):
"""
| This function pretty-display the lexical items in Lexique383.
:param obj:
:return:
"""
return [x for x in dir(obj) if not x.startswith('__')]


def dataclass_with_default_init(_cls=None, *args, **kwargs):
Expand Down
3 changes: 1 addition & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
colorama
tqdm
# faster_than_csv
Click>=7.1
pandas
setuptools~=49.6.0
pytest~=6.2.3
joblib~=1.0.1
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
with open('HISTORY.rst') as history_file:
history = history_file.read()

requirements = ['joblib', 'colorama', 'tqdm', 'Click>=7.1', 'joblib', 'pandas']
requirements = ['joblib', 'colorama', 'tqdm', 'Click>=7.1'] # integrate 'faster_than_csv'

setup_requirements = ['pytest-runner', ]

Expand Down

0 comments on commit dfc93a9

Please sign in to comment.