Ejemplo n.º 1
0
def correct_i_compl(phrase, verb):
    """
    transform indirect complement to relative
    Input=sentence and verbal class         Output=sentence and verbal class         
    """

    #If we have a direct transitive verb
    if verb in ResourcePool().direct_transitive:

        #init
        x = 0
        while x < len(phrase):
            #If there is a proposal with an adverbial
            if x + 1 < len(phrase) and phrase[x] in ResourcePool().compelement_proposals:
                #If there is a plural
                phrase = phrase[:x] + analyse_nominal_group.find_plural(phrase[x:])

                if analyse_nominal_group.find_sn_pos(phrase, x + 1):
                    adverbial = analyse_nominal_group.find_sn_pos(phrase, x + 1)
                    begin_pos = x - 1

                    #We will find the subject of the relative
                    while not analyse_nominal_group.find_sn_pos(phrase, begin_pos):
                        begin_pos -= 1
                    nom_gr = analyse_nominal_group.find_sn_pos(phrase, begin_pos)

                    #If there nominal group is just before the adverbial
                    if begin_pos + len(nom_gr) == x:
                        phrase = phrase[:x] + ['which', 'is'] + [phrase[x]] + adverbial + [';'] + phrase[x + len(
                            adverbial) + 1:]
            x += 1
    return phrase
Ejemplo n.º 2
0
def correct_i_compl(phrase, verb):
    """
    transform indirect complement to relative
    Input=sentence and verbal class         Output=sentence and verbal class         
    """

    #If we have a direct transitive verb
    if verb in ResourcePool().direct_transitive:

        #init
        x = 0
        while x < len(phrase):
            #If there is a proposal with an adverbial
            if x + 1 < len(phrase) and phrase[x] in ResourcePool(
            ).compelement_proposals:
                #If there is a plural
                phrase = phrase[:x] + analyse_nominal_group.find_plural(
                    phrase[x:])

                if analyse_nominal_group.find_sn_pos(phrase, x + 1):
                    adverbial = analyse_nominal_group.find_sn_pos(
                        phrase, x + 1)
                    begin_pos = x - 1

                    #We will find the subject of the relative
                    while not analyse_nominal_group.find_sn_pos(
                            phrase, begin_pos):
                        begin_pos -= 1
                    nom_gr = analyse_nominal_group.find_sn_pos(
                        phrase, begin_pos)

                    #If there nominal group is just before the adverbial
                    if begin_pos + len(nom_gr) == x:
                        phrase = phrase[:x] + ['which', 'is'] + [
                            phrase[x]
                        ] + adverbial + [';'] + phrase[x + len(adverbial) + 1:]
            x += 1
    return phrase
Ejemplo n.º 3
0
def other_sentence(type, request, sentence):
    """
    process the other from of a sentence                                
    Input=type and requesting of sentence and the sentence                               
    Output=class Sentence                                                            
    """

    #init
    vg = VerbalGroup([], [], '', [], [], [], [], VerbalGroup.affirmative, [])
    analysis = Sentence(type, request, [], [])
    modal = []

    if not sentence:
        return []

    #We have to add punctuation if there is not
    if sentence[len(sentence) - 1] not in ['.', '?', '!']:
        sentence = sentence + ['.']

    #We start with determination of probably second verb in subsentence
    sentence = other_functions.find_scd_verb_sub(sentence)

    #We search the subject
    sbj = analyse_nominal_group.find_sn_pos(sentence, 0)

    if sbj != [] or type == RELATIVE:
        #If we haven't a data type => it is a statement
        if type == '':
            analysis.data_type = STATEMENT


        #We have to separate the case using these, this or there
        if sentence[0] in ResourcePool().demonstrative_det and analyse_verb.infinitive([sentence[1]],
                                                                                       'present simple') == ['be']:
            #We recover this information and remove it
            analysis.sn = [NominalGroup([sentence[0]], [], [], [], [])]
            if sentence[0] == 'there' and sentence[1] == 'are':
                analysis.sn[0]._quantifier = 'SOME'
            sentence = sentence[1:]

        if not analysis.sn:
            #We recover the subject
            sentence = analyse_nominal_structure.recover_ns(sentence, analysis, 0)

        #End of the sentence? -> nominal sentence
        if sentence == [] or sentence[0] in ['.', '!', '?']:
            analysis.sv = []
            return analysis

        #We have to know if there is a modal
        if sentence[0] in ResourcePool().modal:
            modal = sentence[0]
            if modal == 'can' or modal == 'must' or modal == 'shall' or modal == 'may':
                sentence = sentence[1:]

        #We must take into account all possible cases to recover the sentence's tense
        if len(sentence) > 1 and sentence[1] == 'not':
            vg.state = VerbalGroup.negative

            #Before the negative form we have an auxiliary for the negation
            if sentence[0] == 'do' or sentence[0] == 'does' or sentence[0] == 'did':
                vg.vrb_tense = analyse_verb.find_tense_statement([sentence[0]])
                sentence = sentence[2:]
                sentence = analyse_verbal_structure.delete_unusable_word(sentence)
                sentence = analyse_verbal_structure.find_vrb_adv(sentence, vg)

            #There is a modal
            elif modal:
                sentence = [sentence[0]] + sentence[2:]
                sentence = analyse_verbal_structure.delete_unusable_word(sentence)
                sentence = analyse_verbal_structure.find_vrb_adv(sentence, vg)
                vg.vrb_tense = analyse_verb.find_tense_statement(sentence)

            else:
                #We remove 'not' and find the tense
                sentence = sentence[:1] + sentence[2:]
                sentence = analyse_verbal_structure.delete_unusable_word(sentence)
                sentence = analyse_verbal_structure.find_vrb_adv(sentence, vg)
                vg.vrb_tense = analyse_verb.find_tense_statement(sentence)

        #For the affirmative processing
        else:
            if sentence[0] == 'not':
                vg.state = VerbalGroup.negative
                sentence = sentence[1:]

            sentence = analyse_verbal_structure.delete_unusable_word(sentence)
            sentence = analyse_verbal_structure.find_vrb_adv(sentence, vg)
            vg.vrb_tense = analyse_verb.find_tense_statement(sentence)

        verb = analyse_verb.find_verb_statement(sentence, vg.vrb_tense)
        verb_main = analyse_verb.return_verb(sentence, verb, vg.vrb_tense)
        vg.vrb_main = [other_functions.convert_to_string(verb_main)]

        #We delete the verb
        sentence = sentence[sentence.index(verb[0]) + len(verb_main):]

        #We perform the processing with the modal
        if modal:
            vg.vrb_main = [modal + '+' + vg.vrb_main[0]]

    #This is a imperative form
    else:
        #re-init
        analysis.data_type = IMPERATIVE
        vg.vrb_tense = 'present simple'

        if sentence[0] in ResourcePool().proposals:
            sentence = ['.'] + sentence

        #Negative form
        if sentence[1] == 'not':
            sentence = sentence[sentence.index('not') + 1:]
            sentence = analyse_verbal_structure.delete_unusable_word(sentence)
            sentence = analyse_verbal_structure.find_vrb_adv(sentence, vg)
            vg.state = VerbalGroup.negative
        else:
            sentence = analyse_verbal_structure.delete_unusable_word(sentence)
            sentence = analyse_verbal_structure.find_vrb_adv(sentence, vg)

        #We process the verb
        verb = [sentence[0]]
        verb_main = analyse_verb.return_verb(sentence, verb, vg.vrb_tense)
        vg.vrb_main = [other_functions.convert_to_string(verb_main)]

        #We delete the verb
        sentence = sentence[sentence.index(verb[0]) + len(verb_main):]

    if sentence and sentence[-1] == '?':
        analysis.data_type = YES_NO_QUESTION

    #We recover the conjunctive subsentence
    sentence = analyse_verbal_structure.process_conjunctive_sub(sentence, vg)

    #It verifies if there is a secondary verb
    sec_vrb = analyse_verbal_structure.find_scd_vrb(sentence)
    if sec_vrb:
        sentence = analyse_verbal_structure.process_scd_sentence(sentence, vg, sec_vrb)

    #We recover the subsentence
    sentence = analyse_verbal_structure.process_subsentence(sentence, vg)

    if sentence != [] and vg.vrb_main != []:
        #Process relative changes
        sentence = analyse_verbal_structure.correct_i_compl(sentence, vg.vrb_main[0])

    sentence = analyse_verbal_structure.process_compare(sentence, vg)

    sentence = analyse_nominal_group.find_plural(sentence)
    #We recover the direct, indirect complement and the adverbial
    sentence = analyse_verbal_structure.recover_obj_iobj(sentence, vg)

    #We have to take off abverbs form the sentence
    sentence = analyse_verbal_structure.find_adv(sentence, vg)

    #In case there is a state verb followed by an adjective
    sentence = analyse_verbal_structure.state_adjective(sentence, vg)

    #If there is a forgotten
    sentence = analyse_verbal_structure.find_vrb_adv(sentence, vg)

    vg = analyse_verbal_structure.DOC_to_IOC(vg)

    while len(sentence) > 1:
        stc = analyse_verbal_structure.create_nom_gr(sentence, request)
        #We recover the direct, indirect complement and the adverbial
        stc = analyse_verbal_structure.recover_obj_iobj(stc, vg)
        if stc == sentence:
            #We leave the loop
            break
        else:
            sentence = stc

    vg = analyse_verbal_structure.refine_indirect_complement(vg)
    vg = analyse_verbal_structure.refine_subsentence(vg)
    vg = analyse_verbal_structure.DOC_to_IOC(vg)

    analysis.sv = [vg]
    return analysis
