Пример #1
0
 def cast_base_verb(verb, pos):
     if pos == "VB":
         # Verb, base
         return verb
     elif pos == "VBD":
         # Verb, past tense
         return conjugate(verb, tense="past")
     elif pos == "VBG":
         # Verb, gerund or present participle
         return conjugate(verb,
                          tense="present",
                          mood="indicative",
                          aspect="progressive")
     elif pos == "VBN":
         # Verb, past participle
         return conjugate(verb,
                          tense="past",
                          mood="indicative",
                          aspect="progressive")
     elif pos == "VBP":
         # Verb, non 3rd-person singular present
         return conjugate(verb,
                          tense="present",
                          person=1,
                          number="singular",
                          mood="indicative",
                          aspect="imperfective")
     elif pos == "VBZ":
         # Verb, 3rd-person singular present
         return conjugate(verb,
                          tense="present",
                          person=3,
                          number="singular",
                          mood="indicative",
                          aspect="imperfective")
Пример #2
0
 def change_tense(self, past=False, pres_3sg=False):
     root = self.root
     if root is None or not is_verb(root):
         return
     conj = []
     for tok in self.answer:
         if 'head' in tok and tok['head'] == root['id'] and tok[
                 'deprel'] == 'conj' and is_verb(tok):
             conj.append(tok)
     if past:
         for tok in [root] + conj:
             if tok['form'] == 'leave':
                 new_form = 'left'
             else:
                 new_form = conjugate(tok['form'], tense='past')
             tok['form'] = new_form
             tok['xpostag'] = 'VBD'
     elif pres_3sg:
         for tok in [root] + conj:
             tok['form'] = conjugate(tok['form'],
                                     tense='present',
                                     person=3,
                                     number='singular')
             tok['xpostag'] = 'VBZ'
     return
Пример #3
0
def convert_postag(complex_word, candidates):
    specific_tag = NLP.pos_tag(complex_word)[0][1]
    generic_tag = get_type(specific_tag)
    # print(generic_tag)
    final_candidates = set()
    if generic_tag == "NN":  ### Nouns
        # print(generic_tag)
        for candidate in candidates:
            candidate_tag = NLP.pos_tag(candidate)[0][1]
            if specific_tag == "NNS" and candidate_tag != "NNS":
                candidate = pluralize(candidate)
                # print("pluraaal  ", candidate)
            elif specific_tag == "NN" and candidate_tag == "NNS":
                candidate = singularize(candidate)
                # print("singulaaar" , candidate)
            # print("wwilll add")
            final_candidates.add(candidate)
    elif generic_tag == "ADJ":  ## Adjectives
        for candidate in candidates:
            candidate_tag = NLP.pos_tag(candidate)[0][1]
            if specific_tag == "JJR" and candidate_tag != "JJR":
                candidate = comparative(candidate)
                # print(candidate , "jjr")
            elif specific_tag == "JJS" and candidate_tag != "JJS":
                # print(candidate , "jjs")
                candidate = superlative(candidate)
            # print(candidate , "added")
            final_candidates.add(candidate)
    elif generic_tag == "VB":  ## Verbs
        complex_tense = tenses(complex_word)
        if (len(complex_tense)) < 1: return candidates

        for candidate in candidates:
            # print("my tense" ,  complex_tense.upper()  ," candidate " , candidate , " ", tenses(candidate)[0][0] )
            if len(tenses(candidate)) > 0 and tenses(
                    candidate)[0][0] != complex_tense:
                if complex_tense == "past":
                    candidate = conjugate(candidate, tense=PAST)
                elif complex_tense == "present":
                    candidate = conjugate(candidate, tense=PRESENT)
                elif complex_tense == "future":
                    candidate = conjugate(candidate, tense=FUTURE)
                elif complex_tense == "infinitive":
                    candidate = conjugate(candidate, tense=INFINITIVE)
            final_candidates.add(candidate)
    else:
        final_candidates = candidates

    return final_candidates
Пример #4
0
def myFunction():

    try:
        verb=en.conjugate('go',tense='PAST')
    except StopIteration:
        return
    return verb
Пример #5
0
    def stem(self, word):
        token = singularize(word)

        conjugation = conjugate(token, 'inf')
        if conjugation:
            token = str(conjugation)

        return token
Пример #6
0
def inflect_verbs(doc):
    matches = verb_inflection_matcher(doc)
    matches = format_matches(matches)
    for idx in matches:
        text = doc[idx].text
        tag = doc[idx].tag_
        past_tense = False
        alias = 'pl'
        if tag in ['VBD', 'VBN']:
            alias = 'ppl' # past tense
        try: conjugate(text, alias)
        except: pass
        inflected = conjugate(text, alias)
        if inflected == 'used': # fix bug in pattern.en
            inflected = 'use'
        doc[idx]._.new_text = inflected
        string = doc[idx].string
        inflected_str = string.replace(text, inflected)
        doc[idx]._.new_string = inflected_str
        doc[idx]._.change = 'inflected'
    return doc
