فهرست منبع

package ready to be installed

Nikos Atlas 2 سال پیش
والد
کامیت
184da42adb
40فایلهای تغییر یافته به همراه165 افزوده شده و 36 حذف شده
  1. 4 0
      .gitignore
  2. 1 1
      .idea/word-recommender.iml
  3. 0 16
      main.py
  4. 1 1
      pytest.ini
  5. 25 5
      setup.py
  6. 0 1
      src/tests/snapshots/test_leaf_strategy/test_leaf_strategy/Regex_for_redacted_phone_numbers_with_extra_info_for_PlayStation-expected0/leaf_strategy
  7. 0 0
      word_recommender/__init__.py
  8. 22 0
      word_recommender/__main__.py
  9. 1 1
      word_recommender/generators/__init__.py
  10. 14 4
      word_recommender/generators/generator.py
  11. 0 0
      word_recommender/generators/synonym.py
  12. 7 0
      word_recommender/parsers/__init__.py
  13. 16 0
      word_recommender/parsers/javascript.py
  14. 29 0
      word_recommender/parsers/parser.py
  15. 0 0
      word_recommender/playground.py
  16. 0 0
      word_recommender/strategies/__init__.py
  17. 0 3
      word_recommender/strategies/leaf_strategy.py
  18. 0 0
      word_recommender/tests/__init__.py
  19. 0 0
      word_recommender/tests/snapshots/test_antonyms/test_antonyms/cat/antonyms
  20. 0 0
      word_recommender/tests/snapshots/test_antonyms/test_antonyms/dog/antonyms
  21. 0 0
      word_recommender/tests/snapshots/test_antonyms/test_antonyms/factory/antonyms
  22. 0 0
      word_recommender/tests/snapshots/test_antonyms/test_antonyms/fetcher/antonyms
  23. 0 0
      word_recommender/tests/snapshots/test_antonyms/test_antonyms/serializer/antonyms
  24. 1 0
      word_recommender/tests/snapshots/test_javascript_parser/test_parser/Regex_for_redacted_phone_numbers_with_extra_info_for_PlayStation/javascript_parser
  25. 1 0
      word_recommender/tests/snapshots/test_leaf_strategy/test_leaf_strategy/Regex_for_redacted_phone_numbers_with_extra_info_for_PlayStation-expected0/leaf_strategy
  26. 1 0
      word_recommender/tests/snapshots/test_parser/test_parser/Regex_for_redacted_phone_numbers_with_extra_info_for_PlayStation/parser
  27. 0 0
      word_recommender/tests/snapshots/test_synonyms/test_synonyms/cat/synonyms
  28. 0 0
      word_recommender/tests/snapshots/test_synonyms/test_synonyms/dog/synonyms
  29. 0 0
      word_recommender/tests/snapshots/test_synonyms/test_synonyms/factory/synonyms
  30. 0 0
      word_recommender/tests/snapshots/test_synonyms/test_synonyms/fetcher/synonyms
  31. 0 0
      word_recommender/tests/snapshots/test_synonyms/test_synonyms/serializer/synonyms
  32. 0 0
      word_recommender/tests/test.py
  33. 1 1
      word_recommender/tests/test_antonyms.py
  34. 19 0
      word_recommender/tests/test_javascript_parser.py
  35. 2 2
      word_recommender/tests/test_leaf_strategy.py
  36. 19 0
      word_recommender/tests/test_parser.py
  37. 1 1
      word_recommender/tests/test_synonyms.py
  38. 0 0
      word_recommender/types.py
  39. 0 0
      word_recommender/utils/__init__.py
  40. 0 0
      word_recommender/utils/word_helpers.py

+ 4 - 0
.gitignore

@@ -12,6 +12,10 @@ __pycache__
 newrelic_agent.log
 /tags
 
+build
+dist
+word_recommender.egg-info
+
 # PyCharm stuff we don't need to keep in our repo.
 .idea/codeStyles/
 .idea/codeStyleSettings.xml

+ 1 - 1
.idea/word-recommender.iml

@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <module type="PYTHON_MODULE" version="4">
   <component name="NewModuleRootManager">
-    <content url="file://$MODULE_DIR$" />
+    <content url="file://$MODULE_DIR$/../word_recommender" />
     <orderEntry type="jdk" jdkName="Python 3.8 (word-recommender)" jdkType="Python SDK" />
     <orderEntry type="sourceFolder" forTests="false" />
   </component>

