Nikos Atlas 2 years ago
parent
commit
42a29912b4

+ 6 - 4
word_processor/parsers/javascript.py

@@ -1,3 +1,5 @@
+from spacy.tokens import Token
+
 from word_processor.parsers import Parser
 
 
@@ -6,11 +8,11 @@ class JavascriptParser(Parser):
     def __init__(self, **kwargs):
         super().__init__(**kwargs)
 
-    def parse_word(self, word: str):
-        return word.text.title()
+    def parse_word(self, word: Token):
+        return word.lemma_.title()
 
-    def parse_words(self, words: [str]):
+    def parse_words(self, words: [Token]):
         parsed_words = super().parse_words(words)
-        parsed_words.reverse()
+        #parsed_words.reverse()
 
         return parsed_words

+ 9 - 2
word_processor/strategies/leaf_no_preps_strategy.py

@@ -9,8 +9,15 @@ def leaf_no_prep_strategy(doc):
     :return Array of strings
     """
 
-    ## TODO dont account for `for`, `at`, `in` based on token._dep type
     suggestions = leaf_strategy(doc)
-    return suggestions
 
+    new_suggestions = []
+    for suggestion in suggestions:
+        new_suggestion = []
+        for token in suggestion:
+            if token.dep_ != 'prep':
+                new_suggestion.append(token)
 
+        new_suggestions.append(new_suggestion)
+
+    return new_suggestions

+ 11 - 3
word_processor/strategies/leaf_strategy.py

@@ -1,5 +1,9 @@
+from spacy.tokens import Token
 
-def leaf_strategy(doc):
+from word_processor.types import DEP_TYPES
+
+
+def leaf_strategy(doc) -> [[Token]]:
     """
     Should return an arrays of variable names based on leaf strategy
 
@@ -7,8 +11,12 @@ def leaf_strategy(doc):
     :return Array of strings
     """
 
-    suggestions = dfs(doc[0])
-    return suggestions
+    for token in doc:
+        if token.dep_ == DEP_TYPES['ROOT']:
+            suggestions = dfs(token)
+            break
+
+    return suggestions or []
 
 
 def dfs(graph, result=[], output=[]):

+ 32 - 0
word_processor/tests/test.py

@@ -1 +1,33 @@
+import json
 
+import pytest
+from word_processor.parsers import JavascriptParser
+
+from word_processor.strategies.leaf_no_preps_strategy import leaf_no_prep_strategy
+
+from word_processor.generators import Generator
+from spacy import displacy
+
[email protected](
+    'text',
+    [
+        # 'Regex for redacted phone numbers with extra info for PlayStation',
+        # 'Two days ago i was shocked by a red pair of trousers',
+        'Setting priority for attempting hydration',
+        # 'Attempting to set hydration priority',
+        # 'file Cache Provider get Cache Dir',
+        #'Setters Value Animator Animator Update Listener'
+    ]
+)
+def test_antonyms(snapshot, text):
+    generator = Generator(text=text, strategy=leaf_no_prep_strategy, parser=JavascriptParser())
+
+    results = generator.suggest()
+
+    text_results = []
+    for suggestion in results:
+        text_results.append([token.lemma_ for token in suggestion])
+
+    presented_results = generator.present()
+    # displacy.serve(generator.doc, port=5001)
+    print(presented_results)

+ 3 - 5
word_processor/tests/test_leaf_no_prep_strategy.py

@@ -1,11 +1,9 @@
-import json
-
 import pytest
 
 import spacy
+from word_processor.strategies.leaf_no_preps_strategy import leaf_no_prep_strategy
 
 from word_processor.generators import Generator
-from word_processor.strategies import leaf_strategy
 
 nlp = spacy.load("en_core_web_trf")
 
@@ -14,8 +12,8 @@ nlp = spacy.load("en_core_web_trf")
     ('Regex for redacted phone numbers with extra info for PlayStation',
      ['PhoneNumberRegex', 'RedactedPhoneNumberRegex'])
 ])
-def test_leaf_strategy(snapshot, text, expected):
-    generator = Generator(text=text, strategy=leaf_strategy)
+def test_leaf_no_prep_strategy(snapshot, text, expected):
+    generator = Generator(text=text, strategy=leaf_no_prep_strategy)
 
     results = generator.suggest()
 

+ 5 - 0
word_processor/types.py

@@ -0,0 +1,5 @@
+
+DEP_TYPES = {
+    'ROOT': 'ROOT',
+}
+