-
Notifications
You must be signed in to change notification settings - Fork 10
[#1244] Add CustomizableLinkCreator to LCA #1276
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
4638dde
Merge remote-tracking branch 'origin' into senna-1244-1
b9892db
Minor fixes
f0c4fc1
WIP
58010ae
Merge remote-tracking branch 'origin' into senna-1244-1
807d2ce
WIP
8724f80
Add new optional parameter LINK_CREATOR_EXTRA_PARAMETERS
20c8565
Minor fixes
0e66373
Minor new features - accepts string as join() delimiter
1383431
Add CustomizableLinkCreator
9071528
Merge remote-tracking branch 'origin' into senna-1244-1
95f5377
Format fixes
88987df
Peer review fixes
92c3540
Peer review fixes
090d4eb
Peer review fixes
aa211ec
Format fixes
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
src/agents/link_creation_agent/link_creators/CustomizableLinkCreator.cc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| #include "CustomizableLinkCreator.h" | ||
|
|
||
| #include "Hasher.h" | ||
| #include "tags.h" | ||
|
|
||
| using namespace link_creators; | ||
|
|
||
| // ------------------------------------------------------------------------------------------------- | ||
| // Public methods | ||
|
|
||
| CustomizableLinkCreator::CustomizableLinkCreator() {} | ||
|
|
||
| CustomizableLinkCreator::~CustomizableLinkCreator() {} | ||
|
|
||
| LinkCreationStats CustomizableLinkCreator::create(shared_ptr<QueryAnswer> query_answer) { | ||
| STACK_TRACE(); | ||
| LinkCreationStats stats; | ||
| for (LinkSpecification& spec : this->link_specification) { | ||
| if ((spec.target_elements.size() == 0) || (spec.link_type == "")) { | ||
| RAISE_ERROR("Invalid empty target elements or link_type"); | ||
| break; | ||
| } | ||
| vector<string> handles; | ||
| vector<double> strength_components; | ||
| handles.push_back(Hasher::node_handle(SYMBOL, spec.link_type)); | ||
| for (QueryAnswerElement& element : spec.target_elements) { | ||
| handles.push_back(query_answer->get(element)); | ||
| } | ||
| string key = Utils::join(handles, ' '); | ||
| if (!visited(key)) { | ||
| visit(key); | ||
| stats.visited = true; | ||
| for (QueryAnswerElement& element : spec.strength_elements) { | ||
| strength_components.push_back(get_strength(query_answer->get(element))); | ||
| } | ||
| AddLinkStatus add_status = add_or_update_link( | ||
| handles, compute_strength(strength_components, spec.strength_composition)); | ||
| if (add_status == CREATED) { | ||
| stats.created++; | ||
| } else if (add_status == UPDATED) { | ||
| stats.updated++; | ||
| } | ||
| } | ||
| } | ||
| return stats; | ||
| } | ||
|
|
||
| void CustomizableLinkCreator::extra_parameters(const string& extra_parameters) { | ||
| if (extra_parameters != "") { | ||
| vector<string> tokens = Utils::split(extra_parameters); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| untokenize(tokens); | ||
| } | ||
| } | ||
|
|
||
| void CustomizableLinkCreator::add_link_specification(const vector<QueryAnswerElement>& target_elements, | ||
| const vector<QueryAnswerElement>& strength_elements, | ||
| StrengthComposition strength_composition, | ||
| const string& link_type) { | ||
| string trimmed_type = Utils::trim(link_type); | ||
| if ((trimmed_type == "") || (trimmed_type.find(' ') != std::string::npos)) { | ||
| RAISE_ERROR("Invalid link_type: " + link_type); | ||
| } | ||
|
|
||
| link_specification.emplace_back( | ||
| target_elements, strength_elements, strength_composition, trimmed_type); | ||
| } | ||
|
|
||
| void CustomizableLinkCreator::tokenize(vector<string>& tokens) { | ||
| tokens.push_back(std::to_string(this->link_specification.size())); | ||
| for (LinkSpecification& spec : this->link_specification) { | ||
| tokens.push_back(std::to_string(spec.target_elements.size())); | ||
| for (QueryAnswerElement& element : spec.target_elements) { | ||
| tokens.push_back(element.to_string()); | ||
| } | ||
| tokens.push_back(std::to_string(spec.strength_elements.size())); | ||
| for (QueryAnswerElement& element : spec.strength_elements) { | ||
| tokens.push_back(element.to_string()); | ||
| } | ||
| tokens.push_back(std::to_string(spec.strength_composition)); | ||
| tokens.push_back(spec.link_type); | ||
| } | ||
| } | ||
|
|
||
| static inline string& safe_get_next_token(vector<string>& tokens, unsigned int& cursor) { | ||
| if (cursor >= tokens.size()) { | ||
| RAISE_ERROR("Invalid tokens for CustomizableLinkCreator"); | ||
| } | ||
| return tokens[cursor++]; | ||
| } | ||
|
|
||
| void CustomizableLinkCreator::untokenize(vector<string>& tokens) { | ||
| unsigned int cursor = 0; | ||
| unsigned int num_specs = Utils::string_to_uint(safe_get_next_token(tokens, cursor)); | ||
| for (unsigned int i = 0; i < num_specs; i++) { | ||
| vector<QueryAnswerElement> _target_elements; | ||
| vector<QueryAnswerElement> _strength_elements; | ||
| StrengthComposition _strength_composition; | ||
| string _link_type; | ||
| unsigned int num_elements = Utils::string_to_uint(safe_get_next_token(tokens, cursor)); | ||
| for (unsigned int j = 0; j < num_elements; j++) { | ||
| _target_elements.push_back( | ||
| QueryAnswerElement::from_string(safe_get_next_token(tokens, cursor))); | ||
| } | ||
| num_elements = Utils::string_to_uint(safe_get_next_token(tokens, cursor)); | ||
| for (unsigned int j = 0; j < num_elements; j++) { | ||
| _strength_elements.push_back( | ||
| QueryAnswerElement::from_string(safe_get_next_token(tokens, cursor))); | ||
| } | ||
| _strength_composition = | ||
| (StrengthComposition) Utils::string_to_uint(safe_get_next_token(tokens, cursor)); | ||
| _link_type = safe_get_next_token(tokens, cursor); | ||
| add_link_specification(_target_elements, _strength_elements, _strength_composition, _link_type); | ||
| } | ||
| if (cursor != tokens.size()) { | ||
| RAISE_ERROR("Invalid trailing tokens for CustomizableLinkCreator"); | ||
| } | ||
| } | ||
|
|
||
| // ------------------------------------------------------------------------------------------------- | ||
| // Private methods | ||
|
|
||
| double CustomizableLinkCreator::compute_strength(const vector<double>& components, | ||
| StrengthComposition composition) { | ||
| double answer = 0.0; | ||
| switch (composition) { | ||
| case PRODUCT: | ||
| answer = 1.0; | ||
| for (double strength : components) { | ||
| answer *= strength; | ||
| } | ||
| break; | ||
| default: | ||
| RAISE_ERROR("Invalid strength composition: " + std::to_string(composition)); | ||
| break; | ||
| } | ||
| return answer; | ||
| } | ||
55 changes: 55 additions & 0 deletions
55
src/agents/link_creation_agent/link_creators/CustomizableLinkCreator.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| #pragma once | ||
| #include <vector> | ||
|
|
||
| #include "LinkCreator.h" | ||
|
|
||
| using namespace std; | ||
|
|
||
| namespace link_creators { | ||
|
|
||
| /** | ||
| * | ||
| */ | ||
| class CustomizableLinkCreator : public LinkCreator { | ||
| public: | ||
| enum StrengthComposition { UNDEFINED = 0, PRODUCT }; | ||
|
|
||
| CustomizableLinkCreator(); | ||
| ~CustomizableLinkCreator(); | ||
|
|
||
| LinkCreationStats create(shared_ptr<QueryAnswer> query_answer); | ||
| virtual void extra_parameters(const string& extra_parameters); | ||
|
|
||
| private: | ||
| class LinkSpecification { | ||
| public: | ||
| LinkSpecification() = default; | ||
| LinkSpecification(const vector<QueryAnswerElement>& target_elements, | ||
| const vector<QueryAnswerElement>& strength_elements, | ||
| StrengthComposition strength_composition, | ||
| string link_type) { | ||
| this->target_elements = target_elements; | ||
| this->strength_elements = strength_elements; | ||
| this->strength_composition = strength_composition; | ||
| this->link_type = link_type; | ||
| } | ||
| vector<QueryAnswerElement> target_elements; | ||
| vector<QueryAnswerElement> strength_elements; | ||
| StrengthComposition strength_composition; | ||
| string link_type; | ||
| }; | ||
|
|
||
| vector<LinkSpecification> link_specification; | ||
|
|
||
| double compute_strength(const vector<double>& components, StrengthComposition composition); | ||
|
|
||
| public: | ||
| void tokenize(vector<string>& tokens); | ||
| void untokenize(vector<string>& tokens); | ||
| void add_link_specification(const vector<QueryAnswerElement>& target_elements, | ||
| const vector<QueryAnswerElement>& strength_elements, | ||
| StrengthComposition strength_composition, | ||
| const string& link_type); | ||
| }; | ||
|
|
||
| } // namespace link_creators |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.