michaelnetbiz commited on
Commit
230f31f
1 Parent(s): d20c778

More text cleaning stuff

Browse files
Files changed (2) hide show
  1. kendex/leviticus.py +55 -56
  2. kendex/prep_push_to_hf.py +28 -13
kendex/leviticus.py CHANGED
@@ -1,72 +1,69 @@
1
- """
2
- text normalization functions
3
- """
4
-
5
  from unidecode import unidecode
6
  import inflect
7
  import re
8
 
9
  _inflect = inflect.engine()
10
- _comma_number_re = re.compile(r'([0-9][0-9\,]+[0-9])')
11
- _decimal_number_re = re.compile(r'([0-9]+\.[0-9]+)')
12
- _pounds_re = re.compile(r'£([0-9\,]*[0-9]+)')
13
- _dollars_re = re.compile(r'\$([0-9\.\,]*[0-9]+)')
14
- _ordinal_re = re.compile(r'[0-9]+(st|nd|rd|th)')
15
- _number_re = re.compile(r'[0-9]+')
16
-
17
- # Regular expression matching whitespace:
18
- _whitespace_re = re.compile(r'\s+')
19
-
20
- # List of (regular expression, replacement) pairs for abbreviations:
21
- _abbreviations = [(re.compile('\\b%s\\.' % x[0], re.IGNORECASE), x[1]) for x in [
22
- ('mrs', 'misess'),
23
- ('mr', 'mister'),
24
- ('dr', 'doctor'),
25
- ('st', 'saint'),
26
- ('co', 'company'),
27
- ('jr', 'junior'),
28
- ('maj', 'major'),
29
- ('gen', 'general'),
30
- ('drs', 'doctors'),
31
- ('rev', 'reverend'),
32
- ('lt', 'lieutenant'),
33
- ('hon', 'honorable'),
34
- ('sgt', 'sergeant'),
35
- ('capt', 'captain'),
36
- ('esq', 'esquire'),
37
- ('ltd', 'limited'),
38
- ('col', 'colonel'),
39
- ('ft', 'fort'),
40
- ]]
41
 
42
 
43
  def _remove_commas(m):
44
- return m.group(1).replace(',', '')
45
 
46
 
47
  def _expand_decimal_point(m):
48
- return m.group(1).replace('.', ' point ')
49
 
50
 
51
  def _expand_dollars(m):
52
  match = m.group(1)
53
- parts = match.split('.')
54
  if len(parts) > 2:
55
- return match + ' dollars' # Unexpected format
 
56
  dollars = int(parts[0]) if parts[0] else 0
57
  cents = int(parts[1]) if len(parts) > 1 and parts[1] else 0
58
  if dollars and cents:
59
- dollar_unit = 'dollar' if dollars == 1 else 'dollars'
60
- cent_unit = 'cent' if cents == 1 else 'cents'
61
- return '%s %s, %s %s' % (dollars, dollar_unit, cents, cent_unit)
62
  elif dollars:
63
- dollar_unit = 'dollar' if dollars == 1 else 'dollars'
64
- return '%s %s' % (dollars, dollar_unit)
65
  elif cents:
66
- cent_unit = 'cent' if cents == 1 else 'cents'
67
- return '%s %s' % (cents, cent_unit)
68
  else:
69
- return 'zero dollars'
70
 
71
 
72
  def _expand_ordinal(m):
@@ -75,22 +72,24 @@ def _expand_ordinal(m):
75
 
76
  def _expand_number(m):
77
  num = int(m.group(0))
78
- if num > 1000 and num < 3000:
79
  if num == 2000:
80
- return 'two thousand'
81
- elif num > 2000 and num < 2010:
82
- return 'two thousand ' + _inflect.number_to_words(num % 100)
83
  elif num % 100 == 0:
