Example #1
0
    def test_merge_rts_intersect(self):
        """ Test merging two rts with intersection."""
        
        d1=[1.0]*int(spy.math.pow(2,10))
        d2=[2.0]*int(spy.math.pow(2,11))
        
        st1=datetime.datetime(year=1990,month=2,day=3,hour=11, minute=15)
        st2=datetime.datetime(year=1990,month=3,day=3,hour=11, minute=15)
        
        ts1=rts(d1,st1,time_interval(hours=1))
        ts2=rts(d2,st2,time_interval(hours=1))
        
        ## Intentionally put some nan into ts1 to see effect.
        ii=ts1.index_after(st2)
        num_nan=10
        ts1.data[ii+1:ii+num_nan+2]=spy.nan               
        
        
        nt=merge(ts1,ts2)
        
        self.assertEqual(nt.start,ts1.start)
        self.assertEqual(nt.end,ts2.end)
        
        total_n=number_intervals(ts1.start,ts2.end,ts1.interval)+1
        
        self.assertEqual(len(nt.data),total_n)
        
        s1=nt.index_after(ts1.end)
        s2=nt.index_after(ts2.start)
        s3=ts2.index_after(ts1.end)
        self.assert_(spy.allclose(nt.data[0:ii+1],ts1.data[0:ii+1]))
        self.assert_(spy.allclose(nt.data[ii+1:ii+num_nan+2],ts2.data[0:num_nan+1]))
        self.assert_(spy.allclose(nt.data[ii+num_nan+2:s1+1],ts1.data[ii+num_nan+2:s1+1]))
        self.assert_(spy.allclose(nt.data[s1+1:len(nt.data)],ts2.data[s3+1:len(ts2.data)]))
        
        ## a small test
        d1=[1.0]*4
        d2=[2.0]*2
        
        st1=datetime.datetime(year=1990,month=2,day=1)
        st2=datetime.datetime(year=1990,month=3,day=1)
        
        ts1=rts(d1,st1,time_interval(months=1))
        ts2=rts(d2,st2,time_interval(months=1))
        nt=merge(ts1,ts2)
        self.assertEqual(len(nt),len(ts1))
        self.assertEqual(nt.data[-1],ts1.data[-1])
    

        d1=[1.0,1.0,1.0,spy.nan]
        d2=[2.0,3.0,4.0]
        
        st1=datetime.datetime(year=1990,month=2,day=1)
        st2=datetime.datetime(year=1990,month=3,day=1)
        
        ts1=rts(d1,st1,time_interval(months=1))
        ts2=rts(d2,st2,time_interval(months=1))
        nt=merge(ts1,ts2)
        self.assertEqual(len(nt),len(ts1))
        self.assertEqual(nt.data[-1],ts2.data[-1])
Example #2
0
def test_merge():
    head1 = Node(0)
    head2 = Node(0)
    assert list_len(merge(head1, head2)) == 2

    head1 = Node(0)
    head1._next = Node(1)
    head1._next._next = Node(1)
    head1._next._next._next = Node(3)
    head2 = Node(0)
    head2._next = Node(2)
    merged = merge(head1, head2)
    assert list_len(merged) == 6
    assert merged._next._next._next._next._next._data == 3
Example #3
0
def test_merge_case_none():
    head1 = None
    head2 = None
    assert merge(head1, head2) is None

    head1 = None
    head2 = Node(0)
    head2._next = Node(2)
    assert merge(head1, head2) is head2

    head1 = Node(0)
    head1._next = Node(1)
    head1._next._next = Node(3)
    head2 = None
    assert merge(head1, head2) is head1
Example #4
0
 def test_merge_rts_no_intersect(self):
     """ Test merging two rts without intersection."""
     
     d1=[1]*int(spy.math.pow(2,10))
     d2=[2]*int(spy.math.pow(2,11))
     
     st1=datetime.datetime(year=1990,month=2,day=3,hour=11, minute=15)
     st2=datetime.datetime(year=1990,month=5,day=3,hour=11, minute=15)
     
     ts1=rts(d1,st1,time_interval(hours=1))
     ts2=rts(d2,st2,time_interval(hours=1))
     nt=merge(ts1,ts2)
     
     self.assertEqual(nt.start,ts1.start)
     self.assertEqual(nt.end,ts2.end)
     
     total_n=number_intervals(ts1.start,ts2.end,ts1.interval)+1
     
     self.assertEqual(len(nt.data),total_n)
     s1=nt.index_after(ts1.end)
     s2=nt.index_after(ts2.start)
     
     self.assert_(spy.alltrue(spy.isnan(nt.data[s1+1:s2])))
     self.assert_(spy.allclose(nt.data[0:s1+1],ts1.data))
     self.assert_(spy.allclose(nt.data[s2:len(nt.data)],ts2.data))