Ejemplo n.º 4
0
def y_n_ques(type, request, sentence):
    """
    process the yes or no question from of a sentence
    Input=type and requesting of sentence and the sentence                           
    Output=class Sentence                                                            
    """

    #init
    vg = VerbalGroup([], [], '', [], [], [], [], VerbalGroup.affirmative, [])
    analysis = Sentence(type, request, [], [])
    modal = []
    stc = sentence

    #We start with determination of probably second verb in subsentence
    sentence = other_functions.find_scd_verb_sub(sentence)

    #We have to add punctuation if there is not
    if sentence == [] or sentence[0] == '.' or sentence[0] == '?' or sentence[0] == '!':
        #We have probably the aim as an adverb
        analyse_verbal_structure.find_adv([request], vg)
        analysis.aim = 'thing'
        analysis.sv = [vg]
        return analysis

    #We recover the auxiliary 
    aux = sentence[0]

    #We have to know if there is a modal
    if aux in ResourcePool().modal:
        modal = aux

    #If we have a negative form
    if sentence[1] == 'not':
        vg.state = VerbalGroup.negative
        #We remove 'not'
        sentence = sentence[:1] + sentence[2:]

    #Wrong is a noun but not followed by the determinant
    if sentence[1] == 'wrong' and request == 'thing':
        analysis.sn = [NominalGroup([], [], ['wrong'], [], [])]
        sentence = [sentence[0]] + sentence[2:]

    #In this case we have an imperative sentence
    elif analyse_nominal_group.find_sn_pos(sentence, 1) == [] and type != W_QUESTION:
        #We have to reput the 'not'
        if vg.state == VerbalGroup.negative:
            sentence = sentence[:1] + ['not'] + sentence[1:]
        return other_sentence(type, request, sentence)

    #We delete the auxiliary
    sentence = sentence[1:]

    #We have to separate the case using these, this or there
    if sentence[0] in ResourcePool().demonstrative_det and analyse_verb.infinitive([aux], 'present simple') == ['be']:
        #If we have a verb or an adverb just after (if not, we have a noun)
        if sentence[0].endswith('ed') or sentence[0].endswith('ing') or sentence[0].endswith('ly') or sentence[
            0] in ResourcePool().adverbs:
            #We recover this information and remove it
            analysis.sn = [NominalGroup([sentence[0]], [], [], [], [])]
            if sentence[0] == 'there' and aux == 'are':
                analysis.sn[0]._quantifier = 'SOME'
            sentence = sentence[1:]

    if not analysis.sn:
        #We recover the subject
        sentence = analyse_nominal_structure.recover_ns(sentence, analysis, 0)

    if aux == 'do' and not analyse_verbal_structure.can_be_imperative(sentence):
        return other_sentence('', '', stc)

    #If there is one element => it is an auxiliary => verb 'be'
    if len(sentence) == 0:
        vg.vrb_tense = analyse_verb.find_tense_statement(aux)
        vg.vrb_main = ['be']
    else:
        sentence = analyse_verbal_structure.delete_unusable_word(sentence)
        sentence = analyse_verbal_structure.find_vrb_adv(sentence, vg)
        vg.vrb_tense = analyse_verb.find_tense_question(sentence, aux)

        #We process the verb
        verb = analyse_verb.find_verb_question(sentence, aux, vg.vrb_tense)
        verb_main = analyse_verb.return_verb(sentence, verb, vg.vrb_tense)
        vg.vrb_main = [other_functions.convert_to_string(verb_main)]

        #We delete the verb if the aux is not the verb 'be'
        if vg.vrb_main != ['be']:
            sentence = sentence[sentence.index(verb[0]) + len(verb_main):]
        elif sentence[0] == 'be':
            sentence = sentence[1:]

        #Here we have special processing for different cases
        if sentence:
            #For 'what' descrition case
            if sentence[0] == 'like' and aux != 'would':
                vg.vrb_main = ['like']
                sentence = sentence[1:]

            #For 'how' questions with often
            elif sentence[0].endswith('ing') and not (sentence[0].endswith('thing')):
                vg.vrb_main[0] = vg.vrb_main[0] + '+' + sentence[0]
                sentence = sentence[1:]

        #We recover the conjunctive subsentence
        sentence = analyse_verbal_structure.process_conjunctive_sub(sentence, vg)

        #It verifies if there is a secondary verb
        sec_vrb = analyse_verbal_structure.find_scd_vrb(sentence)
        if sec_vrb:
            sentence = analyse_verbal_structure.process_scd_sentence(sentence, vg, sec_vrb)

        #We recover the subsentence
        sentence = analyse_verbal_structure.process_subsentence(sentence, vg)

        #Process relative changes
        sentence = analyse_verbal_structure.correct_i_compl(sentence, vg.vrb_main[0])

        sentence = analyse_verbal_structure.process_compare(sentence, vg)

        sentence = analyse_nominal_group.find_plural(sentence)
        #We recover the direct, indirect complement and the adverbial
        sentence = analyse_verbal_structure.recover_obj_iobj(sentence, vg)

        #We have to take off adverbs form the sentence
        sentence = analyse_verbal_structure.find_adv(sentence, vg)

    #We perform the processing with the modal
    if modal:
        vg.vrb_main = [modal + '+' + vg.vrb_main[0]]

    #If there is a forgotten
    sentence = analyse_verbal_structure.find_vrb_adv(sentence, vg)

    #In case there is a state verb followed by an adjective
    sentence = analyse_verbal_structure.state_adjective(sentence, vg)

    #We have to correct the mistake of the subject
    for p in ResourcePool().demonstrative_det:
        if analysis.sn and analysis.sn[0].det == [p] and analysis.sn[0].noun == []:
            if sentence != [0] and sentence[0] == '.' and sentence[0] == '?' and sentence[0] == '!':
                if sentence[0] in ResourcePool().proposals:
                    pass
                else:
                    analysis.sn[0].noun = [sentence[0]]
                    sentence = sentence[1:]
                    sentence = analyse_verbal_structure.state_adjective(sentence, vg)

    vg = analyse_verbal_structure.DOC_to_IOC(vg)

    while len(sentence) > 1:
        stc = analyse_verbal_structure.create_nom_gr(sentence, request)
        #We recover the direct, indirect complement and the adverbial
        stc = analyse_verbal_structure.recover_obj_iobj(stc, vg)
        if stc == sentence:
            #We leave the loop
            break
        else:
            sentence = stc

    vg = analyse_verbal_structure.refine_indirect_complement(vg)
    vg = analyse_verbal_structure.refine_subsentence(vg)
    vg = analyse_verbal_structure.DOC_to_IOC(vg)

    analysis.sv = [vg]
    return analysis
Ejemplo n.º 5
0
def process_compare(sentence, vg):
    """
    process the compare
    Input=sentence and verbal structure      Output=sentence verbal structure        
    """

    #init
    i = 0
    conjunction = 'AND'
    gr_nom_list = []

    while i < len(sentence):
        #We will find 'than'
        if sentence[i] == 'than':
            compare = {'nom_gr': [], 'object': ''}

            object = analyse_nominal_group.find_sn_pos(sentence, i + 1)
            #It reproduces the same code as above
            while object:
                #We refine the nominal group if there is an error like ending with question mark
                object = analyse_nominal_group.refine_nom_gr(object)
                #Recovering nominal group
                gr_nom_list = gr_nom_list + [
                    analyse_nominal_structure.fill_nom_gr(sentence, object, i + 1, conjunction)]
                #We take off the nominal group
                sentence = analyse_nominal_group.take_off_nom_gr(sentence, object, i + 1)
                conjunction = 'AND'
                #If there is a relative
                begin_pos_rel = analyse_nominal_group.find_relative(object, sentence, i + 1, ResourcePool().relatives)
                if begin_pos_rel != -1:
                    end_pos_rel = other_functions.recover_end_pos_sub(sentence, ResourcePool().relatives)
                    #We remove the relative part of the sentence
                    sentence = sentence[:begin_pos_rel] + sentence[end_pos_rel:]
                if len(sentence) != i + 1 and (
                            sentence[i + 1] == 'and' or sentence[i + 1] == 'or' or sentence[i + 1] == ':but'):
                    sentence = [sentence[i + 1]] + analyse_nominal_group.find_plural(sentence[1:])
                    object = analyse_nominal_group.find_sn_pos(sentence[i + 2:], i + 1)
                    #We process the 'or' like the 'and' and remove it
                    if sentence[i + 1] == 'or':
                        conjunction = 'OR'
                    elif sentence[i + 1] == ':but':
                        conjunction = 'BUT'
                    else:
                        conjunction = 'AND'
                    sentence = sentence[i + 1:]
                else:
                    object = []

            #Add the nominal group
            compare['nom_gr'] = gr_nom_list

            #Comparator : ends with 'er'
            if sentence[i - 1].endswith('er'):
                compare['object'] = sentence[i - 1]
                sentence = sentence[:i - 1] + sentence[i + 1:]

            #Comparator : with 2 words
            elif sentence[i - 2] == 'more' or sentence[i - 2] == 'less':
                compare['object'] = sentence[i - 2] + '+' + sentence[i - 1]
                sentence = sentence[:i - 1] + sentence[i + 1:]

            #Comparator : exceptions
            elif sentence[i - 1] == 'more' or sentence[i - 1] == 'less':
                compare['object'] = sentence[i - 1]
                sentence = sentence[:i - 1] + sentence[i + 1:]

            vg.comparator = vg.comparator + [compare]
        i += 1
    return sentence
Ejemplo n.º 6
0
def recover_obj_iobj(phrase, vg):
    """
    finds the direct, indirect object and the adverbial                
    We, also, put these information in the class                                     
    Input=sentence and verbal class              Output=sentence and verbal class    
    """

    #init
    conjunction = 'AND'

    object = analyse_nominal_group.find_sn(phrase)

    if phrase and \
            not object and \
                    phrase[0] in ResourcePool().adverbs_at_end:
        vg.advrb = [phrase[0]]

    while object:

        #If it is not a direct object => there is a proposal
        proposal = check_proposal(phrase, object)

        if proposal:

            gr_nom_list = []
            #This 'while' is for duplicate with 'and'
            while object:
                pos_object = phrase.index(object[0])

                #We refine the nominal group if there is an error like ending with question mark
                object = analyse_nominal_group.refine_nom_gr(object)

                #Recovering nominal group
                gr_nom_list = gr_nom_list + [
                    analyse_nominal_structure.fill_nom_gr(phrase, object, pos_object, conjunction)]

                #We take off the nominal group
                phrase = analyse_nominal_group.take_off_nom_gr(phrase, object, pos_object)
                #We will take off the proposal
                phrase = phrase[:phrase.index(proposal[0])] + phrase[phrase.index(proposal[0]) + 1:]
                conjunction = 'AND'

                #If there is a relative
                begin_pos_rel = analyse_nominal_group.find_relative(object, phrase, pos_object,
                                                                    ResourcePool().relatives)

                if begin_pos_rel != -1:
                    end_pos_rel = other_functions.recover_end_pos_sub(phrase, ResourcePool().relatives)
                    #We remove the relative part of the phrase
                    phrase = phrase[:begin_pos_rel] + phrase[end_pos_rel:]

                #If there is 'and', we need to duplicate the information with the proposal if there is
                if len(phrase) != 0 and (phrase[0] == 'and' or phrase[0] == 'or' or phrase[0] == ':but'):

                    phrase = [phrase[0]] + analyse_nominal_group.find_plural(phrase[1:])

                    #We have not duplicate the proposal, it depends on the presence of the nominal group after  
                    if analyse_nominal_group.find_sn_pos(phrase, 1):
                        phrase = [phrase[0]] + proposal + phrase[1:]
                    else:
                        phrase = [phrase[0]] + phrase[1:]

                    object = analyse_nominal_group.find_sn_pos(phrase[1:], 0)

                    #We process the 'or' like the 'and' and remove it
                    if phrase[0] == 'or':
                        conjunction = 'OR'
                    elif phrase[0] == ':but':
                        conjunction = 'BUT'
                    else:
                        conjunction = 'AND'
                    phrase = phrase[1:]

                else:
                    object = []

            vg.i_cmpl = vg.i_cmpl + [Indirect_Complement(proposal, gr_nom_list)]

        else:
            #It is a direct complement
            gr_nom_list = []
            #It reproduces the same code as above
            while object:
                pos_object = phrase.index(object[0])

                #We refine the nominal group if there is an error like ending with question mark
                object = analyse_nominal_group.refine_nom_gr(object)

                #Recovering nominal group
                gr_nom_list = gr_nom_list + [
                    analyse_nominal_structure.fill_nom_gr(phrase, object, pos_object, conjunction)]

                #We take off the nominal group
                phrase = analyse_nominal_group.take_off_nom_gr(phrase, object, pos_object)
                conjunction = 'AND'

                #If there is a relative
                begin_pos_rel = analyse_nominal_group.find_relative(object, phrase, pos_object,
                                                                    ResourcePool().relatives)

                if begin_pos_rel != -1:
                    end_pos_rel = other_functions.recover_end_pos_sub(phrase, ResourcePool().relatives)
                    #We remove the relative part of the phrase
                    phrase = phrase[:begin_pos_rel] + phrase[end_pos_rel:]

                if len(phrase) != 0 and (phrase[0] == 'and' or phrase[0] == 'or' or phrase[0] == ':but'):

                    phrase = [phrase[0]] + analyse_nominal_group.find_plural(phrase[1:])
                    object = analyse_nominal_group.find_sn_pos(phrase[1:], 0)

                    #We process the 'or' like the 'and' and remove it
                    if phrase[0] == 'or':
                        conjunction = 'OR'
                    elif phrase[0] == ':but':
                        conjunction = 'BUT'
                    else:
                        conjunction = 'AND'
                    phrase = phrase[1:]

                else:
                    object = []

            #In a sentence there is just one direct complement if there is no second verb
            if not vg.d_obj:
                vg.d_obj = gr_nom_list
            else:
                #Else the first nominal group found is indirect and this one is direct complement
                vg.i_cmpl = vg.i_cmpl + [Indirect_Complement([], vg.d_obj)]
                vg.d_obj = gr_nom_list

        #If the last nominal group is followed by another one in plural form 
        phrase = analyse_nominal_group.find_plural(phrase)
        object = analyse_nominal_group.find_sn(phrase)

    return phrase
Ejemplo n.º 7
0
def process_compare(sentence, vg):
    """
    process the compare
    Input=sentence and verbal structure      Output=sentence verbal structure        
    """

    #init
    i = 0
    conjunction = 'AND'
    gr_nom_list = []

    while i < len(sentence):
        #We will find 'than'
        if sentence[i] == 'than':
            compare = {'nom_gr': [], 'object': ''}

            object = analyse_nominal_group.find_sn_pos(sentence, i + 1)
            #It reproduces the same code as above
            while object:
                #We refine the nominal group if there is an error like ending with question mark
                object = analyse_nominal_group.refine_nom_gr(object)
                #Recovering nominal group
                gr_nom_list = gr_nom_list + [
                    analyse_nominal_structure.fill_nom_gr(
                        sentence, object, i + 1, conjunction)
                ]
                #We take off the nominal group
                sentence = analyse_nominal_group.take_off_nom_gr(
                    sentence, object, i + 1)
                conjunction = 'AND'
                #If there is a relative
                begin_pos_rel = analyse_nominal_group.find_relative(
                    object, sentence, i + 1,
                    ResourcePool().relatives)
                if begin_pos_rel != -1:
                    end_pos_rel = other_functions.recover_end_pos_sub(
                        sentence,
                        ResourcePool().relatives)
                    #We remove the relative part of the sentence
                    sentence = sentence[:begin_pos_rel] + sentence[end_pos_rel:]
                if len(sentence) != i + 1 and (sentence[i + 1] == 'and'
                                               or sentence[i + 1] == 'or'
                                               or sentence[i + 1] == ':but'):
                    sentence = [
                        sentence[i + 1]
                    ] + analyse_nominal_group.find_plural(sentence[1:])
                    object = analyse_nominal_group.find_sn_pos(
                        sentence[i + 2:], i + 1)
                    #We process the 'or' like the 'and' and remove it
                    if sentence[i + 1] == 'or':
                        conjunction = 'OR'
                    elif sentence[i + 1] == ':but':
                        conjunction = 'BUT'
                    else:
                        conjunction = 'AND'
                    sentence = sentence[i + 1:]
                else:
                    object = []

            #Add the nominal group
            compare['nom_gr'] = gr_nom_list

            #Comparator : ends with 'er'
            if sentence[i - 1].endswith('er'):
                compare['object'] = sentence[i - 1]
                sentence = sentence[:i - 1] + sentence[i + 1:]

            #Comparator : with 2 words
            elif sentence[i - 2] == 'more' or sentence[i - 2] == 'less':
                compare['object'] = sentence[i - 2] + '+' + sentence[i - 1]
                sentence = sentence[:i - 1] + sentence[i + 1:]

            #Comparator : exceptions
            elif sentence[i - 1] == 'more' or sentence[i - 1] == 'less':
                compare['object'] = sentence[i - 1]
                sentence = sentence[:i - 1] + sentence[i + 1:]

            vg.comparator = vg.comparator + [compare]
        i += 1
    return sentence
Ejemplo n.º 8
0
def recover_obj_iobj(phrase, vg):
    """
    finds the direct, indirect object and the adverbial                
    We, also, put these information in the class                                     
    Input=sentence and verbal class              Output=sentence and verbal class    
    """

    #init
    conjunction = 'AND'

    object = analyse_nominal_group.find_sn(phrase)

    if phrase and \
            not object and \
                    phrase[0] in ResourcePool().adverbs_at_end:
        vg.advrb = [phrase[0]]

    while object:

        #If it is not a direct object => there is a proposal
        proposal = check_proposal(phrase, object)

        if proposal:

            gr_nom_list = []
            #This 'while' is for duplicate with 'and'
            while object:
                pos_object = phrase.index(object[0])

                #We refine the nominal group if there is an error like ending with question mark
                object = analyse_nominal_group.refine_nom_gr(object)

                #Recovering nominal group
                gr_nom_list = gr_nom_list + [
                    analyse_nominal_structure.fill_nom_gr(
                        phrase, object, pos_object, conjunction)
                ]

                #We take off the nominal group
                phrase = analyse_nominal_group.take_off_nom_gr(
                    phrase, object, pos_object)
                #We will take off the proposal
                phrase = phrase[:phrase.index(proposal[0])] + phrase[
                    phrase.index(proposal[0]) + 1:]
                conjunction = 'AND'

                #If there is a relative
                begin_pos_rel = analyse_nominal_group.find_relative(
                    object, phrase, pos_object,
                    ResourcePool().relatives)

                if begin_pos_rel != -1:
                    end_pos_rel = other_functions.recover_end_pos_sub(
                        phrase,
                        ResourcePool().relatives)
                    #We remove the relative part of the phrase
                    phrase = phrase[:begin_pos_rel] + phrase[end_pos_rel:]

                #If there is 'and', we need to duplicate the information with the proposal if there is
                if len(phrase) != 0 and (phrase[0] == 'and' or phrase[0]
                                         == 'or' or phrase[0] == ':but'):

                    phrase = [phrase[0]] + analyse_nominal_group.find_plural(
                        phrase[1:])

                    #We have not duplicate the proposal, it depends on the presence of the nominal group after
                    if analyse_nominal_group.find_sn_pos(phrase, 1):
                        phrase = [phrase[0]] + proposal + phrase[1:]
                    else:
                        phrase = [phrase[0]] + phrase[1:]

                    object = analyse_nominal_group.find_sn_pos(phrase[1:], 0)

                    #We process the 'or' like the 'and' and remove it
                    if phrase[0] == 'or':
                        conjunction = 'OR'
                    elif phrase[0] == ':but':
                        conjunction = 'BUT'
                    else:
                        conjunction = 'AND'
                    phrase = phrase[1:]

                else:
                    object = []

            vg.i_cmpl = vg.i_cmpl + [IndirectComplement(proposal, gr_nom_list)]

        else:
            #It is a direct complement
            gr_nom_list = []
            #It reproduces the same code as above
            while object:
                pos_object = phrase.index(object[0])

                #We refine the nominal group if there is an error like ending with question mark
                object = analyse_nominal_group.refine_nom_gr(object)

                #Recovering nominal group
                gr_nom_list = gr_nom_list + [
                    analyse_nominal_structure.fill_nom_gr(
                        phrase, object, pos_object, conjunction)
                ]

                #We take off the nominal group
                phrase = analyse_nominal_group.take_off_nom_gr(
                    phrase, object, pos_object)
                conjunction = 'AND'

                #If there is a relative
                begin_pos_rel = analyse_nominal_group.find_relative(
                    object, phrase, pos_object,
                    ResourcePool().relatives)

                if begin_pos_rel != -1:
                    end_pos_rel = other_functions.recover_end_pos_sub(
                        phrase,
                        ResourcePool().relatives)
                    #We remove the relative part of the phrase
                    phrase = phrase[:begin_pos_rel] + phrase[end_pos_rel:]

                if len(phrase) != 0 and (phrase[0] == 'and' or phrase[0]
                                         == 'or' or phrase[0] == ':but'):

                    phrase = [phrase[0]] + analyse_nominal_group.find_plural(
                        phrase[1:])
                    object = analyse_nominal_group.find_sn_pos(phrase[1:], 0)

                    #We process the 'or' like the 'and' and remove it
                    if phrase[0] == 'or':
                        conjunction = 'OR'
                    elif phrase[0] == ':but':
                        conjunction = 'BUT'
                    else:
                        conjunction = 'AND'
                    phrase = phrase[1:]

                else:
                    object = []

            #In a sentence there is just one direct complement if there is no second verb
            if not vg.d_obj:
                vg.d_obj = gr_nom_list
            else:
                #Else the first nominal group found is indirect and this one is direct complement
                vg.i_cmpl = vg.i_cmpl + [IndirectComplement([], vg.d_obj)]
                vg.d_obj = gr_nom_list

        #If the last nominal group is followed by another one in plural form
        phrase = analyse_nominal_group.find_plural(phrase)
        object = analyse_nominal_group.find_sn(phrase)

    return phrase
