Add dirty code for working with the new ontology

This commit is contained in:
Aleksey Filippov 2023-06-02 17:41:05 +04:00
parent 9d6691f230
commit 50e36be6d0
3 changed files with 50 additions and 36 deletions

View File

@ -10,7 +10,7 @@ from src.syntax import Syntax
def _main(wav_file: str): def _main(wav_file: str):
# text: str = Speech().run_recognition(wav_file) # text: str = Speech().run_recognition(wav_file)
text: str = 'Можно ли рефинансировать ипотеку?' text: str = 'Как получить деньги с заблокированной карты?'
print(f'Text: {text}') print(f'Text: {text}')
parse_tree: ParseTree = Syntax().get_parse_tree(text) parse_tree: ParseTree = Syntax().get_parse_tree(text)
print(f'Parse tree:\n{parse_tree}') print(f'Parse tree:\n{parse_tree}')

View File

@ -1,45 +1,58 @@
from typing import List, Dict from typing import List
from ordered_set import OrderedSet
from owlready2 import get_ontology, Ontology from owlready2 import get_ontology, Ontology
class MyOntology: class MyOntology:
def __init__(self) -> None: def __init__(self) -> None:
self.__onto: Ontology = get_ontology("file://./ontology.owl").load() self.__onto: Ontology = get_ontology("file://./new-ontology.owx").load()
def __find_str_in_list(self, string: str, string_list: List[str]) -> int: def __get_parent_with_hierarchy(self, root, parents):
try: pdict = {}
string_list.index(string.replace("_", " ")) for parent in parents:
return 1 level = 0
except ValueError: subclass = parent.is_a[0]
return 0 if subclass == self.__onto.Concept and len(pdict.keys()) == 0:
pdict[1] = [parent]
while subclass != self.__onto.Concept:
level += 1
if subclass == root:
if pdict.get(level) is None:
pdict[level] = []
pdict[level].append(parent)
subclass = subclass.is_a[0]
keys = sorted(pdict.keys())
if len(keys) == 0:
return None, None
return keys[-1], pdict[keys[-1]]
def __get_property_value(self, values: [], instance): def __find_instance(self, root, level: int, terms: List[str]) -> OrderedSet[(int, [])]:
if len(values) != 1: level += 1
raise ValueError(f'Wrong values in {instance.name}') instances: OrderedSet[(int, [])] = OrderedSet()
return values[0] for current_class in root.subclasses():
for instance in current_class.instances():
def __get_event_instance(self, instances: []): if instance.name == terms[-1] is not None:
events: List[Dict[int, str]] = [] plevel, parents = self.__get_parent_with_hierarchy(current_class, instance.is_instance_of)
for instance in instances: filtered_terms = list(filter(lambda term: term != terms[-1], terms))
event = self.__get_property_value(instance.hasEvent, instance) if parents is None:
priority = self.__get_property_value(event.hasPriority, event) plevel = level
events.append({priority: self.__get_property_value(event.hasDescription, event)}) parents = [current_class]
events.sort(key=lambda item: list(item.keys())[0], reverse=True) if len(filtered_terms) == 0:
return events instances.append((plevel, current_class))
return instances
def __find_instances_by_terms(self, my_terms: List[str]) -> []: for parent in parents:
instances = [] result = self.__find_instance(parent, plevel, filtered_terms)
for instance in self.__onto.Concept.instances(): if len(result) == 0:
terms = instance.hasTerm instances.append((plevel, parent))
match: int = 0 else:
for term in terms: [instances.append(item) for item in result]
match = match + self.__find_str_in_list(term.name, my_terms)
if match >= 1:
instances.append(instance)
return instances return instances
def get_event_description(self, terms: List[str]) -> str: def get_event_description(self, terms: List[str]) -> str:
instances: [] = self.__find_instances_by_terms(terms) instances = OrderedSet()
events = self.__get_event_instance(instances) for term in terms:
return '\n'.join(list(map(lambda item: f'{list(item.keys())[0]}: {list(item.values())[0]}', events))) for item in self.__find_instance(self.__onto.Concept, 0, term.split(' ')):
instances.append(item)
instances = sorted(list(instances), reverse=True)
return '\n'.join(list(map(lambda instance: ' '.join(instance[1].hasDescription), instances)))

View File

@ -109,7 +109,8 @@ class NLP:
for child in split: for child in split:
self.__tree.add_to_tree(child, term_type, parent_node) self.__tree.add_to_tree(child, term_type, parent_node)
def __merge_terms(self, leaves: List[List[SemanticTreeNode]]) -> List[str]: @staticmethod
def __merge_terms(leaves: List[List[SemanticTreeNode]]) -> List[str]:
terms: List[str] = [] terms: List[str] = []
for group in leaves: for group in leaves:
if len(group) == 0: if len(group) == 0: