Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add type hints #49

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
add type hints
  • Loading branch information
supersonic1999 committed Apr 17, 2021
commit 96094c695ddeb82cb63f1cd4f8c5c45700000db1
22 changes: 14 additions & 8 deletions src/TRUNAJOD/lexico_semantic_norms.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
We provide two downloadable models of these variables, which come from
:cite:`duchon2013espal` and :cite:`guasch2016spanish`.
"""
from spacy.tokens import Doc
from TRUNAJOD.lexicosemantic_norms_espal import LEXICOSEMANTIC_ESPAL
from TRUNAJOD.lexicosemantic_norms_espal import LSNorm
from TRUNAJOD.utils import lemmatize
Expand All @@ -28,7 +29,12 @@ class LexicoSemanticNorm(object):
:cite:`guasch2016spanish`.
"""

def __init__(self, doc, lexico_semantic_norm_dict, lemmatizer=None):
def __init__(
self,
doc: Doc,
lexico_semantic_norm_dict: dict,
lemmatizer: dict = None,
):
"""Initialize lexico semantic norm object.

Calculate average over number of tokens given a text.
Expand Down Expand Up @@ -102,47 +108,47 @@ def __init__(self, doc, lexico_semantic_norm_dict, lemmatizer=None):
self.__context_avilability /= count
self.__familiarity /= count

def get_arousal(self):
def get_arousal(self) -> float:
"""Get arousal.

:return: Average arousal.
:rtype: float
"""
return self.__arousal

def get_concreteness(self):
def get_concreteness(self) -> float:
"""Get concreteness.

:return: Average concreteness.
:rtype: float
"""
return self.__concreteness

def get_context_availability(self):
def get_context_availability(self) -> float:
"""Get context_availability.

:return: Average context_availability.
:rtype: float
"""
return self.__context_avilability

def get_familiarity(self):
def get_familiarity(self) -> float:
"""Get familiarity.

:return: Average familiarity.
:rtype: float
"""
return self.__familiarity

def get_imageability(self):
def get_imageability(self) -> float:
"""Get imageability.

:return: Average imageability.
:rtype: float
"""
return self.__imageability

def get_valence(self):
def get_valence(self) -> float:
"""Get valence.

:return: Average valence.
Expand All @@ -151,7 +157,7 @@ def get_valence(self):
return self.__valence


def get_conc_imag_familiarity(doc):
def get_conc_imag_familiarity(doc: Doc) -> [float, float, float]:
"""Get lexico-semantic variables.

Computes three lexico-semantic variables: Concreteness, Imageability and
Expand Down
7 changes: 4 additions & 3 deletions src/TRUNAJOD/semantic_measures.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
semantic measurements require word vectors (word embeddings) obtained from
CORPUS semantics.
"""
from spacy.tokens import Doc


def avg_w2v_semantic_similarity(docs, N):
def avg_w2v_semantic_similarity(docs: Doc, N: int) -> float:
"""Compute average semantic similarity between adjacent sentences.

This is using word2vec :cite:`mikolov2013word2vec` model based on SPACY
Expand Down Expand Up @@ -43,7 +44,7 @@ def avg_w2v_semantic_similarity(docs, N):
return avg_sim / float(N - 1)


def get_synsets(lemma, synset_dict):
def get_synsets(lemma: str, synset_dict: dict) -> str:
"""Return synonym set given a word lemma.

The function requires that the synset_dict is passed into it. In our case
Expand All @@ -61,7 +62,7 @@ def get_synsets(lemma, synset_dict):
return synset_dict.get(lemma, {lemma})


def overlap(lemma_list_group, synset_dict):
def overlap(lemma_list_group: list, synset_dict: dict) -> float:
"""Compute average overlap in a text.

Computes semantic synset overlap (synonyms), based on a lemma list group
Expand Down