Ejemplo n.º 9
0
def upper_to_lower(sentence):
    """
    converts the upper case to lower case
    Input=sentence, beginning sentence list                  Output=sentence
    """

    # If the sentence begins with upper case
    if other_functions.find_cap_lettre(sentence[0]):

        # We convert upper case to lower case if it is not 'I'
        if sentence[0] == 'I':
            sentence = expand_contractions(sentence)
            return sentence
        else:
            sentence[0] = sentence[0][0].lower() + sentence[0][1:]

        #We make changes here because we need lower case and not upper case
        sentence = expand_contractions(sentence)
        sentence = adverbial_or_subsentence(sentence)
        stc = process_and_beginning_sentence(sentence)

        #If sentence is modified we can return it
        if stc != sentence:
            return stc

        #We find an action verb => it is an imperative sentence
        if sentence[0] in ThematicRolesDict().get_all_verbs():
            return sentence

        #If we find the word in the Beginning_sentence list
        for v in ResourcePool().sentence_starts:
            if sentence[0] == v[0]:
                return sentence

        #We find a number
        if other_functions.number(sentence[0]) == 1:
            return sentence

        #If there is plural
        sentence = analyse_nominal_group.find_plural(sentence)
        #If it still start with adjective
        if analyse_nominal_group.is_an_adj(sentence[0]) == 1:
            sentence = ['the'] + sentence

        #If there is a nominal group
        if analyse_nominal_group.find_sn_pos(sentence, 0):
            return sentence

        #Default case: we assume a proper name, we convert lowercase to uppercase
        sentence[0] = sentence[0][0].upper() + sentence[0][1:]

    # If the sentence begins with lower case
    else:
        #We make changes here because we need lower case and not upper case
        sentence = expand_contractions(sentence)
        sentence = adverbial_or_subsentence(sentence)
        sentence = process_and_beginning_sentence(sentence)

        #If we find the word in the Beginning_sentence list so we can return it
        for v in ResourcePool().sentence_starts:
            if sentence[0] == v[0]:
                return sentence

        #If there is plural
        sentence = analyse_nominal_group.find_plural(sentence)
        #If it still start with adjective
        if analyse_nominal_group.is_an_adj(sentence[0]) == 1:
            sentence = ['the'] + sentence

    return sentence