Example #5
0
    def test_merge_large_rts(self):

        largenum=250000        

        st1=datetime.datetime(1920,1,2)
        data1=range(0,largenum)
        dt=datetime.timedelta(minutes=15)
        rt1=rts(data1,st1,dt,{})

        st2=rt1.end+dt
        data2=range(largenum,2*largenum)
        rt2=rts(data2,st2,dt,{})

        st3=rt2.end+dt
        data3=range(2*largenum,3*largenum)
        rt3=rts(data3,st3,dt,{})

        st4=rt3.end+dt
        data4=range(3*largenum,4*largenum)
        rt4=rts(data4,st4,dt,{})        
        
        ts=merge(rt1,rt2,rt3,rt4)

        self.assertEqual(len(ts),largenum*4)
        self.assertEqual(ts.data[largenum],data2[0])
        self.assertEqual(ts.data[2*largenum],data3[0])
        self.assertEqual(ts.data[3*largenum],data4[0])
Example #6
0
 def test_merge_rts_2d_intersect(self): 
     
     """ Test merging of two rts of 2-d data and has intersection."""
     
     d1=[[1.0,2.0]]*int(spy.math.pow(2,10))
     d2=[[3.0,4.0]]*int(spy.math.pow(2,11))
     
     st1=datetime.datetime(year=1990,month=2,day=3,hour=11, minute=15)
     st2=datetime.datetime(year=1990,month=3,day=3,hour=11, minute=15)
     
     ts1=rts(d1,st1,time_interval(hours=1))
     ts2=rts(d2,st2,time_interval(hours=1))
     
     ## Intentionally put some nan into ts1 to see effect.
     ii=ts1.index_after(st2)
     num_nan=10
     ts1.data[ii+1:ii+num_nan+2,]=spy.nan  
     
     nt=merge(ts1,ts2)
     
     self.assertEqual(nt.start,ts1.start)
     self.assertEqual(nt.end,ts2.end)
     
     total_n=number_intervals(ts1.start,ts2.end,ts1.interval)+1
     
     self.assertEqual(len(nt.data),total_n)
     s1=nt.index_after(ts1.end)
     s2=nt.index_after(ts2.start)
     s3=ts2.index_after(ts1.end)
     self.assert_(spy.allclose(nt.data[0:ii+1,],ts1.data[0:ii+1,]))
     self.assert_(spy.allclose(nt.data[ii+1:ii+num_nan+2,],ts2.data[0:num_nan+1,]))
     self.assert_(spy.allclose(nt.data[ii+num_nan+2:s1+1,],ts1.data[ii+num_nan+2:s1+1,]))
     self.assert_(spy.allclose(nt.data[s1+1:len(nt.data),],ts2.data[s3+1:len(ts2.data),]))
