Remove unnecessary nested dict
MEMBER
We can probably scan the dataset scripts with a regexpr to try to identify this pattern cc @patrickvonplaten maybe
2020-07-13T08:47:41Z
https://github.com/huggingface/datasets/pull/385#issuecomment-657416255
https://api.github.com/repos/huggingface/datasets/issues/385
MDEyOklzc3VlQ29tbWVudDY1NzQxNjI1NQ==
https://api.github.com/repos/huggingface/datasets/issues/comments/657416255/reactions
2020-07-13T08:47:41Z
https://api.github.com/repos/huggingface/datasets/issues/comments/657416255
https://avatars.githubusercontent.com/u/7353373?v=4
https://api.github.com/users/thomwolf/events{/privacy}
https://api.github.com/users/thomwolf/followers
https://api.github.com/users/thomwolf/following{/other_user}
https://api.github.com/users/thomwolf/gists{/gist_id}
https://github.com/thomwolf
thomwolf
MDQ6VXNlcjczNTMzNzM=
https://api.github.com/users/thomwolf/orgs
https://api.github.com/users/thomwolf/received_events
https://api.github.com/users/thomwolf/repos
https://api.github.com/users/thomwolf/starred{/owner}{/repo}
https://api.github.com/users/thomwolf/subscriptions
User
https://api.github.com/users/thomwolf
public
CONTRIBUTOR
@mariamabarham This script should work. I tested it for a couple of datasets. There might be exceptions where the script breaks - did not test everything.