Ejemplo n.º 10
0
def other_sentence(type, request, sentence):
    """
    process the other from of a sentence                                
    Input=type and requesting of sentence and the sentence                               
    Output=class Sentence                                                            
    """

    #init
    vg = VerbalGroup([], [], '', [], [], [], [], VerbalGroup.affirmative, [])
    analysis = Sentence(type, request, [], [])
    modal = []

    if not sentence:
        return []

    #We have to add punctuation if there is not
    if sentence[len(sentence) - 1] not in ['.', '?', '!']:
        sentence = sentence + ['.']

    #We start with determination of probably second verb in subsentence
    sentence = other_functions.find_scd_verb_sub(sentence)

    #We search the subject
    sbj = analyse_nominal_group.find_sn_pos(sentence, 0)

    if sbj != [] or type == RELATIVE:
        #If we haven't a data type => it is a statement
        if type == '':
            analysis.data_type = STATEMENT

        #We have to separate the case using these, this or there
        if sentence[0] in ResourcePool(
        ).demonstrative_det and analyse_verb.infinitive(
            [sentence[1]], 'present simple') == ['be']:
            #We recover this information and remove it
            analysis.sn = [NominalGroup([sentence[0]], [], [], [], [])]
            if sentence[0] == 'there' and sentence[1] == 'are':
                analysis.sn[0]._quantifier = 'SOME'
            sentence = sentence[1:]

        if not analysis.sn:
            #We recover the subject
            sentence = analyse_nominal_structure.recover_ns(
                sentence, analysis, 0)

        #End of the sentence? -> nominal sentence
        if sentence == [] or sentence[0] in ['.', '!', '?']:
            analysis.sv = []
            return analysis

        #We have to know if there is a modal
        if sentence[0] in ResourcePool().modal:
            modal = sentence[0]
            if modal == 'can' or modal == 'must' or modal == 'shall' or modal == 'may':
                sentence = sentence[1:]

        #We must take into account all possible cases to recover the sentence's tense
        if len(sentence) > 1 and sentence[1] == 'not':
            vg.state = VerbalGroup.negative

            #Before the negative form we have an auxiliary for the negation
            if sentence[0] == 'do' or sentence[0] == 'does' or sentence[
                    0] == 'did':
                vg.vrb_tense = analyse_verb.find_tense_statement([sentence[0]])
                sentence = sentence[2:]
                sentence = analyse_verbal_structure.delete_unusable_word(
                    sentence)
                sentence = analyse_verbal_structure.find_vrb_adv(sentence, vg)

            #There is a modal
            elif modal:
                sentence = [sentence[0]] + sentence[2:]
                sentence = analyse_verbal_structure.delete_unusable_word(
                    sentence)
                sentence = analyse_verbal_structure.find_vrb_adv(sentence, vg)
                vg.vrb_tense = analyse_verb.find_tense_statement(sentence)

            else:
                #We remove 'not' and find the tense
                sentence = sentence[:1] + sentence[2:]
                sentence = analyse_verbal_structure.delete_unusable_word(
                    sentence)
                sentence = analyse_verbal_structure.find_vrb_adv(sentence, vg)
                vg.vrb_tense = analyse_verb.find_tense_statement(sentence)

        #For the affirmative processing
        else:
            if sentence[0] == 'not':
                vg.state = VerbalGroup.negative
                sentence = sentence[1:]

            sentence = analyse_verbal_structure.delete_unusable_word(sentence)
            sentence = analyse_verbal_structure.find_vrb_adv(sentence, vg)
            vg.vrb_tense = analyse_verb.find_tense_statement(sentence)

        verb = analyse_verb.find_verb_statement(sentence, vg.vrb_tense)
        verb_main = analyse_verb.return_verb(sentence, verb, vg.vrb_tense)
        vg.vrb_main = [other_functions.convert_to_string(verb_main)]

        #We delete the verb
        sentence = sentence[sentence.index(verb[0]) + len(verb_main):]

        #We perform the processing with the modal
        if modal:
            vg.vrb_main = [modal + '+' + vg.vrb_main[0]]

    #This is a imperative form
    else:
        #re-init
        analysis.data_type = IMPERATIVE
        vg.vrb_tense = 'present simple'

        if sentence[0] in ResourcePool().proposals:
            sentence = ['.'] + sentence

        #Negative form
        if sentence[1] == 'not':
            sentence = sentence[sentence.index('not') + 1:]
            sentence = analyse_verbal_structure.delete_unusable_word(sentence)
            sentence = analyse_verbal_structure.find_vrb_adv(sentence, vg)
            vg.state = VerbalGroup.negative
        else:
            sentence = analyse_verbal_structure.delete_unusable_word(sentence)
            sentence = analyse_verbal_structure.find_vrb_adv(sentence, vg)

        #We process the verb
        verb = [sentence[0]]
        verb_main = analyse_verb.return_verb(sentence, verb, vg.vrb_tense)
        vg.vrb_main = [other_functions.convert_to_string(verb_main)]

        #We delete the verb
        sentence = sentence[sentence.index(verb[0]) + len(verb_main):]

    if sentence and sentence[-1] == '?':
        analysis.data_type = YES_NO_QUESTION

    #We recover the conjunctive subsentence
    sentence = analyse_verbal_structure.process_conjunctive_sub(sentence, vg)

    #It verifies if there is a secondary verb
    sec_vrb = analyse_verbal_structure.find_scd_vrb(sentence)
    if sec_vrb:
        sentence = analyse_verbal_structure.process_scd_sentence(
            sentence, vg, sec_vrb)

    #We recover the subsentence
    sentence = analyse_verbal_structure.process_subsentence(sentence, vg)

    if sentence != [] and vg.vrb_main != []:
        #Process relative changes
        sentence = analyse_verbal_structure.correct_i_compl(
            sentence, vg.vrb_main[0])

    sentence = analyse_verbal_structure.process_compare(sentence, vg)

    sentence = analyse_nominal_group.find_plural(sentence)
    #We recover the direct, indirect complement and the adverbial
    sentence = analyse_verbal_structure.recover_obj_iobj(sentence, vg)

    #We have to take off abverbs form the sentence
    sentence = analyse_verbal_structure.find_adv(sentence, vg)

    #In case there is a state verb followed by an adjective
    sentence = analyse_verbal_structure.state_adjective(sentence, vg)

    #If there is a forgotten
    sentence = analyse_verbal_structure.find_vrb_adv(sentence, vg)

    vg = analyse_verbal_structure.DOC_to_IOC(vg)

    while len(sentence) > 1:
        stc = analyse_verbal_structure.create_nom_gr(sentence, request)
        #We recover the direct, indirect complement and the adverbial
        stc = analyse_verbal_structure.recover_obj_iobj(stc, vg)
        if stc == sentence:
            #We leave the loop
            break
        else:
            sentence = stc

    vg = analyse_verbal_structure.refine_indirect_complement(vg)
    vg = analyse_verbal_structure.refine_subsentence(vg)
    vg = analyse_verbal_structure.DOC_to_IOC(vg)

    analysis.sv = [vg]
    return analysis