+ 0 - 16
main.py

@@ -1,16 +0,0 @@
-# This is a sample Python script.
-
-# Press ⌃R to execute it or replace it with your code.
-# Press Double ⇧ to search everywhere for classes, files, tool windows, actions, and settings.
-
-
-def print_hi(name):
-    # Use a breakpoint in the code line below to debug your script.
-    print(f'Hi, {name}')  # Press ⌘F8 to toggle the breakpoint.
-
-
-# Press the green button in the gutter to run the script.
-if __name__ == '__main__':
-    print_hi('PyCharm')
-
-# See PyCharm help at https://www.jetbrains.com/help/pycharm/

+ 1 - 1
pytest.ini

@@ -1,6 +1,6 @@
 [pytest]
 testpaths=
-    src/tests
+    word_recommender/tests
 filterwarnings =
     ignore::RuntimeWarning
     ignore::DeprecationWarning

+ 25 - 5
setup.py

@@ -1,12 +1,32 @@
-from setuptools import setup
+import os
+from subprocess import check_call
+
+from setuptools import setup, find_packages
+from setuptools.command.install import install
+
+
+class PostInstallCommand(install):
+    """Post-installation for installation mode."""
+
+    def run(self):
+        install.run(self)
+        # PUT YOUR POST-INSTALL SCRIPT HERE or CALL A FUNCTION
+        # os.system("python -m spacy download en_core_web_trf")
 
 setup(
-    name='word-recommender',
-    version='0.0.1',
-    packages=['pytest'],
+    name='word_recommender',
+    version='0.0.2',
+    packages=find_packages(),
     url='',
     license='',
     author='nikatlas',
     author_email='[email protected]',
-    description='Variable name generator'
+    description='Variable name generator',
+    install_requires=[
+        'spacy',
+        'nltk',
+    ],
+    cmdclass={
+        'install': PostInstallCommand,
+    },
 )

+ 0 - 1
src/tests/snapshots/test_leaf_strategy/test_leaf_strategy/Regex_for_redacted_phone_numbers_with_extra_info_for_PlayStation-expected0/leaf_strategy

@@ -1 +0,0 @@
-[["Regex", "for", "numbers", "redacted"], ["Regex", "for", "numbers", "phone"], ["Regex", "for", "numbers", "with", "info", "extra"], ["Regex", "for", "numbers", "with", "info", "for", "PlayStation"]]

+ 0 - 0
src/__init__.py → word_recommender/__init__.py


+ 22 - 0
word_recommender/__main__.py

@@ -0,0 +1,22 @@
+import sys
+
+from word_recommender.generators import Generator
+from word_recommender.parsers import JavascriptParser
+from word_recommender.strategies import leaf_strategy
+
+
+def calculate_names(text: str):
+    generator = Generator(text=text, strategy=leaf_strategy, parser=JavascriptParser())
+
+    generator.suggest()
+
+    presentation = generator.present()
+
+    print('\n'.join(presentation))
+
+
+# Press the green button in the gutter to run the script.
+if __name__ == '__main__':
+    calculate_names(sys.argv[1])
+
+# See PyCharm help at https://www.jetbrains.com/help/pycharm/

+ 1 - 1
src/generators/__init__.py → word_recommender/generators/__init__.py

@@ -5,4 +5,4 @@ __all__ = [
     'Generator',
     'get_antonyms',
     'get_synonyms',
-]
+]

+ 14 - 4
src/generators/generator.py → word_recommender/generators/generator.py

@@ -17,9 +17,15 @@ class Generator():
     doc = None
     # the strategy resolver to use
     strategy_resolver = None
+    # the parser for the presentation
+    parser = None
 
-    def __init__(self, text=None, strategy=None):
+    # suggestions calculated
+    suggestions = None
+
+    def __init__(self, text=None, strategy=None, parser=None):
         self.strategy_resolver = strategy
+        self.parser = parser
         if text is not None:
             self.set_text(text)
 
@@ -27,13 +33,17 @@ class Generator():
         if self.doc is None or self.text is None:
             raise Error('No document')
 
-        suggestions = self.strategy_resolver(self.doc)
+        self.suggestions = self.strategy_resolver(self.doc)
 
-        return suggestions
+        return self.suggestions
 
     def set_text(self, text):
         self.text = text
         self.doc = nlp(text)
 
     def set_strategy(self, strategy_resolver):
-        self.strategy_resolver = strategy_resolver
+        self.strategy_resolver = strategy_resolver
+
+    def present(self):
+        self.parser.suggestions = self.suggestions
+        return self.parser.present()

+ 0 - 0
src/generators/synonym.py → word_recommender/generators/synonym.py


+ 7 - 0
word_recommender/parsers/__init__.py

@@ -0,0 +1,7 @@
+from .parser import Parser
+from .javascript import JavascriptParser
+
+__all__ = [
+    'Parser',
+    'JavascriptParser',
+]

+ 16 - 0
word_recommender/parsers/javascript.py

@@ -0,0 +1,16 @@
+from word_recommender.parsers import Parser
+
+
+class JavascriptParser(Parser):
+
+    def __init__(self, **kwargs):
+        super().__init__(**kwargs)
+
+    def parse_word(self, word: str):
+        return word.title()
+
+    def parse_words(self, words: [str]):
+        parsed_words = super().parse_words(words)
+        parsed_words.reverse()
+
+        return parsed_words

+ 29 - 0
word_recommender/parsers/parser.py

@@ -0,0 +1,29 @@
+class Parser:
+    suggestions = None
+
+    def __init__(self, suggestions=None):
+        self.suggestions = suggestions
+
+    def parse_word(self, word: str):
+        return word
+
+    def parse_words(self, words: [str]):
+        return [self.parse_word(word) for word in words]
+
+    def parse(self):
+        self.suggestions = [self.parse_words(words) for words in self.suggestions]
+
+        return self
+
+    def present_word(self, word: str):
+        return word
+
+    def present_words(self, words: [str]):
+        presented_words = [self.present_word(word) for word in words]
+
+        return ''.join(presented_words)
+
+    def present(self):
+        self.parse()
+
+        return [self.present_words(words) for words in self.suggestions]

+ 0 - 0
src/playground.py → word_recommender/playground.py


+ 0 - 0
src/strategies/__init__.py → word_recommender/strategies/__init__.py


+ 0 - 3
src/strategies/leaf_strategy.py → word_recommender/strategies/leaf_strategy.py

@@ -9,9 +9,6 @@ def leaf_strategy(doc):
     :return Array of strings
     """
 
-    for token in doc:
-        print(token.text, token.pos_, token.dep_)
-
     suggestions = dfs(doc[0])
     return suggestions
 

+ 0 - 0
src/tests/__init__.py → word_recommender/tests/__init__.py


+ 0 - 0
src/tests/snapshots/test_antonyms/test_antonyms/cat/antonyms → word_recommender/tests/snapshots/test_antonyms/test_antonyms/cat/antonyms


+ 0 - 0
src/tests/snapshots/test_antonyms/test_antonyms/dog/antonyms → word_recommender/tests/snapshots/test_antonyms/test_antonyms/dog/antonyms


+ 0 - 0
src/tests/snapshots/test_antonyms/test_antonyms/factory/antonyms → word_recommender/tests/snapshots/test_antonyms/test_antonyms/factory/antonyms


+ 0 - 0
src/tests/snapshots/test_antonyms/test_antonyms/fetcher/antonyms → word_recommender/tests/snapshots/test_antonyms/test_antonyms/fetcher/antonyms


+ 0 - 0
src/tests/snapshots/test_antonyms/test_antonyms/serializer/antonyms → word_recommender/tests/snapshots/test_antonyms/test_antonyms/serializer/antonyms


+ 1 - 0
word_recommender/tests/snapshots/test_javascript_parser/test_parser/Regex_for_redacted_phone_numbers_with_extra_info_for_PlayStation/javascript_parser

@@ -0,0 +1 @@
+["RedactedNumbersForRegex", "PhoneNumbersForRegex", "ExtraInfoWithNumbersForRegex", "PlaystationForInfoWithNumbersForRegex"]

+ 1 - 0
word_recommender/tests/snapshots/test_leaf_strategy/test_leaf_strategy/Regex_for_redacted_phone_numbers_with_extra_info_for_PlayStation-expected0/leaf_strategy

@@ -0,0 +1 @@
+[["Regex", "for", "numbers", "redacted"], ["Regex", "for", "numbers", "phone"], ["Regex", "for", "numbers", "with", "info", "extra"], ["Regex", "for", "numbers", "with", "info", "for", "PlayStation"], ["Regex", "for", "numbers", "redacted"], ["Regex", "for", "numbers", "phone"], ["Regex", "for", "numbers", "with", "info", "extra"], ["Regex", "for", "numbers", "with", "info", "for", "PlayStation"]]

+ 1 - 0
word_recommender/tests/snapshots/test_parser/test_parser/Regex_for_redacted_phone_numbers_with_extra_info_for_PlayStation/parser

@@ -0,0 +1 @@
+["Regexfornumbersredacted", "Regexfornumbersphone", "Regexfornumberswithinfoextra", "RegexfornumberswithinfoforPlayStation", "Regexfornumbersredacted", "Regexfornumbersphone", "Regexfornumberswithinfoextra", "RegexfornumberswithinfoforPlayStation", "Regexfornumbersredacted", "Regexfornumbersphone", "Regexfornumberswithinfoextra", "RegexfornumberswithinfoforPlayStation"]

+ 0 - 0
src/tests/snapshots/test_synonyms/test_synonyms/cat/synonyms → word_recommender/tests/snapshots/test_synonyms/test_synonyms/cat/synonyms


+ 0 - 0
src/tests/snapshots/test_synonyms/test_synonyms/dog/synonyms → word_recommender/tests/snapshots/test_synonyms/test_synonyms/dog/synonyms


+ 0 - 0
src/tests/snapshots/test_synonyms/test_synonyms/factory/synonyms → word_recommender/tests/snapshots/test_synonyms/test_synonyms/factory/synonyms


+ 0 - 0
src/tests/snapshots/test_synonyms/test_synonyms/fetcher/synonyms → word_recommender/tests/snapshots/test_synonyms/test_synonyms/fetcher/synonyms


+ 0 - 0
src/tests/snapshots/test_synonyms/test_synonyms/serializer/synonyms → word_recommender/tests/snapshots/test_synonyms/test_synonyms/serializer/synonyms


+ 0 - 0
src/tests/test.py → word_recommender/tests/test.py


+ 1 - 1
src/tests/test_antonyms.py → word_recommender/tests/test_antonyms.py

@@ -2,7 +2,7 @@ import json
 
 import pytest
 
-import src.generators.synonym as synonym
+import word_recommender.generators.synonym as synonym
 
 
 @pytest.mark.parametrize(

+ 19 - 0
word_recommender/tests/test_javascript_parser.py

@@ -0,0 +1,19 @@
+import json
+
+import pytest
+
+from word_recommender.generators import Generator
+from word_recommender.parsers import JavascriptParser
+from word_recommender.strategies import leaf_strategy
+
[email protected]('text', [
+    'Regex for redacted phone numbers with extra info for PlayStation'
+])
+def test_parser(snapshot, text):
+    generator = Generator(text=text, strategy=leaf_strategy, parser=JavascriptParser())
+
+    generator.suggest()
+
+    presentation = generator.present()
+    snapshot.assert_match(json.dumps(presentation), 'javascript_parser')
+

+ 2 - 2
src/tests/test_leaf_strategy.py → word_recommender/tests/test_leaf_strategy.py

@@ -5,8 +5,8 @@ import pytest
 import spacy
 from spacy import displacy
 
-from src.generators import Generator
-from src.strategies import leaf_strategy
+from word_recommender.generators import Generator
+from word_recommender.strategies import leaf_strategy
 
 nlp = spacy.load("en_core_web_trf")
 

+ 19 - 0
word_recommender/tests/test_parser.py

@@ -0,0 +1,19 @@
+import json
+
+import pytest
+
+from word_recommender.generators import Generator
+from word_recommender.parsers import Parser
+from word_recommender.strategies import leaf_strategy
+
[email protected]('text', [
+    'Regex for redacted phone numbers with extra info for PlayStation'
+])
+def test_parser(snapshot, text):
+    generator = Generator(text=text, strategy=leaf_strategy, parser=Parser())
+
+    generator.suggest()
+
+    presentation = generator.present()
+    snapshot.assert_match(json.dumps(presentation), 'parser')
+

+ 1 - 1
src/tests/test_synonyms.py → word_recommender/tests/test_synonyms.py

@@ -2,7 +2,7 @@ import json
 
 import pytest
 
-import src.generators.synonym as synonym
+import word_recommender.generators.synonym as synonym
 
 
 @pytest.mark.parametrize(

+ 0 - 0
src/types.py → word_recommender/types.py


+ 0 - 0
src/utils/__init__.py → word_recommender/utils/__init__.py


+ 0 - 0
src/utils/word_helpers.py → word_recommender/utils/word_helpers.py