@@ -29,8 +29,7 @@ def word_freqs() -> list[tuple[str, int]]:
2929 Credit: Korakot Chaovavanich https://www.facebook.com/groups/thainlp/posts/434330506948445
3030 """
3131 freqs : list [tuple [str , int ]] = []
32- lines = list (get_corpus (_UNIGRAM_FILENAME ))
33- for line in lines :
32+ for line in get_corpus (_UNIGRAM_FILENAME ):
3433 word_freq = line .split ("\t " )
3534 if len (word_freq ) >= 2 :
3635 freqs .append ((word_freq [0 ], int (word_freq [1 ])))
@@ -42,9 +41,8 @@ def unigram_word_freqs() -> dict[str, int]:
4241 """Get unigram word frequency from Thai National Corpus (TNC)
4342 """
4443 freqs : dict [str , int ] = defaultdict (int )
45- lines = list (get_corpus (_UNIGRAM_FILENAME ))
46- for i in lines :
47- _temp = i .strip ().split (" " )
44+ for line in get_corpus (_UNIGRAM_FILENAME ):
45+ _temp = line .strip ().split (" " )
4846 if len (_temp ) >= 2 :
4947 freqs [_temp [0 ]] = int (_temp [- 1 ])
5048
@@ -60,10 +58,14 @@ def bigram_word_freqs() -> dict[tuple[str, str], int]:
6058 return freqs
6159 path = str (path )
6260
63- with open (path , encoding = "utf-8-sig" ) as fh :
64- for i in fh .readlines ():
65- temp = i .strip ().split (" " )
66- 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
6769
6870 return freqs
6971
@@ -77,9 +79,13 @@ def trigram_word_freqs() -> dict[tuple[str, str, str], int]:
7779 return freqs
7880 path = str (path )
7981
80- with open (path , encoding = "utf-8-sig" ) as fh :
81- for i in fh .readlines ():
82- temp = i .strip ().split (" " )
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