forked from facebookresearch/AugLy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
54 lines (45 loc) · 1.51 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python3
# Copyright (c) Facebook, Inc. and its affiliates.
from copy import deepcopy
from typing import Any, Dict, List, Optional
import augly.text.intensity as txtintensity
def get_func_kwargs(
metadata: Optional[List[Dict[str, Any]]], local_kwargs: Dict[str, Any], **kwargs
) -> Dict[str, Any]:
if metadata is None:
return {}
func_kwargs = deepcopy(local_kwargs)
func_kwargs.pop("metadata")
func_kwargs.update(**kwargs)
return func_kwargs
def get_metadata(
metadata: Optional[List[Dict[str, Any]]],
function_name: str,
texts: Optional[List[str]] = None,
aug_texts: Optional[List[str]] = None,
**kwargs,
) -> None:
if metadata is None:
return
assert isinstance(
metadata, list
), "Expected `metadata` to be set to None or of type list"
assert (
texts is not None
), "Expected `texts` to be passed in if metadata was provided"
assert (
aug_texts is not None
), "Expected `aug_texts` to be passed in if metadata was provided"
metadata.append(
{
"name": function_name,
"input_type": "list" if isinstance(texts, list) else "string",
"src_length": len(texts) if isinstance(texts, list) else 1,
"dst_length": len(aug_texts),
**kwargs,
}
)
intensity_kwargs = {"metadata": metadata[-1], **kwargs}
metadata[-1]["intensity"] = getattr(
txtintensity, f"{function_name}_intensity", lambda **_: 0.0
)(**intensity_kwargs)