Example #7
0
def structure_text(text, template):
    title = ''
    author_name = []
    teacher_name = ''
    class_name = ''
    bibliography = []

    pop_list = []
    offset = 0

    # Set the structure variables
    for i in range(len(text)):
        if text[i].startswith(u'\u0023\u0020'): # Title of the document (# [TITLE])
            title = text[i][2:].strip('\n')
            pop_list.append(i)
        elif text[i].startswith(u'\u0023\u0023\u0020Author:\u0020'): # Author of the document (## Author: [AUTHOR])
            author_name = text[i][11:].strip('\n').split(' ')
            pop_list.append(i)
        elif text[i].startswith(u'\u0023\u0023\u0020Teacher:\u0020'): # Teacher of the document (## Teacher: [TEACHER])
            teacher_name = text[i][12:].strip('\n')
            pop_list.append(i)
        elif text[i].startswith(u'\u0023\u0023\u0020Class:\u0020'): # Class name for the document (## Class: [CLASS])
            class_name = text[i][10:].strip('\n')
            pop_list.append(i)
        elif text[i].startswith(u'\u0023\u0023\u0020Cite:'): # Citations for the document (## Cite: \n [ENTRY] \n [ENTRY])
            pop_list.append(i)
            for j in range(i+1, len(text)): # Add ALL lines after the (## Cite:) to the bibliography
                text[j] += u'\n'
                bibliography.append(text[j])
                pop_list.append(j)

    # Format the bibliography
    replace_chars(bibliography)

    # Merge the structure variables with the template
    for i in range(len(template)):
        template[i] = template[i].replace('TITLE', title)
        template[i] = template[i].replace('FIRSTNAME', author_name[0])
        template[i] = template[i].replace('LASTNAME', author_name[1])
        template[i] = template[i].replace('CLASS', class_name)
        template[i] = template[i].replace('TEACHER', teacher_name)

    merge(bibliography, template, u'\u0025\u0020BEGIN\u0020WORKS\u0020CITED\u0020HERE', u'\u005cbibent\n')

    for i in pop_list: # Remove the metadata from essay once structure variables have been assigned
        text.pop(i - offset) # Remove the item from the list
        offset += 1 # Increment the offset to account for the previous item removal
Example #8
0
    def testMergeTwoSystems(self):
        '''Tests merging two distinct Databases.
        '''
        # Creates Database containing systems 11 Com and 11 UMi
        comAndUmiDB = Database()
        data = convert.getOEC(minidom.parse("test_files/11 Com.xml"))
        comAndUmiDB.update_xml(data)
        data = convert.getOEC(minidom.parse("test_files/11 UMi.xml"))
        comAndUmiDB.update_xml(data)

        mergedDB = merge(self.umiDB, self.comDB)

        self.assertEqual(mergedDB == comAndUmiDB, True)
Example #9
0
def getTestdata(filepath=""):  #chunk to be tagged

    # Location of test data file
    #filepath="testSplitted/107-16164699"
    test_data = []
    sent_ids = []

    res = merge(filepath)  # get fvs for bayse
    for w in res:
        test_data.append((res[w]))
        sent_ids.append(w)
    #test data is the list to feed into the classifier
    #sentence ids are the corresponding sids for the test data
    return sent_ids, test_data
Example #10
0
def main(input_file, is_compile, is_verbose):

    fp, fn = ntpath.split(input_file)

    with open(mla_template, 'r') as template_file: # Open the final template file for reading
        template = template_file.readlines() # Read lines of the template file

        with codecs.open(input_file, 'r', encoding='utf-8') as essay_file: # Open the *.md file for reading
            essay = essay_file.readlines() # Read lines in *.md file
            if (is_verbose):
                print 'Reading data from ' + input_file

            convert(essay, template) # Convert markdown to tex
            if (is_verbose):
                print 'Converting ' + fn + ' to a LaTeX file'

            merge(essay, template, u'\u0025\u0020BEGIN\u0020ESSAY\u0020HERE') # Combine the essay and the template
            if (is_verbose):
                print 'Merging data from ' + fn + ' with template ' + ntpath.split(mla_template)[1]

            tex_path = fp + '/' + os.path.splitext(fn)[0] + '.tex' # Create the file path of the .tex file

            with codecs.open(tex_path, 'w+', encoding='utf-8') as mla_tex: # Open (create) the *.tex file for editing
                if (is_verbose):
                    print 'Writing data to ' + ntpath.split(tex_path)[1]
                for line in template:
                    mla_tex.write(line)
            mla_tex.close()
        essay_file.close()
    template_file.close()

    if (is_compile == True):
        if (is_verbose):
            print 'Generating ' + os.path.splitext(ntpath.split(tex_path)[1])[0] + '.pdf from ' + ntpath.split(tex_path)[1]
            gen_pdf(ntpath.split(tex_path)[0], ntpath.split(tex_path)[1], True)
        else:
            gen_pdf(ntpath.split(tex_path)[0], ntpath.split(tex_path)[1], False)