Пример #7
0
def iambic_syllable(count):
    random_pronoun = choice(pronoun)
    random_noun = choice(nouns)
    random_adverb = choice(adverbs)
    random_verb = conjugate(choice(verbs), tense="past")
    random_verb2 = choice(verbs)
    random_noun2 = choice(nouns)
    conjunction = choice(conjunctions)
    random_verb3 = choice(verbs)

    iambic_phrase = random_pronoun + ' ' + random_adverb + \
                    ' ' + random_verb + ' the ' + random_noun + '.'
    iambic_phrase2 = '\n' + conjunction + ' ' + conjugate(random_verb2, tense="past") + \
                    ' the ' + random_noun2 + '.' + '\nI ' + random_verb3 + '.'

    iambic_phrase += iambic_phrase2

    if (estimate(iambic_phrase) == count):
        return iambic_phrase
    else:
        return iambic_syllable(count)
Пример #8
0
def get_words(dict):
    keys = list(dict.keys())
    random.shuffle(keys)

    # Loop inspired by Austin Marshall on Stack Overflow
    # https://stackoverflow.com/questions/7785672/how-to-iterate-through-dict-in-random-order-in-python
    for key in keys:
        while dict[key] == "" and key != "NOUN_2P":
            if key.startswith("A"):
                dict[key] = input("Please input an " + get_type(key) + ": ")
            else:
                dict[key] = input("Please input a " + get_type(key) + ": ")

            if dict[key] == "":
                print("I didn't get that. Please try again")

    # Special modifications of other words
    dict["NOUN_2P"] = pluralize(dict["NOUN_2"])
    dict["VERB_1P"] = conjugate(dict["VERB_1"], "p")
Пример #9
0
def is_capable_of(stri):
    sentences = sent_tokenize(stri)
    keywords = ['is capable of', 'are capable of']
    outcome = ''
    for sent in sentences:
        for keyword in keywords:
            if keyword in sent:
                before_keyword, keyword, after_keyword = sent.partition(
                    keyword)
                token = nltk.word_tokenize(sent)
                for i in nltk.pos_tag(token):
                    if i[0] == after_keyword.split()[0] and i[1] == 'VBG':
                        outcome += " " + before_keyword + conjugate(
                            verb=after_keyword.split()[0],
                            tense=PRESENT,
                            number=SG) + " " + ' '.join(
                                after_keyword.split()[1:])
            else:
                if sent not in outcome:
                    outcome += " " + sent
    return outcome
Пример #10
0
def _get_conjs(verb=""):
    """
    Generate possible conjugation of given verb
    """
    return [en.conjugate(verb, c)+"\t_\t_\tVB" for c in tense]
