Kaynağa Gözat

create leaf_strategy with basic dfs implementation

sxoinas12 2 yıl önce
ebeveyn
işleme
3303c2346e

+ 2 - 5
src/__tests__/test_leaf_strategy.py

@@ -3,6 +3,7 @@ import pytest
 import spacy
 from spacy import displacy
 
+
 from src.generators import Generator
 from src.strategies import leaf_strategy
 
@@ -15,10 +16,6 @@ def test_leaf_strategy(text, expected):
     generator = Generator(text=text, strategy=leaf_strategy)
 
     results = generator.suggest()
-
+    displacy.serve(docs=[generator.doc])
     doc = generator.doc
-    for token in doc:
-        print(token)
-
-    print(results)
     # assert result == expected

+ 18 - 1
src/strategies/leaf_strategy.py

@@ -1,3 +1,6 @@
+import itertools
+
+
 def leaf_strategy(doc):
     """
     Should return an arrays of variable names based on leaf strategy
@@ -6,4 +9,18 @@ def leaf_strategy(doc):
     :return Array of strings
     """
 
-    pass
+    for token in doc:
+        print(token.text, token.pos_, token.dep_)
+
+    suggestions = dfs(doc[0])
+    return suggestions
+
+
+def dfs(graph, result=[], output=[]):
+    flag = False
+    for u in graph.children:
+        flag = True
+        dfs(u, [*result, graph.text], output)
+    if flag is False:
+        output.append([*result, graph.text])
+    return output