personal_knowledge_library 3.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of personal_knowledge_library might be problematic. Click here for more details.

Files changed (42) hide show
  1. knowledge/__init__.py +91 -0
  2. knowledge/base/__init__.py +22 -0
  3. knowledge/base/access.py +167 -0
  4. knowledge/base/entity.py +267 -0
  5. knowledge/base/language.py +27 -0
  6. knowledge/base/ontology.py +2734 -0
  7. knowledge/base/search.py +473 -0
  8. knowledge/base/tenant.py +192 -0
  9. knowledge/nel/__init__.py +11 -0
  10. knowledge/nel/base.py +495 -0
  11. knowledge/nel/engine.py +123 -0
  12. knowledge/ontomapping/__init__.py +667 -0
  13. knowledge/ontomapping/manager.py +320 -0
  14. knowledge/public/__init__.py +27 -0
  15. knowledge/public/cache.py +115 -0
  16. knowledge/public/helper.py +373 -0
  17. knowledge/public/relations.py +128 -0
  18. knowledge/public/wikidata.py +1324 -0
  19. knowledge/services/__init__.py +128 -0
  20. knowledge/services/asyncio/__init__.py +7 -0
  21. knowledge/services/asyncio/base.py +458 -0
  22. knowledge/services/asyncio/graph.py +1420 -0
  23. knowledge/services/asyncio/group.py +450 -0
  24. knowledge/services/asyncio/search.py +439 -0
  25. knowledge/services/asyncio/users.py +270 -0
  26. knowledge/services/base.py +533 -0
  27. knowledge/services/graph.py +1897 -0
  28. knowledge/services/group.py +819 -0
  29. knowledge/services/helper.py +142 -0
  30. knowledge/services/ontology.py +1234 -0
  31. knowledge/services/search.py +488 -0
  32. knowledge/services/session.py +444 -0
  33. knowledge/services/tenant.py +281 -0
  34. knowledge/services/users.py +445 -0
  35. knowledge/utils/__init__.py +10 -0
  36. knowledge/utils/graph.py +417 -0
  37. knowledge/utils/wikidata.py +197 -0
  38. knowledge/utils/wikipedia.py +175 -0
  39. personal_knowledge_library-3.0.0.dist-info/LICENSE +201 -0
  40. personal_knowledge_library-3.0.0.dist-info/METADATA +1163 -0
  41. personal_knowledge_library-3.0.0.dist-info/RECORD +42 -0
  42. personal_knowledge_library-3.0.0.dist-info/WHEEL +4 -0