Ejemplo n.º 11
0
def y_n_ques(type, request, sentence):
    """
    process the yes or no question from of a sentence
    Input=type and requesting of sentence and the sentence                           
    Output=class Sentence                                                            
    """

    #init
    vg = VerbalGroup([], [], '', [], [], [], [], VerbalGroup.affirmative, [])
    analysis = Sentence(type, request, [], [])
    modal = []
    stc = sentence

    #We start with determination of probably second verb in subsentence
    sentence = other_functions.find_scd_verb_sub(sentence)

    #We have to add punctuation if there is not
    if sentence == [] or sentence[0] == '.' or sentence[0] == '?' or sentence[
            0] == '!':
        #We have probably the aim as an adverb
        analyse_verbal_structure.find_adv([request], vg)
        analysis.aim = 'thing'
        analysis.sv = [vg]
        return analysis

    #We recover the auxiliary
    aux = sentence[0]

    #We have to know if there is a modal
    if aux in ResourcePool().modal:
        modal = aux

    #If we have a negative form
    if sentence[1] == 'not':
        vg.state = VerbalGroup.negative
        #We remove 'not'
        sentence = sentence[:1] + sentence[2:]

    #Wrong is a noun but not followed by the determinant
    if sentence[1] == 'wrong' and request == 'thing':
        analysis.sn = [NominalGroup([], [], ['wrong'], [], [])]
        sentence = [sentence[0]] + sentence[2:]

    #In this case we have an imperative sentence
    elif analyse_nominal_group.find_sn_pos(sentence,
                                           1) == [] and type != W_QUESTION:
        #We have to reput the 'not'
        if vg.state == VerbalGroup.negative:
            sentence = sentence[:1] + ['not'] + sentence[1:]
        return other_sentence(type, request, sentence)

    #We delete the auxiliary
    sentence = sentence[1:]

    #We have to separate the case using these, this or there
    if sentence[0] in ResourcePool(
    ).demonstrative_det and analyse_verb.infinitive(
        [aux], 'present simple') == ['be']:
        #If we have a verb or an adverb just after (if not, we have a noun)
        if sentence[0].endswith('ed') or sentence[0].endswith(
                'ing') or sentence[0].endswith(
                    'ly') or sentence[0] in ResourcePool().adverbs:
            #We recover this information and remove it
            analysis.sn = [NominalGroup([sentence[0]], [], [], [], [])]
            if sentence[0] == 'there' and aux == 'are':
                analysis.sn[0]._quantifier = 'SOME'
            sentence = sentence[1:]

    if not analysis.sn:
        #We recover the subject
        sentence = analyse_nominal_structure.recover_ns(sentence, analysis, 0)

    if aux == 'do' and not analyse_verbal_structure.can_be_imperative(
            sentence):
        return other_sentence('', '', stc)

    #If there is one element => it is an auxiliary => verb 'be'
    if len(sentence) == 0:
        vg.vrb_tense = analyse_verb.find_tense_statement(aux)
        vg.vrb_main = ['be']
    else:
        sentence = analyse_verbal_structure.delete_unusable_word(sentence)
        sentence = analyse_verbal_structure.find_vrb_adv(sentence, vg)
        vg.vrb_tense = analyse_verb.find_tense_question(sentence, aux)

        #We process the verb
        verb = analyse_verb.find_verb_question(sentence, aux, vg.vrb_tense)
        verb_main = analyse_verb.return_verb(sentence, verb, vg.vrb_tense)
        vg.vrb_main = [other_functions.convert_to_string(verb_main)]

        #We delete the verb if the aux is not the verb 'be'
        if vg.vrb_main != ['be']:
            sentence = sentence[sentence.index(verb[0]) + len(verb_main):]
        elif sentence[0] == 'be':
            sentence = sentence[1:]

        #Here we have special processing for different cases
        if sentence:
            #For 'what' descrition case
            if sentence[0] == 'like' and aux != 'would':
                vg.vrb_main = ['like']
                sentence = sentence[1:]

            #For 'how' questions with often
            elif sentence[0].endswith('ing') and not (
                    sentence[0].endswith('thing')):
                vg.vrb_main[0] = vg.vrb_main[0] + '+' + sentence[0]
                sentence = sentence[1:]

        #We recover the conjunctive subsentence
        sentence = analyse_verbal_structure.process_conjunctive_sub(
            sentence, vg)

        #It verifies if there is a secondary verb
        sec_vrb = analyse_verbal_structure.find_scd_vrb(sentence)
        if sec_vrb:
            sentence = analyse_verbal_structure.process_scd_sentence(
                sentence, vg, sec_vrb)

        #We recover the subsentence
        sentence = analyse_verbal_structure.process_subsentence(sentence, vg)

        #Process relative changes
        sentence = analyse_verbal_structure.correct_i_compl(
            sentence, vg.vrb_main[0])

        sentence = analyse_verbal_structure.process_compare(sentence, vg)

        sentence = analyse_nominal_group.find_plural(sentence)
        #We recover the direct, indirect complement and the adverbial
        sentence = analyse_verbal_structure.recover_obj_iobj(sentence, vg)

        #We have to take off adverbs form the sentence
        sentence = analyse_verbal_structure.find_adv(sentence, vg)

    #We perform the processing with the modal
    if modal:
        vg.vrb_main = [modal + '+' + vg.vrb_main[0]]

    #If there is a forgotten
    sentence = analyse_verbal_structure.find_vrb_adv(sentence, vg)

    #In case there is a state verb followed by an adjective
    sentence = analyse_verbal_structure.state_adjective(sentence, vg)

    #We have to correct the mistake of the subject
    for p in ResourcePool().demonstrative_det:
        if analysis.sn and analysis.sn[0].det == [
                p
        ] and analysis.sn[0].noun == []:
            if sentence != [0] and sentence[0] == '.' and sentence[
                    0] == '?' and sentence[0] == '!':
                if sentence[0] in ResourcePool().proposals:
                    pass
                else:
                    analysis.sn[0].noun = [sentence[0]]
                    sentence = sentence[1:]
                    sentence = analyse_verbal_structure.state_adjective(
                        sentence, vg)

    vg = analyse_verbal_structure.DOC_to_IOC(vg)

    while len(sentence) > 1:
        stc = analyse_verbal_structure.create_nom_gr(sentence, request)
        #We recover the direct, indirect complement and the adverbial
        stc = analyse_verbal_structure.recover_obj_iobj(stc, vg)
        if stc == sentence:
            #We leave the loop
            break
        else:
            sentence = stc

    vg = analyse_verbal_structure.refine_indirect_complement(vg)
    vg = analyse_verbal_structure.refine_subsentence(vg)
    vg = analyse_verbal_structure.DOC_to_IOC(vg)

    analysis.sv = [vg]
    return analysis
Ejemplo n.º 12
0
def upper_to_lower(sentence):
    """
    converts the upper case to lower case
    Input=sentence, beginning sentence list                  Output=sentence
    """

    #If the sentence begins with upper case
    if other_functions.find_cap_lettre(sentence[0]):

        #We convert upper case to lower case if it is not 'I'
        if sentence[0] == 'I':
            sentence = expand_contractions(sentence)
            return sentence
        else:
            sentence[0] = sentence[0][0].lower() + sentence[0][1:]

        #We make changes here because we need lower case and not upper case
        sentence = expand_contractions(sentence)
        sentence = adverbial_or_subsentence(sentence)
        stc = process_and_beginning_sentence(sentence)

        #If sentence is modified we can return it
        if stc != sentence:
            return stc

        #We find an action verb => it is an imperative sentence
        if sentence[0] in ThematicRolesDict().get_all_verbs():
            return sentence

        #If we find the word in the Beginning_sentence list
        for v in ResourcePool().sentence_starts:
            if sentence[0] == v[0]:
                return sentence

        #We find a number
        if other_functions.number(sentence[0]) == 1:
            return sentence

        #If there is plural
        sentence = analyse_nominal_group.find_plural(sentence)
        #If it still start with adjective
        if analyse_nominal_group.is_an_adj(sentence[0]) == 1:
            sentence = ['the'] + sentence

        #If there is a nominal group
        if analyse_nominal_group.find_sn_pos(sentence, 0):
            return sentence

        #Default case: we assume a proper name, we convert lowercase to uppercase
        sentence[0] = sentence[0][0].upper() + sentence[0][1:]

    #If the sentence begins with lower case
    else:
        #We make changes here because we need lower case and not upper case
        sentence = expand_contractions(sentence)
        sentence = adverbial_or_subsentence(sentence)
        sentence = process_and_beginning_sentence(sentence)

        #If we find the word in the Beginning_sentence list so we can return it
        for v in ResourcePool().sentence_starts:
            if sentence[0] == v[0]:
                return sentence

        #If there is plural
        sentence = analyse_nominal_group.find_plural(sentence)
        #If it still start with adjective
        if analyse_nominal_group.is_an_adj(sentence[0]) == 1:
            sentence = ['the'] + sentence

    return sentence