Skip to content

Commit 44d9765

Browse files
Copilotbact
andcommitted
Fix AttributeError: import Synset/Lemma from nltk.corpus.reader.wordnet directly
Co-authored-by: bact <128572+bact@users.noreply.github.com> Agent-Logs-Url: https://github.com/PyThaiNLP/pythainlp/sessions/43afb162-b51d-4003-b3c2-1c93a6fba13b
1 parent 6311614 commit 44d9765

2 files changed

Lines changed: 17 additions & 25 deletions

File tree

pythainlp/corpus/wordnet.py

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@
3030
nltk.download("wordnet")
3131

3232
from nltk.corpus import wordnet
33+
from nltk.corpus.reader.wordnet import Lemma, Synset
3334

3435

3536
def synsets(
3637
word: str, pos: Optional[str] = None, lang: str = "tha"
37-
) -> list[wordnet.Synset]:
38+
) -> list[Synset]:
3839
"""This function returns the synonym set for all lemmas of the given word
3940
with an optional argument to constrain the part of speech of the word.
4041
@@ -78,12 +79,10 @@ def synsets(
7879
>>> synsets("แรง", pos="a", lang="tha")
7980
[Synset('hard.s.10'), Synset('strong.s.02')]
8081
"""
81-
return cast(
82-
list[wordnet.Synset], wordnet.synsets(lemma=word, pos=pos, lang=lang)
83-
)
82+
return cast(list[Synset], wordnet.synsets(lemma=word, pos=pos, lang=lang))
8483

8584

86-
def synset(name_synsets: str) -> wordnet.Synset:
85+
def synset(name_synsets: str) -> Synset:
8786
"""This function returns the synonym set (synset) given the name of the synset
8887
(i.e. 'dog.n.01', 'chase.v.01').
8988
@@ -149,7 +148,7 @@ def all_lemma_names(pos: Optional[str] = None, lang: str = "tha") -> list[str]:
149148
return cast(list[str], wordnet.all_lemma_names(pos=pos, lang=lang))
150149

151150

152-
def all_synsets(pos: Optional[str] = None) -> Iterable[wordnet.Synset]:
151+
def all_synsets(pos: Optional[str] = None) -> Iterable[Synset]:
153152
"""This function iterates over all synsets constrained by the given
154153
part of speech tag.
155154
@@ -176,7 +175,7 @@ def all_synsets(pos: Optional[str] = None) -> Iterable[wordnet.Synset]:
176175
>>> next(generator)
177176
Synset('unable.a.01')
178177
"""
179-
return cast(Iterable[wordnet.Synset], wordnet.all_synsets(pos=pos))
178+
return cast(Iterable[Synset], wordnet.all_synsets(pos=pos))
180179

181180

182181
def langs() -> list[str]:
@@ -199,7 +198,7 @@ def langs() -> list[str]:
199198

200199
def lemmas(
201200
word: str, pos: Optional[str] = None, lang: str = "tha"
202-
) -> list[wordnet.Lemma]:
201+
) -> list[Lemma]:
203202
"""This function returns all lemmas given the word with an optional
204203
argument to constrain the part of speech of the word.
205204
@@ -239,10 +238,10 @@ def lemmas(
239238
>>> lemmas("ม้วน", pos="n")
240239
[Lemma('roll.n.11.ม้วน')]
241240
"""
242-
return cast(list[wordnet.Lemma], wordnet.lemmas(word, pos=pos, lang=lang))
241+
return cast(list[Lemma], wordnet.lemmas(word, pos=pos, lang=lang))
243242

244243

245-
def lemma(name_synsets: str) -> wordnet.Lemma:
244+
def lemma(name_synsets: str) -> Lemma:
246245
"""This function returns lemma object given the name.
247246
248247
.. note::
@@ -269,7 +268,7 @@ def lemma(name_synsets: str) -> wordnet.Lemma:
269268
return wordnet.lemma(name_synsets)
270269

271270

272-
def lemma_from_key(key: str) -> wordnet.Lemma:
271+
def lemma_from_key(key: str) -> Lemma:
273272
"""This function returns lemma object given the lemma key.
274273
This is similar to :func:`lemma` but it needs to be given the key
275274
of lemma instead of the name of lemma.
@@ -295,9 +294,7 @@ def lemma_from_key(key: str) -> wordnet.Lemma:
295294
return wordnet.lemma_from_key(key)
296295

297296

298-
def path_similarity(
299-
synsets1: wordnet.Synset, synsets2: wordnet.Synset
300-
) -> float:
297+
def path_similarity(synsets1: Synset, synsets2: Synset) -> float:
301298
"""This function returns similarity between two synsets based on the
302299
shortest path distance calculated using the equation below.
303300
@@ -336,9 +333,7 @@ def path_similarity(
336333
return cast(float, wordnet.path_similarity(synsets1, synsets2))
337334

338335

339-
def lch_similarity(
340-
synsets1: wordnet.Synset, synsets2: wordnet.Synset
341-
) -> float:
336+
def lch_similarity(synsets1: Synset, synsets2: Synset) -> float:
342337
"""This function returns Leacock Chodorow similarity (LCH)
343338
between two synsets, based on the shortest path distance
344339
and the maximum depth of the taxonomy. The equation to
@@ -375,9 +370,7 @@ def lch_similarity(
375370
return cast(float, wordnet.lch_similarity(synsets1, synsets2))
376371

377372

378-
def wup_similarity(
379-
synsets1: wordnet.Synset, synsets2: wordnet.Synset
380-
) -> float:
373+
def wup_similarity(synsets1: Synset, synsets2: Synset) -> float:
381374
"""This function returns Wu-Palmer similarity (WUP) between two synsets,
382375
based on the depth of the two senses in the taxonomy and their
383376
Least Common Subsumer (most specific ancestor node).

pythainlp/summarize/freq.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ def __init__(self, min_cut: float = 0.1, max_cut: float = 0.9) -> None:
3131
def __rank(ranking: dict[int, float], n: int) -> list[int]:
3232
return nlargest(n, ranking, key=lambda idx: ranking[idx])
3333

34-
def __compute_frequencies(self, word_tokenized_sents: list[list[str]]) -> dict[str, float]:
34+
def __compute_frequencies(
35+
self, word_tokenized_sents: list[list[str]]
36+
) -> dict[str, float]:
3537
counts: Counter[str] = Counter()
3638
for sent in word_tokenized_sents:
3739
for word in sent:
@@ -42,10 +44,7 @@ def __compute_frequencies(self, word_tokenized_sents: list[list[str]]) -> dict[s
4244
return {}
4345

4446
max_freq = float(max(counts.values()))
45-
freqs = {
46-
w: (c / max_freq)
47-
for w, c in counts.items()
48-
}
47+
freqs = {w: (c / max_freq) for w, c in counts.items()}
4948
return {
5049
w: f
5150
for w, f in freqs.items()

0 commit comments

Comments
 (0)