Az-r-ow commited on
Commit
4fbdbc2
·
1 Parent(s): 8b58025

test(data_processing): test cases for data processing functions

Browse files
app/travel_resolver/libs/nlp/data_processing.py CHANGED
@@ -3,7 +3,7 @@ import tensorflow as tf
3
  from tqdm import tqdm
4
 
5
 
6
- def get_tagged_content(sentence: str, tag: str) -> str:
7
  """
8
  Extract the content between two tags in a sentence given the tag.
9
 
@@ -12,12 +12,18 @@ def get_tagged_content(sentence: str, tag: str) -> str:
12
  tag (str): The tag to extract the content between.
13
 
14
  Returns:
15
- str: The content between the tags.
 
 
 
16
 
17
  Example:
18
  >>> get_tagged_content("Je voudrais voyager de <Dep>Nice<Dep> à <Arr>Clermont Ferrand<Arr>.", "<Dep>")
19
  "Nice"
20
  """
 
 
 
21
  tag_match = re.search(rf"{tag}(.*?){tag}", sentence)
22
  if tag_match:
23
  return tag_match.group(1)
@@ -144,6 +150,9 @@ def from_tagged_file_to_bio_file(
144
  with open(output_file, "w") as file:
145
  sentences = content.split("\n")
146
  for sentence in tqdm(sentences):
 
 
 
147
  bio_format = convert_tagged_sentence_to_bio(sentence, tag_entities_pairs)
148
  file.write(bio_format + "\n")
149
 
 
3
  from tqdm import tqdm
4
 
5
 
6
+ def get_tagged_content(sentence: str, tag: str) -> str | None:
7
  """
8
  Extract the content between two tags in a sentence given the tag.
9
 
 
12
  tag (str): The tag to extract the content between.
13
 
14
  Returns:
15
+ str | None: The content between the tags. None if not found
16
+
17
+ Raises:
18
+ ValueError: If tag is not provided or tag not str.
19
 
20
  Example:
21
  >>> get_tagged_content("Je voudrais voyager de <Dep>Nice<Dep> à <Arr>Clermont Ferrand<Arr>.", "<Dep>")
22
  "Nice"
23
  """
24
+ if not tag or not isinstance(tag, str):
25
+ raise ValueError("tag must be a non-empty string")
26
+
27
  tag_match = re.search(rf"{tag}(.*?){tag}", sentence)
28
  if tag_match:
29
  return tag_match.group(1)
 
150
  with open(output_file, "w") as file:
151
  sentences = content.split("\n")
152
  for sentence in tqdm(sentences):
153
+ # skip empty lines
154
+ if not sentence:
155
+ continue
156
  bio_format = convert_tagged_sentence_to_bio(sentence, tag_entities_pairs)
157
  file.write(bio_format + "\n")
158
 
app/travel_resolver/tests/data_processing_test.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import unittest
2
+ from pathlib import Path
3
+ from travel_resolver.libs.nlp.data_processing import (
4
+ get_tagged_content,
5
+ process_sentence,
6
+ convert_tagged_sentence_to_bio,
7
+ from_tagged_file_to_bio_file,
8
+ from_bio_file_to_examples,
9
+ )
10
+
11
+ bio_format_sentence = """Je O
12
+ voudrais O
13
+ voyager O
14
+ de O
15
+ Nice B-LOC-DEP
16
+ à O
17
+ Clermont B-LOC-ARR
18
+ Ferrand I-LOC-ARR
19
+ . O"""
20
+
21
+
22
+ class TestDataProcessing(unittest.TestCase):
23
+ script_dir = Path(__file__).parent
24
+ test_samples_dir = f"{script_dir}/test_samples"
25
+
26
+ def test_get_tagged_content(self):
27
+ simple_city = "Paris"
28
+ input_simple = f"I am going to <Dep>{simple_city}<Dep>"
29
+ result_simple = get_tagged_content(input_simple, "<Dep>")
30
+ self.assertEqual(result_simple, simple_city)
31
+
32
+ composed_city = "Los angeles"
33
+ input_complex = f"I am going to <Dep>{composed_city}<Dep> and <Arr>London<Arr> <Dep>Paris<Dep>"
34
+ result_complex = get_tagged_content(input_complex, "<Dep>")
35
+
36
+ self.assertEqual(result_complex, composed_city)
37
+
38
+ input_empty = "I am going to Paris"
39
+ result_empty = get_tagged_content(input_empty, "<Dep>")
40
+
41
+ self.assertEqual(result_empty, None)
42
+
43
+ with self.assertRaises(ValueError):
44
+ get_tagged_content(input_simple, "")
45
+
46
+ with self.assertRaises(ValueError):
47
+ get_tagged_content(input_simple, 123)
48
+
49
+ def test_convert_tagged_sentence_to_bio(self):
50
+ sentence = "Je voudrais voyager de <Dep>Nice<Dep> à <Arr>Clermont Ferrand<Arr>."
51
+ tag_entities_pairs = [("<Dep>", "LOC-DEP"), ("<Arr>", "LOC-ARR")]
52
+
53
+ result = convert_tagged_sentence_to_bio(sentence, tag_entities_pairs)
54
+
55
+ self.assertEqual(result, bio_format_sentence)
56
+
57
+ def test_from_tagged_file_to_bio(self):
58
+ simple_input_file = f"{self.test_samples_dir}/simple_tagged_sentence.txt"
59
+ expected_file = f"{self.test_samples_dir}/simple_tagged_sentence.bio"
60
+ output_file = f"{self.test_samples_dir}/output.bio"
61
+ tag_entities_pairs = [("<Dep>", "LOC-DEP"), ("<Arr>", "LOC-ARR")]
62
+
63
+ from_tagged_file_to_bio_file(simple_input_file, output_file, tag_entities_pairs)
64
+
65
+ with open(output_file, "r") as f:
66
+ result = f.read()
67
+
68
+ with open(expected_file, "r") as f:
69
+ expected = f.read()
70
+
71
+ self.assertEqual(result, expected)
72
+
73
+ multiple_sentences__file = (
74
+ f"{self.test_samples_dir}/multiple_tagged_sentences.txt"
75
+ )
76
+ expected_file = f"{self.test_samples_dir}/multiple_tagged_sentences.bio"
77
+
78
+ from_tagged_file_to_bio_file(
79
+ multiple_sentences__file, output_file, tag_entities_pairs
80
+ )
81
+
82
+ with open(expected_file, "r") as f:
83
+ expected = f.read()
84
+
85
+ with open(output_file, "r") as f:
86
+ result = f.read()
87
+
88
+ self.assertEqual(result, expected)
89
+
90
+ def test_from_bio_file_to_examples(self):
91
+ bio_file = f"{self.test_samples_dir}/multiple_tagged_sentences.bio"
92
+ text_file = f"{self.test_samples_dir}/multiple_tagged_sentences.txt"
93
+
94
+ examples = from_bio_file_to_examples(bio_file)
95
+
96
+ self.assertEqual(len(examples), 2)
97
+
98
+ with open(text_file, "r") as f:
99
+ content = f.read()
100
+ lines = content.split("\n")
101
+
102
+ # The number of inputs must be equal to the number of lines before parsing
103
+ self.assertEqual(len(examples[0]), len(lines))
104
+ self.assertEqual(len(examples[1]), len(lines))
app/travel_resolver/tests/test_samples/multiple_tagged_sentences.bio ADDED
@@ -0,0 +1,279 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Pouvez-vous O
2
+ me O
3
+ donner O
4
+ des O
5
+ informations O
6
+ sur O
7
+ un O
8
+ vol O
9
+ de O
10
+ MAILLY-LE-CAMP B-LOC-DEP
11
+ à O
12
+ TURCKHEIM B-LOC-ARR
13
+ ? O
14
+ J'aimerais O
15
+ aller O
16
+ de O
17
+ MAULE B-LOC-DEP
18
+ jusqu O
19
+ ' O
20
+ à O
21
+ VINAY B-LOC-ARR
22
+ . O
23
+ Je O
24
+ souhaiterais O
25
+ trouver O
26
+ un O
27
+ itinéraire O
28
+ pour O
29
+ aller O
30
+ de O
31
+ MIGNALOUX-BEAUVOIR B-LOC-DEP
32
+ à O
33
+ BAZOCHES-ET-SAINT-THIBAUT B-LOC-ARR
34
+ . O
35
+ Donne-moi O
36
+ un O
37
+ itinéraire O
38
+ de O
39
+ PONTRIEUX B-LOC-DEP
40
+ jusqu O
41
+ ' O
42
+ à O
43
+ LACQ B-LOC-ARR
44
+ . O
45
+ J'aimerais O
46
+ faire O
47
+ un O
48
+ voyage O
49
+ de O
50
+ MOULINS-SUR-YEVRE B-LOC-DEP
51
+ à O
52
+ TARASCON-SUR-ARIEGE B-LOC-ARR
53
+ en O
54
+ utilisant O
55
+ un O
56
+ service O
57
+ de O
58
+ transport O
59
+ en O
60
+ commun O
61
+ . O
62
+ J'ai O
63
+ besoin O
64
+ de O
65
+ directions O
66
+ pour O
67
+ aller O
68
+ à O
69
+ CASTRES B-LOC-ARR
70
+ depuis O
71
+ ARNAGE B-LOC-DEP
72
+ en O
73
+ utilisant O
74
+ les O
75
+ transports O
76
+ en O
77
+ commun O
78
+ . O
79
+ Je O
80
+ voudrais O
81
+ trouver O
82
+ un O
83
+ vol O
84
+ de O
85
+ ROINVILLE B-LOC-DEP
86
+ vers O
87
+ SESSENHEIM B-LOC-ARR
88
+ avec O
89
+ une O
90
+ compagnie O
91
+ aérienne O
92
+ de O
93
+ ballons O
94
+ à O
95
+ air O
96
+ chaud O
97
+ . O
98
+ Comment O
99
+ aller O
100
+ de O
101
+ CASTELNAU-D'ESTRETEFONDS B-LOC-DEP
102
+ à O
103
+ CIRY-SALSOGNE B-LOC-ARR
104
+ ? O
105
+ Quels O
106
+ sont O
107
+ les O
108
+ meilleurs O
109
+ itinéraires O
110
+ pour O
111
+ aller O
112
+ à O
113
+ ESCLANEDES B-LOC-ARR
114
+ depuis O
115
+ EOLE-EN-BEAUCE B-LOC-DEP
116
+ ? O
117
+ Veuillez O
118
+ me O
119
+ fournir O
120
+ un O
121
+ itinéraire O
122
+ pour O
123
+ me O
124
+ rendre O
125
+ de O
126
+ CINTEGABELLE B-LOC-DEP
127
+ à O
128
+ BEAUTOT B-LOC-ARR
129
+ en O
130
+ utilisant O
131
+ les O
132
+ transports O
133
+ en O
134
+ commun O
135
+ . O
136
+ J'ai O
137
+ besoin O
138
+ d'un O
139
+ itinéraire O
140
+ qui O
141
+ me O
142
+ permette O
143
+ de O
144
+ me O
145
+ rendre O
146
+ de O
147
+ SAINT-BRIEUC B-LOC-DEP
148
+ vers O
149
+ COLLONGES B-LOC-ARR
150
+ . O
151
+ Comment O
152
+ puis-je O
153
+ aller O
154
+ de O
155
+ RAMBOUILLET B-LOC-DEP
156
+ vers O
157
+ MAZEYRAT-D'ALLIER B-LOC-ARR
158
+ en O
159
+ transports O
160
+ en O
161
+ commun O
162
+ ? O
163
+ Je O
164
+ souhaiterais O
165
+ me O
166
+ rendre O
167
+ à O
168
+ MONT B-LOC-ARR
169
+ LOZERE I-LOC-ARR
170
+ ET I-LOC-ARR
171
+ GOULET I-LOC-ARR
172
+ , O
173
+ partant O
174
+ de O
175
+ JUZIERS B-LOC-DEP
176
+ . O
177
+ J'ai O
178
+ besoin O
179
+ d'un O
180
+ itinéraire O
181
+ qui O
182
+ me O
183
+ permette O
184
+ de O
185
+ me O
186
+ rendre O
187
+ de O
188
+ AZAY-LE-RIDEAU B-LOC-DEP
189
+ vers O
190
+ SALON-DE-PROVENCE B-LOC-ARR
191
+ . O
192
+ Quels O
193
+ sont O
194
+ les O
195
+ meilleurs O
196
+ itinéraires O
197
+ pour O
198
+ aller O
199
+ à O
200
+ BUCHERES B-LOC-ARR
201
+ depuis O
202
+ SAINT-PIERRE-D'AURILLAC B-LOC-DEP
203
+ ? O
204
+ J'aimerais O
205
+ faire O
206
+ un O
207
+ voyage O
208
+ de O
209
+ SAVERDUN B-LOC-DEP
210
+ à O
211
+ VINDRAC-ALAYRAC B-LOC-ARR
212
+ en O
213
+ prenant O
214
+ le O
215
+ métro O
216
+ . O
217
+ Quelle O
218
+ est O
219
+ la O
220
+ meilleure O
221
+ route O
222
+ pour O
223
+ aller O
224
+ à O
225
+ GAZERAN B-LOC-ARR
226
+ en O
227
+ partant O
228
+ de O
229
+ L'ETANG-LA-VILLE B-LOC-DEP
230
+ ? O
231
+ J'aimerais O
232
+ aller O
233
+ de O
234
+ ALENCON B-LOC-DEP
235
+ jusqu O
236
+ ' O
237
+ à O
238
+ FRANXAULT B-LOC-ARR
239
+ . O
240
+ Je O
241
+ voudrais O
242
+ trouver O
243
+ un O
244
+ vol O
245
+ de O
246
+ LIART B-LOC-DEP
247
+ vers O
248
+ CHANTILLY B-LOC-ARR
249
+ au O
250
+ meilleur O
251
+ prix O
252
+ possible O
253
+ . O
254
+ Quel O
255
+ est O
256
+ le O
257
+ trajet O
258
+ le O
259
+ plus O
260
+ rapide O
261
+ pour O
262
+ aller O
263
+ de O
264
+ SAINT-LO B-LOC-DEP
265
+ à O
266
+ PARIS B-LOC-ARR
267
+ ? O
268
+ Je O
269
+ souhaite O
270
+ prendre O
271
+ un O
272
+ taxi O
273
+ de O
274
+ LUNEL-VIEL B-LOC-DEP
275
+ pour O
276
+ aller O
277
+ à O
278
+ MESSEIN B-LOC-ARR
279
+ . O
app/travel_resolver/tests/test_samples/multiple_tagged_sentences.txt ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Pouvez-vous me donner des informations sur un vol de <Dep>MAILLY-LE-CAMP<Dep> à <Arr>TURCKHEIM<Arr> ?
2
+ J'aimerais aller de <Dep>MAULE<Dep> jusqu'à <Arr>VINAY<Arr>.
3
+ Je souhaiterais trouver un itinéraire pour aller de <Dep>MIGNALOUX-BEAUVOIR<Dep> à <Arr>BAZOCHES-ET-SAINT-THIBAUT<Arr>.
4
+ Donne-moi un itinéraire de <Dep>PONTRIEUX<Dep> jusqu'à <Arr>LACQ<Arr>.
5
+ J'aimerais faire un voyage de <Dep>MOULINS-SUR-YEVRE<Dep> à <Arr>TARASCON-SUR-ARIEGE<Arr> en utilisant un service de transport en commun.
6
+ J'ai besoin de directions pour aller à <Arr>CASTRES<Arr> depuis <Dep>ARNAGE<Dep> en utilisant les transports en commun.
7
+ Je voudrais trouver un vol de <Dep>ROINVILLE<Dep> vers <Arr>SESSENHEIM<Arr> avec une compagnie aérienne de ballons à air chaud.
8
+ Comment aller de <Dep>CASTELNAU-D'ESTRETEFONDS<Dep> à <Arr>CIRY-SALSOGNE<Arr> ?
9
+ Quels sont les meilleurs itinéraires pour aller à <Arr>ESCLANEDES<Arr> depuis <Dep>EOLE-EN-BEAUCE<Dep> ?Veuillez me fournir un itinéraire pour me rendre de <Dep>CINTEGABELLE<Dep> à <Arr>BEAUTOT<Arr> en utilisant les transports en commun.
10
+ J'ai besoin d'un itinéraire qui me permette de me rendre de <Dep>SAINT-BRIEUC<Dep> vers <Arr>COLLONGES<Arr>.
11
+ Comment puis-je aller de <Dep>RAMBOUILLET<Dep> vers <Arr>MAZEYRAT-D'ALLIER<Arr> en transports en commun ?
12
+ Je souhaiterais me rendre à <Arr>MONT LOZERE ET GOULET<Arr>, partant de <Dep>JUZIERS<Dep>.
13
+ J'ai besoin d'un itinéraire qui me permette de me rendre de <Dep>AZAY-LE-RIDEAU<Dep> vers <Arr>SALON-DE-PROVENCE<Arr>.
14
+ Quels sont les meilleurs itinéraires pour aller à <Arr>BUCHERES<Arr> depuis <Dep>SAINT-PIERRE-D'AURILLAC<Dep> ?
15
+ J'aimerais faire un voyage de <Dep>SAVERDUN<Dep> à <Arr>VINDRAC-ALAYRAC<Arr> en prenant le métro.
16
+ Quelle est la meilleure route pour aller à <Arr>GAZERAN<Arr> en partant de <Dep>L'ETANG-LA-VILLE<Dep> ?
17
+ J'aimerais aller de <Dep>ALENCON<Dep> jusqu'à <Arr>FRANXAULT<Arr>.
18
+ Je voudrais trouver un vol de <Dep>LIART<Dep> vers <Arr>CHANTILLY<Arr> au meilleur prix possible.
19
+ Quel est le trajet le plus rapide pour aller de <Dep>SAINT-LO<Dep> à <Arr>PARIS<Arr> ?
20
+ Je souhaite prendre un taxi de <Dep>LUNEL-VIEL<Dep> pour aller à <Arr>MESSEIN<Arr>.
app/travel_resolver/tests/test_samples/simple_tagged_sentence.bio ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ Je O
2
+ voudrais O
3
+ voyager O
4
+ de O
5
+ Nice B-LOC-DEP
6
+ à O
7
+ Clermont B-LOC-ARR
8
+ Ferrand I-LOC-ARR
9
+ . O