84
- return _inflect.number_to_words(num // 100) + ' hundred'
85
  else:
86
- return _inflect.number_to_words(num, andword='', zero='oh', group=2).replace(', ', ' ')
 
 
87
  else:
88
- return _inflect.number_to_words(num, andword='')
89
 
90
 
91
  def normalize_numbers(text):
92
  text = re.sub(_comma_number_re, _remove_commas, text)
93
- text = re.sub(_pounds_re, r'\1 pounds', text)
94
  text = re.sub(_dollars_re, _expand_dollars, text)
95
  text = re.sub(_decimal_number_re, _expand_decimal_point, text)
96
  text = re.sub(_ordinal_re, _expand_ordinal, text)
@@ -109,7 +108,7 @@ def expand_numbers(text):
109
 
110
 
111
  def collapse_whitespace(text):
112
- return re.sub(_whitespace_re, ' ', text)
113
 
114
 
115
  def convert_to_ascii(text):
 
 
 
 
 
1
  from unidecode import unidecode
2
  import inflect
3
  import re
4
 
5
  _inflect = inflect.engine()
6
+ _comma_number_re = re.compile(r"([0-9][0-9\,]+[0-9])")
7
+ _decimal_number_re = re.compile(r"([0-9]+\.[0-9]+)")
8
+ _pounds_re = re.compile(r"£([0-9\,]*[0-9]+)")
9
+ _dollars_re = re.compile(r"\$([0-9\.\,]*[0-9]+)")
10
+ _ordinal_re = re.compile(r"[0-9]+(st|nd|rd|th)")
11
+ _number_re = re.compile(r"[0-9]+")
12
+
13
+ _whitespace_re = re.compile(r"\s+")
14
+ _abbreviations = [
15
+ (re.compile("\\b%s\\." % x[0], re.IGNORECASE), x[1])
16
+ for x in [
17
+ ("mrs", "misess"),
18
+ ("mr", "mister"),
19
+ ("dr", "doctor"),
20
+ ("st", "saint"),
21
+ ("co", "company"),
22
+ ("jr", "junior"),
23
+ ("maj", "major"),
24
+ ("gen", "general"),
25
+ ("drs", "doctors"),
26
+ ("rev", "reverend"),
27
+ ("lt", "lieutenant"),
28
+ ("hon", "honorable"),
29
+ ("sgt", "sergeant"),
30
+ ("capt", "captain"),
31
+ ("esq", "esquire"),
32
+ ("ltd", "limited"),
33
+ ("col", "colonel"),
34
+ ("ft", "fort"),
35
+ ]
36
+ ]
37
 
38
 
39
  def _remove_commas(m):
40
+ return m.group(1).replace(",", "")
41
 
42
 
43
  def _expand_decimal_point(m):
44
+ return m.group(1).replace(".", " point ")
45
 
46
 
47
  def _expand_dollars(m):
48
  match = m.group(1)
49
+ parts = match.split(".")
50
  if len(parts) > 2:
51
+ return match + " dollars" # Unexpected format
52
+
53
  dollars = int(parts[0]) if parts[0] else 0
54
  cents = int(parts[1]) if len(parts) > 1 and parts[1] else 0
55
  if dollars and cents:
56
+ dollar_unit = "dollar" if dollars == 1 else "dollars"
57
+ cent_unit = "cent" if cents == 1 else "cents"
58
+ return "%s %s, %s %s" % (dollars, dollar_unit, cents, cent_unit)
59
  elif dollars:
60
+ dollar_unit = "dollar" if dollars == 1 else "dollars"
61
+ return "%s %s" % (dollars, dollar_unit)
62
  elif cents:
63
+ cent_unit = "cent" if cents == 1 else "cents"
64
+ return "%s %s" % (cents, cent_unit)
65
  else:
66
+ return "zero dollars"
67
 
68
 
69
  def _expand_ordinal(m):
 
72
 
73
  def _expand_number(m):
74
  num = int(m.group(0))
75
+ if 1000 < num < 3000:
76
  if num == 2000:
77
+ return "two thousand"
78
+ elif 2000 < num < 2010:
79
+ return "two thousand " + _inflect.number_to_words(num % 100)
80
  elif num % 100 == 0:
81
+ return _inflect.number_to_words(num // 100) + " hundred"
82
  else:
83
+ return _inflect.number_to_words(
84
+ num, andword="", zero="oh", group=2
85
+ ).replace(", ", " ")
86
  else:
87
+ return _inflect.number_to_words(num, andword="")
88
 
89
 
90
  def normalize_numbers(text):
91
  text = re.sub(_comma_number_re, _remove_commas, text)
92
+ text = re.sub(_pounds_re, r"\1 pounds", text)
93
  text = re.sub(_dollars_re, _expand_dollars, text)
94
  text = re.sub(_decimal_number_re, _expand_decimal_point, text)
95
  text = re.sub(_ordinal_re, _expand_ordinal, text)
 
108
 
109
 
110
  def collapse_whitespace(text):
111
+ return re.sub(_whitespace_re, " ", text)
112
 
113
 
114
  def convert_to_ascii(text):
kendex/prep_push_to_hf.py CHANGED
@@ -5,6 +5,7 @@ import librosa
5
  import numpy as np
6
  import pandas as pd
7
  from datasets import Audio, Dataset, DatasetDict
 
8
 
9
  from leviticus import normalize
10
 
@@ -12,6 +13,10 @@ MAX_DURATION_IN_SECONDS = 10.0
12
  MIN_DURATION_IN_SECONDS = 1.0
13
  MAX_LEN = 50
14
  MIN_LEN = 5
 
 
 
 
15
 
16
 
17
  def duration_filter(item):
@@ -28,12 +33,25 @@ def text_mapper(item):
28
  return item
29
 
30
 
31
- def create_dataset(item):
32
- dataset = Dataset.from_pandas(item)
33
- dataset = dataset.cast_column("audio", Audio(sampling_rate=16_000))
34
- dataset = dataset.filter(text_filter, input_columns=["text"])
35
- dataset = dataset.filter(duration_filter, input_columns=["duration"])
36
- return dataset
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
 
39
  def main():
@@ -53,15 +71,12 @@ def main():
53
 
54
  df = pd.DataFrame(data).sample(frac=1, random_state=666).reset_index(drop=True)
55
 
56
- train, test = np.split(df, [int(0.9 * len(df))])
57
 
58
- train_dataset = create_dataset(train)
59
- train_dataset = train_dataset.map(text_mapper)
60
- test_dataset = create_dataset(test)
61
- test_dataset = test_dataset.map(text_mapper)
62
 
63
- full_dataset = DatasetDict({"train": train_dataset, "test": test_dataset})
64
- full_dataset.push_to_hub("michaelnetbiz/Kendex")
65
 
66
 
67
  if __name__ == "__main__":
 
5
  import numpy as np
6
  import pandas as pd
7
  from datasets import Audio, Dataset, DatasetDict
8
+ from transformers import AutoTokenizer
9
 
10
  from leviticus import normalize
11
 
 
13
  MIN_DURATION_IN_SECONDS = 1.0
14
  MAX_LEN = 50
15
  MIN_LEN = 5
16
+ SR = 16_000
17
+ TOKENIZER_CHECKPOINT = "distilbert-base-uncased-finetuned-sst-2-english"
18
+
19
+ tokenizer = AutoTokenizer.from_pretrained(TOKENIZER_CHECKPOINT)
20
 
21
 
22
  def duration_filter(item):
 
33
  return item
34
 
35
 
36
+ def create_datasets(df):
37
+ def create_dataset(df_slice):
38
+ audio_column = "audio"
39
+ text_column = "text"
40
+ duration_column = "duration"
41
+
42
+ dataset = Dataset.from_pandas(df_slice)
43
+ dataset = dataset.cast_column(audio_column, Audio(sampling_rate=SR))
44
+ dataset = dataset.filter(text_filter, input_columns=[text_column])
45
+ dataset = dataset.filter(duration_filter, input_columns=[duration_column])
46
+ dataset = dataset.map(text_mapper, batched=False)
47
+
48
+ return dataset
49
+
50
+ train, test = np.split(df, [int(0.9 * len(df))])
51
+ train_dataset = create_dataset(train)
52
+ test_dataset = create_dataset(test)
53
+
54
+ return train_dataset, test_dataset
55
 
56
 
57
  def main():
 
71
 
72
  df = pd.DataFrame(data).sample(frac=1, random_state=666).reset_index(drop=True)
73
 
74
+ train, test = create_datasets(df)
75
 
76
+ full_dataset = DatasetDict({"train": train, "test": test})
 
 
 
77
 
78
+ print(full_dataset)
79
+ # full_dataset.push_to_hub("michaelnetbiz/Kendex")
80
 
81
 
82
  if __name__ == "__main__":