Example #11
0
    def testMergeTwoSystemsWithThree(self):
        '''Tests merging two Databases where that each have multiple systems.
        '''
        # Creates expected Database with all 5 systems
        expectedDB = Database()
        data = convert.getOEC(minidom.parse("test_files/11 Com.xml"))
        expectedDB.update_xml(data)
        data = convert.getOEC(minidom.parse("test_files/11 Umi.xml"))
        expectedDB.update_xml(data)
        data = convert.getOEC(minidom.parse("test_files/14 And.xml"))
        expectedDB.update_xml(data)
        data = convert.getOEC(minidom.parse("test_files/14 Her.xml"))
        expectedDB.update_xml(data)
        data = convert.getOEC(minidom.parse("test_files/16 Cygni.xml"))
        expectedDB.update_xml(data)

        mergedDB = merge(self.herToDel, self.comToAnd)

        self.assertEqual(mergedDB == expectedDB, True)
Example #12
0
    def test_merge_rts_intersect2(self):
        """ Test merging two rts with intersection."""
        
        d1=[1.0]*4
        d2=[2.0]*4
        d1[2]=spy.nan
        
        st1=datetime.datetime(year=1990,month=2,day=1)
        st2=datetime.datetime(year=1990,month=3,day=1)
        
        ts1=rts(d1,st1,time_interval(months=1))
        ts2=rts(d2,st2,time_interval(months=1))
               
        nt=merge(ts1,ts2)
        ## test ts1 data take priority on valid data
        self.assertEqual(nt.data[1],ts1.data[1])

        ## test ts2 data take prioirty on invalid data of ts1
        self.assertEqual(nt.data[2],ts2.data[2])
Example #13
0
def process_query_obj(query_obj, dictionary, postings_list):
    all_doc_ids = dictionary["ALL_TERMS"]

    if isinstance(query_obj, Word):
        term = query_obj.value
        if term in dictionary:
            freq = dictionary[term][0]
            pointer = dictionary[term][1]
            posting_list = get_posting_list(freq, pointer, postings_list)
            if query_obj.is_not:
                posting_list = negate(posting_list, all_doc_ids)
            return posting_list
        elif query_obj.is_not:
            return all_doc_ids
        else:
            return []

    elif isinstance(query_obj, Query):
        posting_list1 = process_query_obj(query_obj.value1, dictionary, postings_list)
        posting_list2 = process_query_obj(query_obj.value2, dictionary, postings_list)
        results_list = merge(posting_list1, posting_list2, query_obj.op)
        if query_obj.is_not:
            results_list = negate(results_list, all_doc_ids)
        return results_list
Example #14
0
 def testMergeSystemWithSubset(self):
     '''Tests merging two Databases where oec contains other.
     '''
     mergedDB = merge(self.comDB, self.comToAnd)
     self.assertEqual(self.comToAnd == mergedDB, True)
Example #15
0
 def testMergeWithSelf(self):
     '''Tests merging a Database with itself.
     '''
     mergedDB = merge(self.comDB, self.comDB)
     self.assertEqual(self.comDB == mergedDB, True)
Example #16
0
 def move(self,a):
     print
     if(int(a)==8):
         print "UP"
         for x in range(self.width):
             list=[]
             for y in range(self.height):
                 list.append(self.Grid[y][x])
             
             list=merge(list)
            
             for y in range(self.height):
                 self.Grid[y][x]=list[y]    
     if(int(a)==4):
         print "LEFT"
         for x in range(self.height):
             list=[]
             list=self.Grid[x]
             list=merge(list)
             self.Grid[x]=list
     if(int(a)==6):
         print "RIGHT"
         for x in range(self.height):
             list=[]
             list=self.Grid[x]
             list.reverse()
             list=merge(list)
             list.reverse()
             self.Grid[x]=list
     if(int(a)==2):
         print "DOWN"
         for x in range(self.width):
             list=[]
             for y in range(self.height):
                 list.append(self.Grid[y][x])
             list.reverse()
             list=merge(list)
             list.reverse()
             for y in range(self.height):
                 self.Grid[y][x]=list[y]
     if(int(a)!=2 and int(a)!=8 and int(a)!=6 and int(a)!=4):
         print "a",a
         print "Are you nuts..?"
         print "Use some brains and give a valid input"
         return
     self.printtheGrid()
     print "Generating a random no. at random empty position"
     print "......."
     time.sleep(1)
     print
     print "HERE"
     self.putnumberrandomly()
     self.printtheGrid()
     
     if(self.checkifwon(self.aim)):
         print "yeah! smarty"
         print "Congratulations, you won."
         quit()
         
     if(self.checkiflost()):
         print "LOSER"
         quit()