Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions git_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# THIS FILE IS GENERATED FROM SPACY SETUP.PY
#
GIT_VERSION = "Unknown"
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ types-requests
types-setuptools>=57.0.0
ruff>=0.9.0
cython-lint>=0.15.0
confection>=1.1.0,<2.0.0
confection>=1.3.2,<2.0.0
11 changes: 11 additions & 0 deletions run_test_si.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import spacy

nlp = spacy.blank("si")

text = "එහි අරමුණු කිහිපයකි. ඒවා අතර;"
doc = nlp(text)

print(f"Total tokens: {len(doc)}")
print()
for i, token in enumerate(doc):
print(f"{i+1:>3} {token.text}")
106 changes: 65 additions & 41 deletions spacy/lang/si/lex_attrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,55 @@

_num_words = [
"බින්දුව",
"බිංදුව",
"එක",
"දෙක",
"තුන",
"හතර",
"පහ",
"හය",
"හත",
"අට",
"නවය",
"නමය",
"දහය",
"එකොළහ",
"දොළහ",
"දහතුන",
"දහහතර",
"දාහතර",
"පහළව",
"පහළොව",
"දහසය",
"දහහත",
"දාහත",
"දහඅට",
"දහනවය",
"විස්ස",
"තිහ",
"හතළිහ",
"පනහ",
"හැට",
"හැත්තෑව",
"අසූව",
"අනූව",
"සියය",
"දහස",
"දාහ",
"ලක්ෂය",
"මිලියනය",
"කෝටිය",
"බිලියනය",
"ට්‍රිලියනය",
]
"බිංදුව","එක","දෙක","තුන","හතර","පහ","හය","හත","අට","නවය","නමය","දහය",
"එකොළහ","දොළහ","දහතුන","දහහතර","දාහතර","පහළව","පහළොව","දහසය","දහහත","දාහත","දහඅට","දහනවය",
"විස්ස","තිහ","හතළිහ","පනහ","හැට","හැත්තෑව","අසූව","අනූව","සියය","සියවෙනි"
"දහස","දාහ","ලක්ෂය","මිලියනය","කෝටිය","බිලියනය","ට්‍රිලියනය",
]

_ordinal_words = [
"පළමු", # first
"දෙවන", # second
"තෙවන","තුන්වන", # third
"සතරවන", # fourth
"පස්වන", # fifth
"හයවන", # sixth
"හත්වන", # seventh
"අටවන", # eighth
"නවවන", # ninth
"දහවන", # tenth
"එකොළොස්වන", # eleventh
"දොළොස්වන", # twelfth
"දහතුන්වන", # thirteenth
"දහහතරවන", # fourteenth
"පහලොස්වන", # fifteenth
"දහසයවන", # sixteenth
"දහහත්වන", # seventeenth
"දහඅටවන", # eighteenth
"දහනවවන", # nineteenth
"විසිවන", # twentieth
"තිස්වන", # thirtieth
"හතළිස්වන", # fortieth
"පනස්වන", # fiftieth
"හැටවන", # sixtieth
"හැත්තෑවන", # seventieth
"අසූවන", # eightieth
"අනූවන", # ninetieth
"සියවන", # hundredth
"දහස්වන", # thousandth
"මිලියනවන", # millionth
"බිලියනවන", # billionth
"ට්‍රිලියනවන", # trillionth
"ක්වාඩ්‍රිලියන්වන", # quadrillionth
"ක්වින්ටිලියන්වන", # quintillionth
"සෙක්ස්ටිලියන්වන", # sextillionth
"සෙප්ටිලියන්වන", # septillionth
"ඔක්ටිලියන්වන", # octillionth
"නොනිලියන්වන", # nonillionth
"ඩෙසිලියන්වන", # decillionth
"ගජිලියන්වන", # gajillionth (informal/made-up)
"බසිලියන්වන", # bazillionth (informal/made-up)
]

def like_num(text):
text = text.replace(",", "").replace(".", "")
Expand All @@ -55,7 +62,24 @@ def like_num(text):
return True
if text.lower() in _num_words:
return True
# Sinhala ordinal suffix check — no .lower() needed
if text in _ordinal_words:
return True
# "23 වෙනි" / "100 වන" — with space
parts = text.split()
if len(parts) == 2 and parts[0].isdigit() and parts[1] in ("වන", "වෙනි"):
return True
# "තුන්වන", "සියවන" — suffix attached
if text.endswith("වෙනි"):
stem = text[:-4]
if stem.isdigit() or stem in _num_words:
return True
if text.endswith("වන"):
stem = text[:-2]
if stem.isdigit() or stem in _num_words:
return True
return False



LEX_ATTRS = {LIKE_NUM: like_num}
8 changes: 8 additions & 0 deletions spacy/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,14 @@ def sl_tokenizer():
def sr_tokenizer():
return get_lang_class("sr")().tokenizer

@pytest.fixture(scope="session")
def si_tokenizer():
return get_lang_class("si")().tokenizer


@pytest.fixture(scope="session")
def si_vocab():
return get_lang_class("si")().vocab

@pytest.fixture(scope="session")
def sq_tokenizer():
Expand Down
Empty file added spacy/tests/lang/si/__init__.py
Empty file.
69 changes: 69 additions & 0 deletions spacy/tests/lang/si/test_text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import pytest

from spacy.lang.si.lex_attrs import like_num

# note: this text taken from https://www.bbc.com/sinhala/articles/cgmpy0kpljno
def test_si_tokenizer_handles_long_text(si_tokenizer):
text = """දිනය 2025 නොවැම්බර් 27 වන දා යි.

එදින මහනුවර සිට නාවලපිටිය බලා ධාවනය වන මගී දුම්රිය පස්වරු 2.06ට පමණ මහනුවර දුම්රිය ස්ථානයෙන් තම දුම්රිය ගමන ආරම්භ කර තිබුණේ,
දැඩි වර්ෂාව මධ්‍යයේ ය.මැදිරි හතරකින් සමන්විත මෙම දුම්රිය කෙටිදුර ධාවනයේ නිරත වන මන්දගාමී දුම්රියකි.
එදින මෙම දුම්රිය ගමනාන්තය කරා ගෙන යාමේ කාර්යය භාර වී තිබුණේ, විශේෂ පන්ති දුම්රිය රියැදුරෙකු වු ජයම්පති මඩිගසේකරට ය.

1982 වසරේ දුම්රිය සේවයට එක්වූ ජයම්පති මඩිගසේකර දැනට විශ්‍රාම ගොස් වසර 6කි."""
tokens = si_tokenizer(text)
assert len(tokens) == 83


@pytest.mark.parametrize(
"text,length",
[
("""හැන්දෑවේ 4 - 5ට විතර හොඳට ම වැස්සා මගේ ජීවිතේට ම දැකල නැති වැස්සක්.""", 15),
("ඩොලරයේ අගය ඉහළ ගියේ ඇයි ?", 6),
("මහනුවර සිට නාවලපිටිය", 3),
("ශ්‍රී ලාංකිකයන් බොහෝ දෙනෙකු සිංහල අලුත් අවුරුද්ද සැමරීමට සූදානම්.", 10),
("සිසුන් 110,000කට පමණ වෘත්තීය අධ්‍යාපනය ලබා දීම", 7),
("එහෙමද? මම එහෙම කීවේ නැ!", 7),
("එහි අරමුණු කිහිපයකි. ඒවා අතර;", 7),
],
)
def test_si_tokenizer_handles_cnts(si_tokenizer, text, length):
tokens = si_tokenizer(text)
assert len(tokens) == length


@pytest.mark.parametrize(
"text,match",
[
("10", True),
("1", True),
("10,000", True),
("1,000", True),
("999.0", True),
("එක", True),
("දෙක", True),
("බිලියනය", True),
("බල්ලා", False),
(",", False),
("1/2", True),
("අශේන්", False),
("වැලිගල්ල", False),
],
)
def test_lex_attrs_like_number(si_tokenizer, text, match):
tokens = si_tokenizer(text)
assert len(tokens) == 1
assert tokens[0].like_num == match


@pytest.mark.parametrize(
"word", ["තෙවන", "මිලියනවන", "100වන", "සියවන", "23වෙනි", "52වෙනි"]
)
def test_si_lex_attrs_like_number_for_ordinal(word):
assert like_num(word)


@pytest.mark.parametrize("word", ["එකොළොස්වන"])
def test_si_lex_attrs_capitals(word):
assert like_num(word)
assert like_num(word.upper())
23 changes: 23 additions & 0 deletions spacy/tests/lang/si/test_tokenizer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import pytest

SI_TOKEN_EXCEPTION_TESTS = [
(
"ශ්‍රී ලංකා හමුදාව නිර්භීතව ත්‍රස්තවාදීන් පරාජය කලහ.",
["ශ්‍රී", "ලංකා", "හමුදාව", "නිර්භීතව", "ත්‍රස්තවාදීන්", "පරාජය", "කලහ","."],
),
(
"සමන්, කරුණාකරලා 10වෙනි පිටුව පෙරලලා කියවන්න.",
["සමන්",",", "කරුණාකරලා", "10වෙනි", "පිටුව", "පෙරලලා", "කියවන්න", "."],
),
(
"දෙවන විමලධර්මසූරිය රජුගේ කාලයේ බුද්ධාගම ප්‍රචලිත කලේය.",
["දෙවන", "විමලධර්මසූරිය", "රජුගේ", "කාලයේ", "බුද්ධාගම", "ප්‍රචලිත", "කලේය","."],
),
]


@pytest.mark.parametrize("text,expected_tokens", SI_TOKEN_EXCEPTION_TESTS)
def test_si_tokenizer_handles_exception_cases(si_tokenizer, text, expected_tokens):
tokens = si_tokenizer(text)
token_list = [token.text for token in tokens if not token.is_space]
assert token_list == expected_tokens
Loading