Skip to content
Merged
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
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -429,8 +429,10 @@ python_version = "3.9"
show_column_numbers = true
show_error_code_links = true
show_error_context = true
strict_optional = true
strict_bytes = true
strict_equality = true
strict_equality_for_none = true
strict_optional = true
warn_no_return = true
warn_redundant_casts = true
warn_return_any = true
Expand Down
16 changes: 11 additions & 5 deletions pythainlp/summarize/keybert.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@
[np.array(emb[0]).mean(axis=0) for emb in embs]
).astype(np.float32)

return cast("NDArray[np.float32]", emb_mean)

Check failure on line 167 in pythainlp/summarize/keybert.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Define a constant instead of duplicating this literal "NDArray[np.float32]" 3 times.

See more on https://sonarcloud.io/project/issues?id=PyThaiNLP_pythainlp&issues=AZ0aS361Tb56BCVJYRFx&open=AZ0aS361Tb56BCVJYRFx&pullRequest=1359


def _generate_ngrams(
Expand Down Expand Up @@ -225,25 +225,31 @@
) -> list[tuple[str, float]]:
import numpy as np

def l2_norm(v: np.ndarray) -> np.ndarray:
def l2_norm(v: "NDArray[np.float32]") -> "NDArray[np.float32]":
vec_size = v.shape[1]
result = np.divide(
v,
np.linalg.norm(v, axis=1).reshape(-1, 1).repeat(vec_size, axis=1),
dtype=np.float32,
)
if not np.isclose(np.linalg.norm(result, axis=1), 1).all():
raise ValueError("Cannot normalize a vector to unit vector.")
return result
return cast("NDArray[np.float32]", result)

def cosine_sim(a: np.ndarray, b: np.ndarray) -> np.ndarray:
return (np.matmul(a, b.T).T).sum(axis=1)
def cosine_sim(
a: "NDArray[np.float32]", b: "NDArray[np.float32]"
) -> "NDArray[np.float32]":
# `a` has one row (document embedding), so flatten to get 1-D scores.
scores = np.matmul(a, b.T).reshape(-1)
return cast("NDArray[np.float32]", scores.astype(np.float32, copy=False))

doc_vector = l2_norm(doc_vector)
word_vectors = l2_norm(word_vectors)
cosine_sims = cosine_sim(doc_vector, word_vectors)
ranking_desc = np.argsort(-cosine_sims)

top_indices = cast("list[int]", ranking_desc[:max_keywords].tolist())
final_ranks = [
(keywords[r], cosine_sims[r]) for r in ranking_desc[:max_keywords]
(keywords[idx], float(cosine_sims[idx])) for idx in top_indices
]
return final_ranks
Loading