```python
#!/usr/bin/env python3

from nlp import prepare_module, DownloadConfig, import_main_class, hf_api
import tempfile


def scan_for_nested_unnecessary_dict(dataset_name):

    def load_builder_class(dataset_name):
        module_path = prepare_module(dataset_name, download_config=DownloadConfig(force_download=True))
        return import_main_class(module_path)

    def load_configs(dataset_name):
        builder_cls = load_builder_class(dataset_name)
        if len(builder_cls.BUILDER_CONFIGS) == 0:
            return [None]
        return builder_cls.BUILDER_CONFIGS

    def scan_features_for_nested_dict(features):
        is_sequence = False
        if hasattr(features, "_type"):
            if features._type != 'Sequence':
                return False
            else:
                is_sequence = True
                features = features.feature

        if isinstance(features, list):
            for value in features:
                if scan_features_for_nested_dict(value):
                    return True
            return False

        elif isinstance(features, dict):
            for key, value in features.items():
                if is_sequence and len(features.keys()) == 1 and hasattr(features[key], "_type") and features[key]._type != "Sequence":
                    return True
                if scan_features_for_nested_dict(value):
                    return True
            return False
        elif hasattr(features, "_type"):
            return False
        else:
            raise ValueError(f"{features} should be either a list, a dict or a feature")

    configs = load_configs(dataset_name)

    for config in configs:
        with tempfile.TemporaryDirectory() as processed_temp_dir:
            # create config and dataset
            dataset_builder_cls = load_builder_class(dataset_name)
            name = config.name if config is not None else None
            dataset_builder = dataset_builder_cls(name=name, cache_dir=processed_temp_dir)

        is_nested_dict_in_dataset = scan_features_for_nested_dict(dataset_builder._info().features)
        if is_nested_dict_in_dataset:
            print(f"{dataset_name} with {name} needs refactoring")


if __name__ == "__main__":
    scan_for_nested_unnecessary_dict("race")  # prints True
    scan_for_nested_unnecessary_dict("mlqa")  # prints True
    scan_for_nested_unnecessary_dict("squad")  # prints Nothing

    # ran the following lines for 1min and seems to work -> didn't check for all datasets though
#    api = hf_api.HfApi()
#    all_datasets = [x.id for x in api.dataset_list(with_community_datasets=False)]
#    for dataset in all_datasets:
#        scan_for_nested_unnecessary_dict(dataset)
```
2020-07-13T13:59:52Z
https://github.com/huggingface/datasets/pull/385#issuecomment-657578425
https://api.github.com/repos/huggingface/datasets/issues/385
MDEyOklzc3VlQ29tbWVudDY1NzU3ODQyNQ==
https://api.github.com/repos/huggingface/datasets/issues/comments/657578425/reactions
2020-07-15T11:27:38Z
https://api.github.com/repos/huggingface/datasets/issues/comments/657578425
https://avatars.githubusercontent.com/u/23423619?v=4
https://api.github.com/users/patrickvonplaten/events{/privacy}
https://api.github.com/users/patrickvonplaten/followers
https://api.github.com/users/patrickvonplaten/following{/other_user}
https://api.github.com/users/patrickvonplaten/gists{/gist_id}
https://github.com/patrickvonplaten
patrickvonplaten
MDQ6VXNlcjIzNDIzNjE5
https://api.github.com/users/patrickvonplaten/orgs
https://api.github.com/users/patrickvonplaten/received_events
https://api.github.com/users/patrickvonplaten/repos
https://api.github.com/users/patrickvonplaten/starred{/owner}{/repo}
https://api.github.com/users/patrickvonplaten/subscriptions
User
https://api.github.com/users/patrickvonplaten
public
CONTRIBUTOR
> @mariamabarham This script should work. I tested it for a couple of datasets. There might be exceptions where the script breaks - did not test everything.
> 
> ```python
> #!/usr/bin/env python3
> 
> from nlp import prepare_module, DownloadConfig, import_main_class, hf_api
> import tempfile
> 
> 
> def scan_for_nested_unnecessary_dict(dataset_name):
> 
>     def load_builder_class(dataset_name):
>         module_path = prepare_module(dataset_name, download_config=DownloadConfig(force_download=True))
>         return import_main_class(module_path)
> 
>     def load_configs(dataset_name):
>         builder_cls = load_builder_class(dataset_name)
>         if len(builder_cls.BUILDER_CONFIGS) == 0:
>             return [None]
>         return builder_cls.BUILDER_CONFIGS
> 
>     def scan_features_for_nested_dict(features):
>         is_sequence = False
>         if hasattr(features, "_type"):
>             if features._type != 'Sequence':
>                 return False
>             else:
>                 is_sequence = True
>                 features = features.feature
> 
>         if isinstance(features, list):
>             for value in features:
>                 if scan_features_for_nested_dict(value):
>                     return True
>             return False
> 
>         elif isinstance(features, dict):
>             for key, value in features.items():
>                 if is_sequence and len(features.keys()) == 1 and hasattr(features[key], "_type") and features[key]._type != "Sequence":
>                     return True
>                 if scan_features_for_nested_dict(value):
>                     return True
>             return False
>         else:
>             raise ValueError(f"{features} should be either a list of a dict")
> 
>     configs = load_configs(dataset_name)
> 
>     for config in configs:
>         with tempfile.TemporaryDirectory() as processed_temp_dir:
>             # create config and dataset
>             dataset_builder_cls = load_builder_class(dataset_name)
>             name = config.name if config is not None else None
>             dataset_builder = dataset_builder_cls(name=name, cache_dir=processed_temp_dir)
> 
>         is_nested_dict_in_dataset = scan_features_for_nested_dict(dataset_builder._info().features)
>         if is_nested_dict_in_dataset:
>             print(f"{dataset_name} with {name} needs refactoring")
> 
> 
> if __name__ == "__main__":
>     scan_for_nested_unnecessary_dict("race")  # prints True
>     scan_for_nested_unnecessary_dict("mlqa")  # prints True
>     scan_for_nested_unnecessary_dict("squad")  # prints Nothing
> 
>     # ran the following lines for 1min and seems to work -> didn't check for all datasets though
> #    api = hf_api.HfApi()
> #    all_datasets = [x.id for x in api.dataset_list(with_community_datasets=False)]
> #    for dataset in all_datasets:
> #        scan_for_nested_unnecessary_dict(dataset)
> ```

