Esempio n. 1
0
def possesion_form(sentence):
    """
    convert 's to possession form 'of'
    Input=sentence                                     Output=sentence
    """

    # init
    begin_pos = 0
    flag = 0

    # We will find the possession case
    while begin_pos < len(sentence):

        #We found a posssession case
        if sentence[begin_pos].endswith("'s") or sentence[begin_pos].endswith(
                "s'"):

            #We have to find the first nominal group
            nominal_group = analyse_nominal_group.find_sn_pos(
                sentence, begin_pos)

            #In the case of a propre name
            while nominal_group != [] and begin_pos != 0 and other_functions.find_cap_lettre(
                    nominal_group[0]) == 1:
                begin_pos -= 1

                nominal_group = analyse_nominal_group.find_sn_pos(
                    sentence, begin_pos)
                flag = 1

            #If flag=1 => there is a propre name so we haven't decrement the begin_pos
            if flag == 0:
                while not nominal_group:
                    begin_pos -= 1
                    nominal_group = analyse_nominal_group.find_sn_pos(
                        sentence, begin_pos)

            else:
                #If there is a propre name, begin_pos is wrong, we have to increment
                begin_pos += 1
                flag = 0

            #We recover the list of nominal groups
            nominal_group_list = find_nominal_group_list(sentence[begin_pos:])
            #We create the final phrase
            end_pos = nominal_group_list[len(nominal_group_list) -
                                         1] + begin_pos
            sentence = sentence[:begin_pos] + create_possession_claus(
                nominal_group_list[:len(nominal_group_list) -
                                   1]) + sentence[end_pos:]

            #We continue processing from the end's position
            begin_pos = end_pos

        else:
            begin_pos += 1

    return sentence
Esempio n. 2
0
def process_and_beginning_sentence(sentence):
    """
    process the beginning of the sentence
    For example if there is adverb, we put it at the end (i_cmpl also)
    Input=sentence                                     Output=sentence
    """

    #If sentence is empty
    if not sentence:
        return []

    #Using flag is for the ending of this function
    if other_functions.find_cap_lettre(sentence[0]) == 1:
        flg = 1
        sentence[0] = sentence[0][0].lower() + sentence[0][1:]
    else:
        flg = 0

    #We have to take off this words
    if sentence[0] == ';' or sentence[0] == 'and' or sentence[0] == 'ah' or sentence[0] == ',' or sentence[0] == 'very':
        sentence = sentence[1:]

    #If it starts with proposal
    if sentence[0] in ResourcePool().proposals:
        gr = determination_nominal_group(sentence, 1, 'of')
        #We put the nominal group at the end of the sentence
        if gr:
            sentence = sentence[len(gr) + 1:] + [','] + [sentence[0]] + gr
            sentence = process_and_beginning_sentence(sentence)
        else:
            #In this case we don't find a nominal group but there is a i_cmpl
            for z in sentence:
                if z == ';' or z == '.' or z == ',':
                    #The final point of i_cmpl is the punctuation
                    phrase = sentence[:sentence.index(z)]
                    if sentence[len(sentence) - 1] == '.' or sentence[len(sentence) - 1] == '?' or sentence[
                                len(sentence) - 1] == '!':
                        sentence = sentence[sentence.index(z) + 1:len(sentence) - 1] + [','] + phrase + [
                            sentence[len(sentence) - 1]]
                    else:
                        sentence = sentence[sentence.index(z) + 1:] + [','] + phrase

    #If it starts with adverb
    if sentence[0] in ResourcePool().adverbs:
        if sentence[len(sentence) - 1] == '.' or sentence[len(sentence) - 1] == '?' or sentence[
                    len(sentence) - 1] == '!':
            sentence = sentence[1:len(sentence) - 1] + [sentence[0]] + [sentence[len(sentence) - 1]]
            sentence = process_and_beginning_sentence(sentence)
        else:
            sentence = sentence[1:] + [','] + [sentence[0]]
            sentence = process_and_beginning_sentence(sentence)

    #If flag is 1 => we have a capitol letter at the beginning
    if flg == 1:
        sentence[0] = sentence[0][0].upper() + sentence[0][1:]

    return sentence
Esempio n. 3
0
def possesion_form(sentence):
    """
    convert 's to possession form 'of'
    Input=sentence                                     Output=sentence
    """

    #init
    begin_pos = 0
    flag = 0

    #We will find the possession case
    while begin_pos < len(sentence):

        #We found a posssession case
        if sentence[begin_pos].endswith("'s") or sentence[begin_pos].endswith("s'"):

            #We have to find the first nominal group
            nominal_group = analyse_nominal_group.find_sn_pos(sentence, begin_pos)

            #In the case of a propre name
            while nominal_group != [] and begin_pos != 0 and other_functions.find_cap_lettre(nominal_group[0]) == 1:
                begin_pos -= 1

                nominal_group = analyse_nominal_group.find_sn_pos(sentence, begin_pos)
                flag = 1

            #If flag=1 => there is a propre name so we haven't decrement the begin_pos
            if flag == 0:
                while not nominal_group:
                    begin_pos -= 1
                    nominal_group = analyse_nominal_group.find_sn_pos(sentence, begin_pos)

            else:
                #If there is a propre name, begin_pos is wrong, we have to increment
                begin_pos += 1
                flag = 0

            #We recover the list of nominal groups
            nominal_group_list = find_nominal_group_list(sentence[begin_pos:])
            #We create the final phrase
            end_pos = nominal_group_list[len(nominal_group_list) - 1] + begin_pos
            sentence = sentence[:begin_pos] + create_possession_claus(
                nominal_group_list[:len(nominal_group_list) - 1]) + sentence[end_pos:]

            #We continue processing from the end's position
            begin_pos = end_pos

        else:
            begin_pos += 1

    return sentence
Esempio n. 4
0
def find_sn(sentence):
    """
    Returns the first nominal group found in the sentence.                              
    
    :param list sentence: the sentence as a list of words                                             
    :return: the nominal group                                                        
    """

    nb_position = 1

    #If sentence is empty
    if not sentence:
        return []

    for x in sentence:
        #If there is a pronoun
        if x in ResourcePool().pronouns:
            return [sentence[sentence.index(x)]]

        #If there is a nominal group with determinant
        if x in ResourcePool().determinants:
            nb_position += adjective_pos(sentence, sentence.index(x) + 1)
            return sentence[sentence.index(x):sentence.index(x) + nb_position]

        #If we have 'something'
        for k in ResourcePool().composed_nouns:
            if x.startswith(k):
                if x in ResourcePool().noun_not_composed:
                    return []
                return [sentence[sentence.index(x)]]

        #If there is a number, it will be the same with determinant
        if other_functions.number(x) == 1:
            nb_position += adjective_pos(sentence, sentence.index(x) + 1)
            return sentence[sentence.index(x):sentence.index(x) + nb_position]

        #If there is a proper name
        counter = sentence.index(x)
        while counter < len(sentence) and other_functions.find_cap_lettre(
                sentence[counter]) == 1:
            counter += 1
            #Not equal => there is a proper name
        if counter != sentence.index(x):
            return sentence[sentence.index(x):counter]

    #Default case
    return []
def find_sn(sentence):
    """
    Returns the first nominal group found in the sentence.                              
    
    :param list sentence: the sentence as a list of words                                             
    :return: the nominal group                                                        
    """

    nb_position = 1

    #If sentence is empty
    if not sentence:
        return []

    for x in sentence:
        #If there is a pronoun
        if x in ResourcePool().pronouns:
            return [sentence[sentence.index(x)]]

        #If there is a nominal group with determinant
        if x in ResourcePool().determinants:
            nb_position += adjective_pos(sentence, sentence.index(x) + 1)
            return sentence[sentence.index(x): sentence.index(x) + nb_position]

        #If we have 'something'
        for k in ResourcePool().composed_nouns:
            if x.startswith(k):
                if x in ResourcePool().noun_not_composed:
                    return []
                return [sentence[sentence.index(x)]]

        #If there is a number, it will be the same with determinant
        if other_functions.number(x) == 1:
            nb_position += adjective_pos(sentence, sentence.index(x) + 1)
            return sentence[sentence.index(x): sentence.index(x) + nb_position]

        #If there is a proper name
        counter = sentence.index(x)
        while counter < len(sentence) and other_functions.find_cap_lettre(sentence[counter]) == 1:
            counter += 1
            #Not equal => there is a proper name
        if counter != sentence.index(x):
            return sentence[sentence.index(x): counter]

    #Default case
    return []
Esempio n. 6
0
def find_sn_pos(sentence, begin_pos):
    """
    We will find the nominal group which is in a known position                      
    We have to use adjective_pos to return the end position of nominal group         

    :param list sentence: the sentence (list of strings)
    :param begin_pos:the position of the nominal group       
    :return: the nominal group (as a list of words)
    """

    if begin_pos >= len(sentence):
        return []

    end_pos = 1

    #If it is a pronoun
    if sentence[begin_pos] in ResourcePool().pronouns:
        return [sentence[begin_pos]]

    #If there is a nominal group with determinant
    if sentence[begin_pos] in ResourcePool().determinants:
        end_pos += adjective_pos(sentence, begin_pos + 1)
        return sentence[begin_pos:end_pos + begin_pos]

    #If we have 'something'
    for k in ResourcePool().composed_nouns:
        if sentence[begin_pos].startswith(k):
            if sentence[begin_pos] in ResourcePool().noun_not_composed:
                return []
            return [sentence[begin_pos]]

            #If there is a number, it will be the same with determinant
    if other_functions.number(sentence[begin_pos]) == 1:
        end_pos += adjective_pos(sentence, begin_pos + 1)
        return sentence[begin_pos:end_pos + begin_pos]

    #If it is a proper name
    counter = begin_pos
    while counter < len(sentence) and other_functions.find_cap_lettre(
            sentence[counter]) == 1:
        counter += 1

    #Default case return [] => ok if counter=begin_pos
    return sentence[begin_pos:counter]
