Skip to content

Commit 084e6d2

Browse files
committed
Bugfix: Force the part of speech tag for "in" to be NN if the token follows a number. In these cases, the token is the abbreviation for inches rather than a preposition (which it would otherwise be tagged as). Add tests for the dimensional phrase structural feature.
1 parent d71216f commit 084e6d2

3 files changed

Lines changed: 47 additions & 1 deletion

File tree

ingredient_parser/en/_structure_features.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,9 @@ def detect_dimensional_phrases(
329329

330330
text_pos = []
331331
for t in tokenized_sentence:
332-
if t.text.lower() in LENGTH_UNITS:
332+
if t.text.lower() in LENGTH_UNITS and t.pos_tag != "IN":
333+
# We need to check the POS tag so we don't confuse "in" (preposition)
334+
# with "in" (abbreviation of inch).
333335
pos = "LEN"
334336
elif t.text.lower() in DIMENSIONS:
335337
pos = "DIM"

ingredient_parser/en/preprocess.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,11 @@ def _calculate_tokens(self, sentence: str) -> list[Token]:
581581
elif text.lower() == "/":
582582
# Force "/" to have SYM tag.
583583
pos = "SYM"
584+
elif text == "in" and i > 0 and tokens[i - 1].feat_text == "!num":
585+
# If the text is "in" and the previous token is a number then this is
586+
# mostly likely to be the abbreviation of inches rather than the
587+
# preposition.
588+
pos = "NN"
584589

585590
features = TokenFeatures(
586591
stem=stem(feat_text),

tests/preprocess/test_sentence_structure_features.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,42 @@ def test_example_phrase_detection_feature(self):
195195
assert token_features.get("example_phrase", False)
196196
else:
197197
assert not token_features.get("example_phrase", False)
198+
199+
200+
class Test_dimensional_phrase_features:
201+
def test_dimensional_phrase_detection(self):
202+
"""
203+
Test dimensional phrase comprising number-unit-dimension is detected.
204+
"""
205+
p = PreProcessor("1 2 in thick piece of steak", custom_units={})
206+
assert p.sentence_structure.dimensional_phrases == [[1, 2, 3]]
207+
208+
def test_dimensional_phrase_no_dimension(self):
209+
"""
210+
Test dimensional phrase comprising number-unit is detected.
211+
"""
212+
p = PreProcessor("2in/5cm piece of ginger", custom_units={})
213+
assert p.sentence_structure.dimensional_phrases == [[0, 1, 2, 3, 4]]
214+
215+
def test_dimensional_phrase_with_parenthesis(self):
216+
"""
217+
Test dimensional phrase comprising two pair of number-unit, with the second pair
218+
in parentheses, followed by a dimension is detected.
219+
"""
220+
p = PreProcessor("1 2 in (5 cm) long piece of steak", custom_units={})
221+
assert p.sentence_structure.dimensional_phrases == [[1, 2, 3, 4, 5, 6, 7]]
222+
223+
def test_dimensional_phrase_with_slash(self):
224+
"""
225+
Test dimensional phrase comprising two pair of number-unit, with the second pair
226+
after a forward slash, followed by a dimension is detected.
227+
"""
228+
p = PreProcessor("1 2 in / 5 cm wide piece of steak", custom_units={})
229+
assert p.sentence_structure.dimensional_phrases == [[1, 2, 3, 4, 5, 6]]
230+
231+
def test_dimensional_phrase_with_preposition(self):
232+
"""
233+
Test dimensional phrase comprising number-unit-"in"-dimension is detected.
234+
"""
235+
p = PreProcessor("1 potato, 3 inches in diameter", custom_units={})
236+
assert p.sentence_structure.dimensional_phrases == [[3, 4, 5, 6]]

0 commit comments

Comments
 (0)