Skip to content
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

Dependency parsing using NLP for list of words rather than a given sentence #634

Open
gupta-alok opened this issue Mar 13, 2023 · 1 comment

Comments

@gupta-alok
Copy link

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?

@Siddharth-Latthe-07
Copy link

Here is a general approach to tackle the above issue:-

  1. Define the Dependencies
  2. 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:-
dependencies = {
    'create': [],
    'cell': ['create'],
    'sector': ['create'],
    'add': ['create', 'cell', 'sector'],
    'delete': ['create', 'cell', 'sector'],
    'redeploy': ['create', 'cell', 'sector', 'add', 'delete']
}

2.2 Implement a Topological Sort
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)

Hope this helps, Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants