Example #1
0
    def test_installs_en_core_web_sm_if_not_found(self):
        """ Due to questionable PyPI security policies, check en_core_web_sm installation is fired if not present """
        nlp = spacy.load('en_core_web_sm')

        with mock.patch('PatternOmatic.api.pkg_resources.working_set'
                        ) as patch_working_set:
            with mock.patch('PatternOmatic.api.spacy_download'
                            ) as patch_spacy_download:
                with mock.patch(
                        'PatternOmatic.api.spacy_load') as patch_spacy_load:
                    patch_working_set.return_value = []
                    patch_spacy_download.return_value = 'I\'ve been fired'
                    patch_spacy_load.return_value = nlp
                    find_patterns(['Hi'])
                    super().assertTrue(patch_spacy_download.called)
Example #2
0
    def test_find_patterns_when_valid_configuration_file_provided(self):
        """ Checks that providing a valid configuration file path loads configuration from that file """

        config_file_path = \
            os.path.join(os.path.join(os.path.dirname(os.path.abspath(__file__)), os.pardir), 'config.ini')
        _ = find_patterns(self.my_samples, configuration=config_file_path)
        super().assertEqual(config_file_path, Config().file_path)
Example #3
0
 def test_find_patterns_when_bad_language_provided(self):
     """ Checks that providing an imaginary language model makes find_patterns use en_core_web_sm """
     with super().assertLogs(LOG) as cm:
         bad_model = 'Something'
         _ = find_patterns(self.my_samples,
                           spacy_language_model_name=bad_model)
         super().assertEqual(
             f'WARNING:PatternOmatic:Model {bad_model} not found, falling back to '
             f'patternOmatic\'s default language model: en_core_web_sm',
             cm.output[1])
Example #4
0
def main(args: List) -> None:
    """
    PatternOmatic's script main function wrapper
    Args:
        args: Command Line Input Arguments

    Returns: None

    """
    LOG.info('Parsing command line arguments...')
    try:
        cli = ArgumentParser(
            description=
            'Finds the Spacy\'s Matcher pattern for the given samples',
            epilog='...using actual Artificial Intelligence')

        # Samples
        cli.add_argument('-s',
                         '--sample',
                         action='append',
                         required=True,
                         nargs='+',
                         type=str,
                         help='A sample phrase')

        # Spacy Language Model
        cli.add_argument('-l',
                         '--language',
                         nargs='?',
                         type=str,
                         default='en_core_web_sm',
                         help='Spacy language model to be used')

        # Configuration file to be used
        cli.add_argument(
            '-c',
            '--config',
            nargs='?',
            type=str,
            help='Configuration file path to be used',
            default=None,
        )

        # Parse command line input arguments/options
        parsed_args = cli.parse_args(args)

        # Join sample arguments
        for index, item in enumerate(parsed_args.sample):
            parsed_args.sample[index] = ' '.join(item)

        #
        # Find patterns
        #
        patterns_found, _ = find_patterns(
            parsed_args.sample,
            configuration=parsed_args.config,
            spacy_language_model_name=parsed_args.language)

        LOG.info(f'Patterns found: {patterns_found}')

    except Exception as ex:
        LOG.critical(f'Fatal error: {repr(ex)}')
        raise ex
Example #5
0
 def test_find_patterns_when_config_instance_provided(self):
     """ Checks when setting up a Config instance before find_patterns invocation works """
     config = Config()
     config.max_runs = 10
     patterns, _ = find_patterns(self.my_samples)
     super().assertEqual(10, len(patterns))
Example #6
0
 def test_find_patterns_when_only_samples_provided(self):
     """ Tests that providing just samples makes the find_pattern keeps working """
     patterns, _ = find_patterns(self.my_samples)
     super().assertEqual(4, len(patterns))