Great, I will try it
2020-07-13T14:43:38Z
https://github.com/huggingface/datasets/pull/385#issuecomment-657602352
https://api.github.com/repos/huggingface/datasets/issues/385
MDEyOklzc3VlQ29tbWVudDY1NzYwMjM1Mg==
https://api.github.com/repos/huggingface/datasets/issues/comments/657602352/reactions
2020-07-13T14:43:38Z
https://api.github.com/repos/huggingface/datasets/issues/comments/657602352
https://avatars.githubusercontent.com/u/38249783?v=4
https://api.github.com/users/mariamabarham/events{/privacy}
https://api.github.com/users/mariamabarham/followers
https://api.github.com/users/mariamabarham/following{/other_user}
https://api.github.com/users/mariamabarham/gists{/gist_id}
https://github.com/mariamabarham
mariamabarham
MDQ6VXNlcjM4MjQ5Nzgz
https://api.github.com/users/mariamabarham/orgs
https://api.github.com/users/mariamabarham/received_events
https://api.github.com/users/mariamabarham/repos
https://api.github.com/users/mariamabarham/starred{/owner}{/repo}
https://api.github.com/users/mariamabarham/subscriptions
User
https://api.github.com/users/mariamabarham
public
MEMBER
I'm not sure the work on this PR was finished @lhoestq  cc @mariamabarham @patrickvonplaten 
2020-07-15T10:07:13Z
https://github.com/huggingface/datasets/pull/385#issuecomment-658678138
https://api.github.com/repos/huggingface/datasets/issues/385
MDEyOklzc3VlQ29tbWVudDY1ODY3ODEzOA==
https://api.github.com/repos/huggingface/datasets/issues/comments/658678138/reactions
2020-07-15T10:07:33Z
https://api.github.com/repos/huggingface/datasets/issues/comments/658678138
https://avatars.githubusercontent.com/u/7353373?v=4
https://api.github.com/users/thomwolf/events{/privacy}
https://api.github.com/users/thomwolf/followers
https://api.github.com/users/thomwolf/following{/other_user}
https://api.github.com/users/thomwolf/gists{/gist_id}
https://github.com/thomwolf
thomwolf
MDQ6VXNlcjczNTMzNzM=
https://api.github.com/users/thomwolf/orgs
https://api.github.com/users/thomwolf/received_events
https://api.github.com/users/thomwolf/repos
https://api.github.com/users/thomwolf/starred{/owner}{/repo}
https://api.github.com/users/thomwolf/subscriptions
User
https://api.github.com/users/thomwolf
public
MEMBER
Sorry for that, apparently there are other datasets that could have unnecessary nested dicts.
We can have another PR to scan and fix the other datasets.

2020-07-15T11:04:38Z
https://github.com/huggingface/datasets/pull/385#issuecomment-658704029
https://api.github.com/repos/huggingface/datasets/issues/385
MDEyOklzc3VlQ29tbWVudDY1ODcwNDAyOQ==
https://api.github.com/repos/huggingface/datasets/issues/comments/658704029/reactions
2020-07-15T11:04:38Z
https://api.github.com/repos/huggingface/datasets/issues/comments/658704029
https://avatars.githubusercontent.com/u/42851186?v=4
https://api.github.com/users/lhoestq/events{/privacy}
https://api.github.com/users/lhoestq/followers
https://api.github.com/users/lhoestq/following{/other_user}
https://api.github.com/users/lhoestq/gists{/gist_id}
https://github.com/lhoestq
lhoestq
MDQ6VXNlcjQyODUxMTg2
https://api.github.com/users/lhoestq/orgs
https://api.github.com/users/lhoestq/received_events
https://api.github.com/users/lhoestq/repos
https://api.github.com/users/lhoestq/starred{/owner}{/repo}
https://api.github.com/users/lhoestq/subscriptions
User
https://api.github.com/users/lhoestq
public
This PR is removing unnecessary nested dictionary used in some datasets. For now the following datasets are updated:

- MLQA

- RACE

Will be adding more if necessary.

#378 