def find_sn_pos(sentence, begin_pos):
    """
    We will find the nominal group which is in a known position                      
    We have to use adjective_pos to return the end position of nominal group         

    :param list sentence: the sentence (list of strings)
    :param begin_pos:the position of the nominal group       
    :return: the nominal group (as a list of words)
    """

    if begin_pos >= len(sentence):
        return []

    end_pos = 1

    #If it is a pronoun
    if sentence[begin_pos] in ResourcePool().pronouns:
        return [sentence[begin_pos]]

    #If there is a nominal group with determinant
    if sentence[begin_pos] in ResourcePool().determinants:
        end_pos += adjective_pos(sentence, begin_pos + 1)
        return sentence[begin_pos: end_pos + begin_pos]

    #If we have 'something'
    for k in ResourcePool().composed_nouns:
        if sentence[begin_pos].startswith(k):
            if sentence[begin_pos] in ResourcePool().noun_not_composed:
                return []
            return [sentence[begin_pos]]

            #If there is a number, it will be the same with determinant
    if other_functions.number(sentence[begin_pos]) == 1:
        end_pos += adjective_pos(sentence, begin_pos + 1)
        return sentence[begin_pos: end_pos + begin_pos]

    #If it is a proper name
    counter = begin_pos
    while counter < len(sentence) and other_functions.find_cap_lettre(sentence[counter]) == 1:
        counter += 1

    #Default case return [] => ok if counter=begin_pos
    return sentence[begin_pos: counter]
Esempio n. 8
0
def process_and_beginning_sentence(sentence):
    """
    process the beginning of the sentence
    For example if there is adverb, we put it at the end (i_cmpl also)
    Input=sentence                                     Output=sentence
    """

    # If sentence is empty
    if not sentence:
        return []

    # Using flag is for the ending of this function
    if other_functions.find_cap_lettre(sentence[0]) == 1:
        flg = 1
        sentence[0] = sentence[0][0].lower() + sentence[0][1:]
    else:
        flg = 0

    #We have to take off this words
    if sentence[0] == ';' or sentence[0] == 'and' or sentence[
            0] == 'ah' or sentence[0] == ',' or sentence[0] == 'very':
        sentence = sentence[1:]

    #If it starts with proposal
    if sentence[0] in ResourcePool().proposals:
        gr = determination_nominal_group(sentence, 1, 'of')
        #We put the nominal group at the end of the sentence
        if gr:
            sentence = sentence[len(gr) + 1:] + [','] + [sentence[0]] + gr
            sentence = process_and_beginning_sentence(sentence)
        else:
            #In this case we don't find a nominal group but there is a i_cmpl
            for z in sentence:
                if z == ';' or z == '.' or z == ',':
                    #The final point of i_cmpl is the punctuation
                    phrase = sentence[:sentence.index(z)]
                    if sentence[len(sentence) - 1] == '.' or sentence[
                            len(sentence) -
                            1] == '?' or sentence[len(sentence) - 1] == '!':
                        sentence = sentence[sentence.index(z) +
                                            1:len(sentence) -
                                            1] + [','] + phrase + [
                                                sentence[len(sentence) - 1]
                                            ]
                    else:
                        sentence = sentence[sentence.index(z) +
                                            1:] + [','] + phrase

    #If it starts with adverb
    if sentence[0] in ResourcePool().adverbs:
        if sentence[len(sentence) -
                    1] == '.' or sentence[len(sentence) -
                                          1] == '?' or sentence[len(sentence) -
                                                                1] == '!':
            sentence = sentence[1:len(sentence) - 1] + [sentence[0]] + [
                sentence[len(sentence) - 1]
            ]
            sentence = process_and_beginning_sentence(sentence)
        else:
            sentence = sentence[1:] + [','] + [sentence[0]]
            sentence = process_and_beginning_sentence(sentence)

    #If flag is 1 => we have a capitol letter at the beginning
    if flg == 1:
        sentence[0] = sentence[0][0].upper() + sentence[0][1:]

    return sentence
Esempio 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
Esempio n. 10
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