Пример #11
0
def pass2act(doc, rec=False):
    parse = nlp(doc)
    newdoc = ''
    for sent in parse.sents:

        # Init parts of sentence to capture:
        subjpass = ''
        subj = ''
        verb = ''
        verbtense = ''
        adverb = {'bef': '', 'aft': ''}
        part = ''
        prep = ''
        agent = ''
        aplural = False
        advcltree = None
        aux = list(list(nlp('. .').sents)[0])  # start with 2 'null' elements
        xcomp = ''
        punc = '.'
        # Analyse dependency tree:
        for word in sent:
            if word.dep_ == 'advcl':
                if word.head.dep_ in ('ROOT', 'auxpass'):
                    advcltree = word.subtree
            if word.dep_ == 'nsubjpass':
                if word.head.dep_ == 'ROOT':
                    subjpass = ''.join(
                        w.text_with_ws.lower() if w.tag_ not in (
                            'NNP', 'NNPS') else w.text_with_ws
                        for w in word.subtree).strip()
            if word.dep_ == 'nsubj':
                subj = ''.join(w.text_with_ws.lower() if w.tag_ not in (
                    'NNP', 'NNPS') else w.text_with_ws
                               for w in word.subtree).strip()
                if word.head.dep_ == 'auxpass':
                    if word.head.head.dep_ == 'ROOT':
                        subjpass = subj
            if word.dep_ in ('advmod', 'npadvmod', 'oprd'):
                if word.head.dep_ == 'ROOT':
                    if verb == '':
                        adverb['bef'] = ''.join(
                            w.text_with_ws.lower() if w.tag_ not in (
                                'NNP', 'NNPS') else w.text_with_ws
                            for w in word.subtree).strip()
                    else:
                        adverb['aft'] = ''.join(
                            w.text_with_ws.lower() if w.tag_ not in (
                                'NNP', 'NNPS') else w.text_with_ws
                            for w in word.subtree).strip()
            if word.dep_ == 'auxpass':
                if word.head.dep_ == 'ROOT':
                    if not subjpass:
                        subjpass = subj
            if word.dep_ in ('aux', 'auxpass', 'neg'):
                if word.head.dep_ == 'ROOT':
                    aux += [word]
            if word.dep_ == 'ROOT':
                verb = word.text
                if word.tag_ == 'VB':
                    verbtense = en.INFINITIVE
                elif word.tag_ == 'VBD':
                    verbtense = en.PAST
                elif word.tag_ == 'VBG':
                    verbtense = en.PRESENT
                    verbaspect = en.PROGRESSIVE
                elif word.tag_ == 'VBN':
                    verbtense = en.PAST
                else:
                    try:
                        verbtense = en.tenses(word.text)[0][0]
                    except IndexError:
                        pass
            if word.dep_ == 'prt':
                if word.head.dep_ == 'ROOT':
                    part = ''.join(w.text_with_ws.lower() if w.tag_ not in (
                        'NNP', 'NNPS') else w.text_with_ws
                                   for w in word.subtree).strip()
            if word.dep_ == 'prep':
                if word.head.dep_ == 'ROOT':
                    prep = ''.join(w.text_with_ws.lower() if w.tag_ not in (
                        'NNP', 'NNPS') else w.text_with_ws
                                   for w in word.subtree).strip()
            if word.dep_.endswith('obj'):
                if word.head.dep_ == 'agent':
                    if word.head.head.dep_ == 'ROOT':
                        agent = ''.join(
                            w.text + ', ' if w.dep_ == 'appos' else (
                                w.text_with_ws.lower() if w.tag_ not in (
                                    'NNP', 'NNPS') else w.text_with_ws)
                            for w in word.subtree).strip()
                        aplural = word.tag_ in ('NNS', 'NNPS')
            if word.dep_ in ('xcomp', 'ccomp', 'conj'):
                if word.head.dep_ == 'ROOT':
                    xcomp = ''.join(w.text_with_ws.lower() if w.tag_ not in (
                        'NNP', 'NNPS') else w.text_with_ws
                                    for w in word.subtree).strip()
                    that = xcomp.startswith('that')
                    xcomp = pass2act(xcomp, True).strip(' .')
                    if not xcomp.startswith('that') and that:
                        xcomp = 'that ' + xcomp
            if word.dep_ == 'punct' and not rec:
                if word.text != '"':
                    punc = word.text

        # exit if not passive:
        if subjpass == '':
            newdoc += str(sent) + ' '
            continue

        # if no agent is found:
        if agent == '':
            # what am I gonna do? BITconEEEEEEECT!!!!
            newdoc += str(sent) + ' '
            continue

        # invert nouns:
        agent = nouninv(agent)
        subjpass = nouninv(subjpass)

        # F*****G CONJUGATION!!!!!!!!!!!!!:
        auxstr = ''
        num = en.SINGULAR if not aplural or agent in ('he',
                                                      'she') else en.PLURAL
        aux.append(aux[0])
        verbaspect = None
        for (pp, p, a, n) in zip(aux, aux[1:], aux[2:], aux[3:]):
            if a.lemma_ == '.':
                continue

            if a.lemma_ == 'not':
                if p.lemma_ == 'be':
                    if n.lemma_ == 'be':
                        verbtense = en.tenses(a.text)[0][0]
                        auxstr += en.conjugate('be',
                                               tense=en.tenses(p.text)[0][0],
                                               number=num) + ' '
                        verbaspect = en.PROGRESSIVE
                    else:
                        auxstr += en.conjugate('do',
                                               tense=en.tenses(p.text)[0][0],
                                               number=num) + ' '
                        verbtense = en.INFINITIVE
                auxstr += 'not '
            elif a.lemma_ == 'be':
                if p.lemma_ == 'be':
                    verbtense = en.tenses(a.text)[0][0]
                    auxstr += en.conjugate(
                        'be', tense=en.tenses(a.text)[0][0], number=num) + ' '
                    verbaspect = en.PROGRESSIVE
                elif p.tag_ == 'MD':
                    verbtense = en.INFINITIVE
            elif a.lemma_ == 'have':
                num == en.PLURAL if p.tag_ == 'MD' else num
                auxstr += en.conjugate(
                    'have', tense=en.tenses(a.text)[0][0], number=num) + ' '
                if n.lemma_ == 'be':
                    verbaspect = en.PROGRESSIVE
                    verbtense = en.tenses(n.text)[0][0]
            else:
                auxstr += a.text_with_ws
        auxstr = auxstr.lower().strip()

        if verbaspect:
            verb = en.conjugate(verb, tense=verbtense, aspect=verbaspect)
        else:
            verb = en.conjugate(verb, tense=verbtense)

        advcl = ''
        if advcltree:
            for w in advcltree:
                if w.pos_ == 'VERB' and en.tenses(
                        w.text)[0][4] == en.PROGRESSIVE:
                    advcl += 'which ' + en.conjugate(
                        w.text, tense=en.tenses(verb)[0][0]) + ' '
                else:
                    advcl += w.text_with_ws

        newsent = ' '.join(
            list(
                filter(None, [
                    agent, auxstr, adverb['bef'], verb, part, subjpass,
                    adverb['aft'], advcl, prep, xcomp
                ]))) + punc
        if not rec:
            newsent = newsent[0].upper() + newsent[1:]
        newdoc += newsent + ' '
    return newdoc
Пример #12
0
 def get_lemma(self, verb=""):
     return en.conjugate(verb, tense="infinitive")
Пример #13
0
 def past_tence(v):
     if v == 'bite':
         return 'bitten'
     elif v == 'found':
         return 'founded'
     return conjugate(verb=v, tense=PAST, number=SG)  # he / she / it