You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have search alot about for a given implementation within NLP that can expect list of words as input variable and define the sequencing or some kind of dependency for execution. For instance:
There is a list of words as, ['delete', 'add', 'redeploy', 'create', 'cell', 'sector']
Now, I would like to re-arrange these words in a sequence that tells which word/action/event should occur first before the next one. Therefore, with that logic the list of words should then be re-arranged as, ['create', 'cell', 'sector', 'add', 'delete', 'redeploy']. Which means, 'create' as an operation/action should occur before 'add' or 'delete' operation/action.
Is there any specific library exist that can calculate this dependency/sequencing among a given list of words?
The text was updated successfully, but these errors were encountered:
Here is a general approach to tackle the above issue:-
Define the Dependencies
Implement Dependency Resolution
You can implement a simple dependency resolution algorithm based on the predefined rules.
2.1 Define the Dependencies
Create a dictionary where each action has a list of actions
sample snippet:-
from collections import defaultdict, deque
def topological_sort(dependencies):
# Calculate in-degrees of each node
in_degree = defaultdict(int)
for node in dependencies:
in_degree[node] = 0
for node, deps in dependencies.items():
for dep in deps:
in_degree[node] += 1
# Collect nodes with zero in-degree
zero_in_degree_queue = deque([node for node in in_degree if in_degree[node] == 0])
sorted_order = []
while zero_in_degree_queue:
node = zero_in_degree_queue.popleft()
sorted_order.append(node)
for dependent in dependencies:
if node in dependencies[dependent]:
in_degree[dependent] -= 1
if in_degree[dependent] == 0:
zero_in_degree_queue.append(dependent)
if len(sorted_order) == len(dependencies):
return sorted_order
else:
raise Exception("There exists a cycle in the dependencies")
# Define the list of actions
actions = ['delete', 'add', 'redeploy', 'create', 'cell', 'sector']
# Filter out dependencies that are not in the actions list
filtered_dependencies = {action: [dep for dep in deps if dep in actions] for action, deps in dependencies.items() if action in actions}
# Sort actions based on dependencies
sorted_actions = topological_sort(filtered_dependencies)
print(sorted_actions)
I have search alot about for a given implementation within NLP that can expect list of words as input variable and define the sequencing or some kind of dependency for execution. For instance:
There is a list of words as, ['delete', 'add', 'redeploy', 'create', 'cell', 'sector']
Now, I would like to re-arrange these words in a sequence that tells which word/action/event should occur first before the next one. Therefore, with that logic the list of words should then be re-arranged as, ['create', 'cell', 'sector', 'add', 'delete', 'redeploy']. Which means, 'create' as an operation/action should occur before 'add' or 'delete' operation/action.
Is there any specific library exist that can calculate this dependency/sequencing among a given list of words?
The text was updated successfully, but these errors were encountered: