Example #1
0
def interval_point(Start, End, Fraction):
    """The function takes 3 numbers, either integers or floats and with an if statement
    verifies if the value of fraction is between 1 and 0"""
    if Fraction > 1 & Fraction < 0:
        print('Fractional Value must be between 0 and 1')
    else:
        """If the value of fraction is accepted, the function returns a fraction of the 
        difference between the value of Start and End and adds it to start"""
        return Fraction * math.absolute(End - Start) + Start
def process_per_sentence(f,context, tgt_words, max_dist, d_factor):
    
    print context
    word_synsets = {}
    synset_index = {}
    index = 0
        
    for i in range(len(context)):
        t_context = tuple([i,context[i]])
#         word = wn.morphy(context[i])
#         if( word == None ) :
#             word = context[i]
        word_synsets[t_context] = wn.synsets(context[i])
        for synset in word_synsets[t_context]:
            t_synset = tuple([i, synset])
            synset_index[t_synset] = index
#             print synset, index
            index += 1
            
    graph_matrix = [[0 for i in range(index)] for j in range(index)]
    #print indexprint [word for word in words]
       
    for i in range(len(context)):
        for j in range(len(context)):
            if i != j:
                 
                #check how far the 2 words are from each other
                if( absolute(i-j) <= max_dist ):
                     
                    for synset1 in word_synsets[ tuple([i, context[i]]) ]:
                        t_s1 = tuple([i, synset1])
                        for synset2 in word_synsets[ tuple([j, context[j]]) ]:
                            t_s2 = tuple([j, synset2])
#                             sim = synset1.wup_similarity(synset2)
                            sim1 = wn.wup_similarity(synset1, synset2)
                            sim2 = wn.wup_similarity(synset2, synset1)
                            if isinstance(sim1, numbers.Number) == False: sim1 = 0
                            if isinstance(sim2, numbers.Number) == False: sim2 = 0
                            
                            graph_matrix[synset_index[t_s1]][synset_index[t_s2]] = sim1
                            graph_matrix[synset_index[t_s2]][synset_index[t_s1]] = sim2
 
    ranked_sense = pr.get_pagerank(graph_matrix, d_factor)
#     print ranked_sense
   
#process only for target_words
    for i in range(len(tgt_words)):
        if isinstance(t_words[i], numbers.Number) == False:
            word_t = tuple([i, context[i]])
#         targeword_t_id = 
            synsets = word_synsets[word_t]
            max_r = 0
            res_offset = -1
            chosen_synset = None
            for synset in synsets:
                t_synset = tuple([i, synset])
                if ranked_sense[synset_index[t_synset]] >= max_r:
                    max_r = ranked_sense[synset_index[t_synset]]
                    res_offset = synset.offset
                    chosen_synset = synset  
                    offset_str=pad_zeros(res_offset)
                    answer_line=tgt_words[i][0:3]+" "+tgt_words[i]+" eng-30-"+offset_str+"-"+chosen_synset.pos+"\n"
                    print answer_line
                    f.write(answer_line)
Example #3
0
from math import log10 as log
from math import fabs as absolute


def function(n):
    return n * log(n) - 1


a = 2.0
b = 3.0
x = 0.0
cont = 0
e = 0.0001

while absolute(a - b) > e:
    x = (a + b) / 2

    if function(a) * function(x) < 0:
        b = x
    else:
        a = x

    cont += 1

print "Numero de interacoes", cont
print x
Example #4
0
from math import fabs as absolute
from math import log10 as log


def function(n):
    return (n * log(n)) - 1


def derivative(n):
    p = 0.000000000001
    return (function(n + p) - function(n)) / p


x0 = 3.0
x1 = 0.0
e = 0.00001
cont = 0
buff = x0

while not absolute(buff - x1) < e:
    cont += 1
    x1 = x0 - (function(x0) / derivative(x0))
    buff = x0
    x0 = x1

print(cont)
print(x0)
Example #5
0
def process_per_sentence(f,words_list, word_map, max_dist, d_factor):
     
    word_synsets = {}
    synset_index = {}
    index = 0
    word_position = {}
    words = []
    
    for word_id in words_list:
        words.append(word_map[word_id])
        word_position[word_map[word_id]] = int(word_id.split(".")[2][1:])
        
    for word in words:
        word_synsets[word] = wn.synsets(word)
        for synset in word_synsets[word]:
            synset_index[synset] = index
            #print synset, index
            index += 1
#        length += len(word_synsets[word])
    graph_matrix = [[0 for i in range(index)] for j in range(index)]
    #print indexprint [word for word in words]
    
    for word1 in words:
        for word2 in words:
            if word1 != word2:
                
                #check how far the 2 words are from each other
                if( absolute(word_position[word1] - word_position[word2]) <= max_dist ):
                    
                    for synset1 in word_synsets[word1]:
                        for synset2 in word_synsets[word2]:
                            sim = synset1.path_similarity(synset2)
                            if isinstance(sim, numbers.Number) == False: sim = 0
                            graph_matrix[synset_index[synset1]][synset_index[synset2]] = sim
                            graph_matrix[synset_index[synset2]][synset_index[synset1]] = sim
                        #print synset1,sim 
#1 0.0742189207914
#15 0.0442477876106
#17 0.0743000904923
#25 0.0673822870518
     
#     print graph_matrix

    ranked_sense = pr.get_pagerank(graph_matrix, d_factor)
#     print [word for word in words]
    for word in words:
        synsets = word_synsets[word]
        max_r = 0
        max_index = -1
        max_offset = 0
        chosen_synset = None
        for synset in synsets:
            if ranked_sense[synset_index[synset]] >= max_r:
                max_r = ranked_sense[synset_index[synset]]
                #print max_r
                max_offset = synset.offset
                max_index = synset_index[synset]
                chosen_synset = synset
                
                
        rk=searchwl(word_map,word_list,word)
        offset_str=pad_zeros(max_offset)
        answer_line=key[0:3]+" "+rk+" eng-30-"+offset_str+"-"+chosen_synset.pos+"\n"
        f.write(answer_line)
Example #6
0
from math import fabs as absolute
from math import log10 as log


def function(n):
    return (n * log(n)) - 1


def derivative(n):
    p = 0.000000000001  #10^-12
    return (function(n + p) - function(n)) / p


x0 = 3.0
x1 = 0.0
e = 0.0001
cont = 0

while True:
    cont += 1
    x1 = x0 - (function(x0) / derivative(x0))

    if absolute(x0 - x1) < e:
        x0 = x1
        break

    x0 = x1

print "Numero de interacoes", cont
print x0