Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
Torchaudio
Commits
19f7f971
Unverified
Commit
19f7f971
authored
Oct 11, 2021
by
moto
Committed by
GitHub
Oct 11, 2021
Browse files
Clean up constructor of CMUDict (#1852)
parent
3aa0d573
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
34 additions
and
26 deletions
+34
-26
torchaudio/datasets/cmudict.py
torchaudio/datasets/cmudict.py
+34
-26
No files found.
torchaudio/datasets/cmudict.py
View file @
19f7f971
...
...
@@ -108,49 +108,57 @@ class CMUDict(Dataset):
Args:
root (str or Path): Path to the directory where the dataset is found or downloaded.
exclude_punctuations (bool, optional):
When enabled, exclude the pronounciation of punctuations, such as
`!EXCLAMATION-POINT` and `#HASH-MARK`.
download (bool, optional):
Whether to download the dataset if it is not found at root path. (default: ``False``).
url (str, optional):
The URL to download the dictionary from.
(default: ``"http://svn.code.sf.net/p/cmusphinx/code/trunk/cmudict/cmudict-0.7b"``)
url_symbols (str, optional):
The URL to download the list of symbols from.
(default: ``"http://svn.code.sf.net/p/cmusphinx/code/trunk/cmudict/cmudict-0.7b.symbols"``)
download (bool, optional):
Whether to download the dataset if it is not found at root path. (default: ``False``).
"""
def
__init__
(
self
,
root
:
Union
[
str
,
Path
],
exclude_punctuations
:
bool
=
True
,
*
,
download
:
bool
=
False
,
url
:
str
=
"http://svn.code.sf.net/p/cmusphinx/code/trunk/cmudict/cmudict-0.7b"
,
url_symbols
:
str
=
"http://svn.code.sf.net/p/cmusphinx/code/trunk/cmudict/cmudict-0.7b.symbols"
,
download
:
bool
=
False
,
exclude_punctuations
:
bool
=
True
)
->
None
:
)
->
None
:
self
.
exclude_punctuations
=
exclude_punctuations
root
=
Path
(
root
)
if
not
os
.
path
.
isdir
(
root
):
os
.
mkdir
(
root
)
if
download
:
if
os
.
path
.
isdir
(
root
):
self
.
_root_path
=
Path
(
root
)
if
not
os
.
path
.
isdir
(
self
.
_root_path
):
raise
RuntimeError
(
f
'The root directory does not exist;
{
root
}
'
)
dict_file
=
self
.
_root_path
/
os
.
path
.
basename
(
url
)
symbol_file
=
self
.
_root_path
/
os
.
path
.
basename
(
url_symbols
)
if
not
os
.
path
.
exists
(
dict_file
):
if
not
download
:
raise
RuntimeError
(
'The dictionary file is not found in the following location. '
f
'Set `download=True` to download it.
{
dict_file
}
'
)
checksum
=
_CHECKSUMS
.
get
(
url
,
None
)
download_url
(
url
,
root
,
hash_value
=
checksum
,
hash_type
=
"md5"
)
if
not
os
.
path
.
exists
(
symbol_file
):
if
not
download
:
raise
RuntimeError
(
'The symbol file is not found in the following location. '
f
'Set `download=True` to download it.
{
symbol_file
}
'
)
checksum
=
_CHECKSUMS
.
get
(
url_symbols
,
None
)
download_url
(
url_symbols
,
root
,
hash_value
=
checksum
,
hash_type
=
"md5"
)
else
:
RuntimeError
(
"The argument `root` must be a path to directory, "
f
"but '
{
root
}
' is passed in instead."
)
self
.
_root_path
=
root
basename
=
os
.
path
.
basename
(
url
)
basename_symbols
=
os
.
path
.
basename
(
url_symbols
)
with
open
(
os
.
path
.
join
(
self
.
_root_path
,
basename_
symbol
s
)
,
"r"
)
as
text
:
with
open
(
symbol
_file
,
"r"
)
as
text
:
self
.
_symbols
=
[
line
.
strip
()
for
line
in
text
.
readlines
()]
with
open
(
os
.
path
.
join
(
self
.
_root_path
,
basename
)
,
"r"
,
encoding
=
'latin-1'
)
as
text
:
self
.
_dictionary
=
_parse_dictionary
(
text
.
readlines
(),
exclude_punctuations
=
self
.
exclude_punctuations
)
with
open
(
dict_file
,
"r"
,
encoding
=
'latin-1'
)
as
text
:
self
.
_dictionary
=
_parse_dictionary
(
text
.
readlines
(),
exclude_punctuations
=
self
.
exclude_punctuations
)
def
__getitem__
(
self
,
n
:
int
)
->
Tuple
[
str
,
List
[
str
]]:
"""Load the n-th sample from the dataset.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment