Skip to content

Commit 08092b2

Browse files
Copilotbact
andcommitted
Fix ResourceWarning: add exception handling for file operations in tnc.py
Co-authored-by: bact <128572+bact@users.noreply.github.com>
1 parent 1b004ac commit 08092b2

1 file changed

Lines changed: 16 additions & 10 deletions

File tree

pythainlp/corpus/tnc.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,14 @@ def bigram_word_freqs() -> dict[tuple[str, str], int]:
5858
return freqs
5959
path = str(path)
6060

61-
with open(path, encoding="utf-8-sig") as fh:
62-
for line in fh:
63-
temp = line.strip().split(" ")
64-
if len(temp) >= 3:
65-
freqs[(temp[0], temp[1])] = int(temp[-1])
61+
try:
62+
with open(path, encoding="utf-8-sig") as fh:
63+
for line in fh:
64+
temp = line.strip().split(" ")
65+
if len(temp) >= 3:
66+
freqs[(temp[0], temp[1])] = int(temp[-1])
67+
except (IOError, OSError):
68+
pass
6669

6770
return freqs
6871

@@ -76,10 +79,13 @@ def trigram_word_freqs() -> dict[tuple[str, str, str], int]:
7679
return freqs
7780
path = str(path)
7881

79-
with open(path, encoding="utf-8-sig") as fh:
80-
for line in fh:
81-
temp = line.strip().split(" ")
82-
if len(temp) >= 4:
83-
freqs[(temp[0], temp[1], temp[2])] = int(temp[-1])
82+
try:
83+
with open(path, encoding="utf-8-sig") as fh:
84+
for line in fh:
85+
temp = line.strip().split(" ")
86+
if len(temp) >= 4:
87+
freqs[(temp[0], temp[1], temp[2])] = int(temp[-1])
88+
except (IOError, OSError):
89+
pass
8490

8591
return freqs

0 commit comments

Comments
 (0)