File size: 2,094 Bytes
6cf191b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
#############################
# Imports
#############################
# Python modules
from abc import ABC, abstractmethod
from typing import Tuple, Optional, List
# Remote modules
from nltk.stem import WordNetLemmatizer
# Local modules
#############################
# Constants
#############################
class KGBaseHandler(ABC):
def __init__(self):
super().__init__()
self.st = WordNetLemmatizer()
def normalize_noun(self, ent):
try:
noun = self.st.lemmatize(ent, pos='n')
noun = self.st.lemmatize(noun, pos='v')
except Exception as _:
noun = ent[:-1] if ent[-1] == 's' else ent
return noun
def normalize_nouns(self, ent):
local_ent = ent[:]
nouns = local_ent.split(' ')
if len(nouns) == 1:
return ' '.join([self.normalize_noun(e) for e in nouns])
return local_ent
def ignore_less_relevant_connection(self, relations):
if len(relations) >= 2:
for r in relations:
if r != 'related_to':
return r
return relations[0]
@abstractmethod
def get_relation_types(self) -> List[str]:
pass
@abstractmethod
def exists_relation_between(self, concept, other_concept) -> bool:
pass
@abstractmethod
def relation_between(self, concept, other_concept) -> Tuple[Optional[str], Optional[str]]:
pass
@abstractmethod
def get_related_concepts(self, concept) -> Optional[List[str]]:
pass
@abstractmethod
def does_concept_exist(self, concept) -> bool:
pass
class NoKnowledge(KGBaseHandler):
def __init__(self):
super(NoKnowledge, self).__init__()
def get_relation_types(self) -> List[str]:
return []
def exists_relation_between(self, concept, other_concept) -> bool:
return False
def relation_between(self, concept, other_concept) -> Tuple[Optional[str], Optional[str]]:
return (None, None)
def does_concept_exist(self, concept) -> bool:
return False
|