Ejemplo n.º 1
0
    def __str__(self):
        words_with_plural = list()
        for word in self.words.split(' '):
            plural = plural_singular.get_plurals(word, cbow=self.wordembedding)
            if plural:
                words_with_plural.append('[{} {}]'.format(word, plural))
            else:
                words_with_plural.append(word)

        words_with_plural = ' '.join(words_with_plural)

        if len(self.group) > 1:

            gen_rule = ('u: {label} (<<{words}>>)\n\t'
                        '$quest = %originalsentence\n\t'
                        '^pick(~not_well_understood), %user, '
                        'mas ^pick(~search_options):\n\t - {questions}\n'
                        '{group_rejoinders}').format(
                            label=self.label,
                            words=words_with_plural,
                            questions='\n\t - '.join(self.questions) + ' -',
                            group_rejoinders=self.rejoinders_text())
        else:
            gen_rule = ('u: {label} (<<{words}>>)\n\t'
                        '$quest = %originalsentence\n\t'
                        '^pick(~not_well_understood), %user, '
                        '^pick(~you_mean) "{sugestion}"?\n'
                        '{group_rejoinders}').format(
                            label=self.label,
                            words=words_with_plural,
                            sugestion=self.group[0].title,
                            group_rejoinders=self.rejoinders_text())

        return gen_rule
Ejemplo n.º 2
0
    def generate_keywords_plurals(self):
        plurals = list()
        if self.name.endswith('_gen'):
            return plurals

        for kw in self.keywords:
            plural = ''
            for subkw in kw.split(' '):
                plural_sub = plural_singular.get_plurals(
                    subkw, cbow=self.wordembedding)
                if plural_sub:
                    plural = plural + ' ' + plural_sub
            plurals.append(plural)
        return plurals
Ejemplo n.º 3
0
    def add_plurals(self, embedding_model):
        question = self.add_syns_question
        # if self.splited_entities:
        #     entities = self.splited_entities
        # else:
        entities = list()
        tg_words = find_keywords.tag_text(self.add_syns_question)
        for word in tg_words.split(' '):
            if (
                not word.startswith('*') and
                find_keywords.has_tags(word, ['N', 'NPROP', 'ADJ', 'PCP'])
            ):
                entities.append(word.split('/')[0])

        for ent in entities:
            ent_plural = plural_singular.get_plurals(ent, embedding_model)
            if ent_plural:
                question = question.replace(
                    ent, '[{} {}]'.format(ent, ent_plural)
                )
        self.add_syns_question = question
Ejemplo n.º 4
0
def generate_topics_rule(rejoinder, cbow, syns):
    result = re.match(
        r'a: (?P<label>M\d+) \((?P<words>.*)\).*?'
        r'(?P<options>\^pick.*?)(?P<patterns>b:.*)',
        rejoinder,
        flags=re.DOTALL).groupdict()
    rule_words = get_splited_words(result['words'])

    rejoinders = list()
    patterns = re.finditer(r'b: \((?P<words>.*?)\) (?P<action>.*?)\n',
                           result['patterns'])
    for pat in patterns:
        pat_dict = pat.groupdict()
        pat_words = get_splited_words(pat_dict['words'])

        # Remove stopwords words in rule keywords
        words = [
            w for w in pat_words if w not in rule_words and w not in stopwords
        ]
        words = get_words_syns(words, syns)

        plurals = list()
        for w in words:
            plural = plural_singular.get_plurals(w, cbow=cbow)
            if plural:
                plurals.append(plural)

        rejoinders.append('a: ({words}) {action} ^end(RULE)'.format(
            words='[{}]'.format(' '.join(words + plurals)),
            action=pat_dict['action']))

    rule = ('u: ({words}) ^refine()\n\t{rejoinders}\n\t'
            'a: ()\n'
            '\t\tif($go_to_menu){{^reuse(~menu.{label})}}\n'
            '\t\telse {{$go_to_menu = true}}').format(
                words=result['words'],
                rejoinders='\n\t'.join(rejoinders),
                options=result['options'],
                label=result['label'])
    return rule
Ejemplo n.º 5
0
    def add_plurals_and_nouns_syns(self, embedding_model):
        question = self.add_syns_question
        entities = list()
        tg_words = find_keywords.tag_text(self.add_syns_question)
        for word in tg_words.split(' '):
            if (not word.startswith('*') and find_keywords.has_tags(
                    word, ['N', 'NPROP', 'ADJ', 'PCP'])):
                entities.append(word.split('/')[0])

        for ent in entities:
            syns = self.get_syns(ent)
            plurals = list()
            for syn in syns:
                syn_plural = plural_singular.get_plurals(syn, embedding_model)
                if syn_plural:
                    plurals.append(syn_plural)

            plurals_text = ' {}'.format(' '.join(plurals)) if plurals else ''
            question = question.replace(
                ent, '[{}{}]'.format(' '.join(syns), plurals_text))

        self.add_syns_question = question
Ejemplo n.º 6
0
 def test_plural_singular(self):
     assert 'pedidos' == plural_singular.get_plurals('pedido')
Ejemplo n.º 7
0
def generate_topic_menu(topics, cbow, syns):
    top_names = list()
    rejoinders = list()
    index = 0
    for topic in topics:
        if topic.beauty_name:
            top_names.append(topic.beauty_name)
            options = list()
            options_rule = list()
            for rule in topic.rules:
                if rule.title:
                    options.append(rule.title)
                    output = '^reuse(~{}.{})'.format(topic.name, rule.label)
                    options_rule.append('\n\t\tb: ({pattern}) {output}'.format(
                        pattern=rule.title, output=output))

            # Join options
            options = '\n\t\t- '.join(options)
            options_text = (
                '\n\t\t^pick(~all_right), aqui estão opções relacionadas a '
                '"{entity}":\n\t\t- {options} \n\t\t- Menu completo - '
            ).format(entity=topic.beauty_name, options=options)
            options_rule = ''.join(options_rule)

            name = topic.beauty_name.lower()
            plural = ''

            # Add syns to topic name
            names = list()
            for subname in name.split(' '):
                names.extend(get_syns(subname, syns))

            for subname in names:
                plural_sub = plural_singular.get_plurals(subname, cbow=cbow)
                if plural_sub:
                    plural = plural + ' ' + plural_sub

            pattern = '[{} {}]'.format(' '.join(names),
                                       plural) if plural else name
            # Remove extra spaces
            pattern = pattern.replace('  ', ' ')

            rej = 'a: M{num} ({pattern}){options_text}{options_rule}'.format(
                num=index,
                pattern=pattern,
                options_text=options_text,
                options_rule=options_rule,
            )
            rejoinders.append(rej)
            index = index + 1

    topics_names = '- {}'.format('\n\t- '.join(top_names))
    menu = TopicMenu(topics_names,
                     '\n\t'.join(rejoinders),
                     'menu',
                     words='menu opções ajuda',
                     extra='nostay ')

    rules = [generate_topics_rule(rej, cbow, syns) for rej in rejoinders]
    topics = TopicTopics('', rules, 'topics')
    return menu, topics