@@ -0,0 +1,142 @@
1
+ # -*- coding: utf-8 -*-
2
+ # Copyright © 2021-present Wacom. All rights reserved.
3
+ from typing import Any
4
+ from typing import Dict, List, Iterator
5
+
6
+ import loguru
7
+
8
+ from knowledge.base.entity import (
9
+ DATA_PROPERTIES_TAG,
10
+ DATA_PROPERTY_TAG,
11
+ VALUE_TAG,
12
+ DESCRIPTION_TAG,
13
+ TYPE_TAG,
14
+ LABELS_TAG,
15
+ IS_MAIN_TAG,
16
+ DESCRIPTIONS_TAG,
17
+ LOCALE_TAG,
18
+ INDEXING_NEL_TARGET,
19
+ INDEXING_VECTOR_SEARCH_TARGET,
20
+ INDEXING_FULLTEXT_TARGET,
21
+ TARGETS_TAG,
22
+ INDEXING_VECTOR_SEARCH_DOCUMENT_TARGET,
23
+ )
24
+ from knowledge.base.language import SUPPORTED_LOCALES
25
+ from knowledge.base.ontology import OntologyPropertyReference
26
+ from knowledge.base.ontology import ThingObject, EN_US
27
+ from knowledge.services import TENANT_RIGHTS_TAG
28
+
29
+ RELATIONS_BULK_LIMIT: int = 30
30
+ """
31
+ In one request only 30 relations can be created, otherwise the database operations are too many.
32
+ """
33
+ logger = loguru.logger
34
+
35
+
36
+ def split_updates(
37
+ updates: Dict[OntologyPropertyReference, List[str]], max_operations: int = RELATIONS_BULK_LIMIT
38
+ ) -> Iterator[Dict[str, List[str]]]:
39
+ """
40
+
41
+ Parameters
42
+ ----------
43
+ updates: Dict[OntologyPropertyReference, List[str]]
44
+ The updates to split into batches.
45
+ max_operations: int (default: RELATIONS_BULK_LIMIT)
46
+ The maximum number of operations
47
+
48
+ Yields
49
+ -------
50
+ batch: List[Dict[str, List[str]]]
51
+ The batch of updates to process.
52
+ """
53
+ batch: List[Dict[str, List[str]]] = []
54
+ current_batch_size: int = 0
55
+ for predicate, targets in updates.items():
56
+ target_entry: Dict[str, List[str]] = {"relation": predicate.iri, "targets": []}
57
+ batch.append(target_entry)
58
+ for target in targets:
59
+ if current_batch_size >= max_operations:
60
+ yield batch
61
+ target_entry = {"relation": predicate.iri, "targets": []}
62
+ batch = [target_entry]
63
+ current_batch_size = 0
64
+ target_entry["targets"].append(target)
65
+ current_batch_size += 1
66
+ if current_batch_size > 0:
67
+ yield batch
68
+
69
+
70
+ def entity_payload(entity: ThingObject) -> Dict[str, Any]:
71
+ """
72
+ Create the payload for the entity.
73
+ Parameters
74
+ ----------
75
+ entity: ThingObject
76
+ The entity to create the payload for.
77
+
78
+ Returns
79
+ -------
80
+ Dict[str, Any]
81
+ The payload for the entity.
82
+ """
83
+ # Different localized content
84
+ labels: List[dict] = []
85
+ descriptions: List[dict] = []
86
+ literals: List[dict] = []
87
+ # Add description in different languages
88
+ for desc in entity.description:
89
+ if desc is None or desc.content is None:
90
+ logger.warning("Description is None")
91
+ continue
92
+
93
+ if len(desc.content) > 0 and not desc.content == " ":
94
+ descriptions.append({DESCRIPTION_TAG: desc.content, LOCALE_TAG: desc.language_code})
95
+ if len(descriptions) == 0:
96
+ # Adding an empty description
97
+ for label in entity.label:
98
+ if len(label.content) > 0 and not label.content == " ":
99
+ descriptions.append(
100
+ {DESCRIPTION_TAG: f"Description of {label.content}", LOCALE_TAG: label.language_code}
101
+ )
102
+
103
+ # Labels are tagged as main label
104
+ for label in entity.label:
105
+ if label is not None and label.content is not None and len(label.content) > 0 and label.content != " ":
106
+ labels.append({VALUE_TAG: label.content, LOCALE_TAG: label.language_code, IS_MAIN_TAG: True})
107
+ # Alias are no main labels
108
+ for label in entity.alias:
109
+ if label is not None and len(label.content) > 0 and label.content != " ":
110
+ labels.append({VALUE_TAG: label.content, LOCALE_TAG: label.language_code, IS_MAIN_TAG: False})
111
+ # Labels are tagged as main label
112
+ for _, list_literals in entity.data_properties.items():
113
+ for li in list_literals:
114
+ if li.data_property_type:
115
+ literals.append(
116
+ {
117
+ VALUE_TAG: li.value,
118
+ LOCALE_TAG: (
119
+ li.language_code if li.language_code and li.language_code in SUPPORTED_LOCALES else EN_US
120
+ ),
121
+ DATA_PROPERTY_TAG: li.data_property_type.iri,
122
+ }
123
+ )
124
+ payload: Dict[str, Any] = {
125
+ TYPE_TAG: entity.concept_type.iri,
126
+ DESCRIPTIONS_TAG: descriptions,
127
+ LABELS_TAG: labels,
128
+ DATA_PROPERTIES_TAG: literals,
129
+ }
130
+ targets: List[str] = []
131
+ if entity.use_vector_index:
132
+ targets.append(INDEXING_VECTOR_SEARCH_TARGET)
133
+ if entity.use_vector_index_document:
134
+ targets.append(INDEXING_VECTOR_SEARCH_DOCUMENT_TARGET)
135
+ if entity.use_full_text_index:
136
+ targets.append(INDEXING_FULLTEXT_TARGET)
137
+ if entity.use_for_nel:
138
+ targets.append(INDEXING_NEL_TARGET)
139
+ payload[TARGETS_TAG] = targets
140
+ if entity.tenant_access_right:
141
+ payload[TENANT_RIGHTS_TAG] = entity.tenant_access_right.to_list()
142
+ return payload