Ejemplo n.º 1
0
class ValidFakeDatabase:

    # initializes dummy data to return
  def __init__(self):
    self.dateA = datetime.datetime(2013, 8, 4, 12, 30, 45)
	self.dateB = datetime.datetime(1999, 7, 6, 12, 30, 45)
    self.paperA = Paper("12345", "The Health Benefits of the All-Bacon Diet", ["4445", "666", "123"], ["Genetics", "Bioinformatics", "Search Engines", "Artificial Intelligence"], "Bacon is actually one of the healthiest foods of all time.  This is an abstract!  For the full article, download the PDF.", "1234", self.dateA, datetime.datetime.now(), "1111", ["ref1", "ref2", "ref3"], "14000", ["citation link 1", "citation link 2", "citation link 2"], "Your Favorite Publisher",["Alan Turing", "Shia Leboeuf", "Andrew Davidson"])
    self.paperB = Paper("90210", "The Dangers of Coding While Hungry", ["12068", "7797", "4326"], ["Genetics", "Bioinformatics", "Search Engines", "Artificial Intelligence"], " Abstracts never seem to be simple or contain useful information.", "444", self.dateA, datetime.datetime.now(), "6677", ["ref1", "ref2", "ref3"], "14000", ["citation link 1", "citation link 2", "citation link 2"], "Your Favorite Publisher",["Andrew Davidson","William Shakespeare","Edsger Dijkstra"])
    self.paperC = Paper("666", "The Struggles of Eating a Giordano's Pizza Alone", ["567", "2213", "989"], ["6237", "3177", "432"], "Abstracts are the SparkNotes of the academic world.", "12534434", self.dateB, datetime.datetime.now(), "2345", ["ref1", "ref2", "ref3"], "14000", ["citation link 1", "citation link 2", "citation link 2"], "Prentice Hall", ["Andrew Davidson","William Shakespeare","Edsger Dijkstra"])

    self.authorA = Author("55555", "Shia Leboeuf", "4444", ["0", "1"],["The Health Benefits of the All-Bacon Diet", "The Dangers of Coding While Hungry"],[["Andrew Davidson","William Shakespeare","Edsger Dijkstra"],["Alan Turing", "Shia Leboeuf", "Andrew Davidson"]],[self.dateB,self.dateA])
    self.authorB = Author("43216", "Andrew Davidson", "1", ["0", "1"],["The Health Benefits of the All-Bacon Diet", "The Dangers of Coding While Hungry"],[["Andrew Davidson","William Shakespeare","Edsger Dijkstra"],["Alan Turing", "Shia Leboeuf", "Andrew Davidson"]],[self.dateB,self.dateA])
    self.authorC = Author("6542", "William Shakespeare", "11542", ["2", "1"],["The Struggles of Eating a Giordano's Pizza Alone","The Dangers of Coding While Hungry"],[["Andrew Davidson","William Shakespeare","Edsger Dijkstra"],["Alan Turing", "Shia Leboeuf", "Andrew Davidson"]],[self.dateB,self.dateA])
    self.authorD = Author("64632", "Edsger Dijkstra", "147", ["2", "1"],["The Struggles of Eating a Giordano's Pizza Alone","The Dangers of Coding While Hungry"],[["Andrew Davidson","William Shakespeare","Edsger Dijkstra"],["Alan Turing", "Shia Leboeuf", "Andrew Davidson"]],[self.dateB,self.dateA])
    self.authorE = Author("63421", "Alan Turing", "40000", ["2", "1"],["The Struggles of Eating a Giordano's Pizza Alone","The Dangers of Coding While Hungry"],[["Andrew Davidson","William Shakespeare","Edsger Dijkstra"],["Alan Turing", "Shia Leboeuf", "Andrew Davidson"]],[self.dateB,self.dateA])

    self.tagA = Tag("Genetics", "40000", ["0", "1"])
    self.tagB = Tag("Bioinformatics", "12345", ["0", "1"])
    self.tagC = Tag("Search Engines", "5555", ["2", "3"])
    self.tagD = Tag("Artificial Intelligence", "42", ["2", "3"])
    
    self.publisherA = Publisher("1233", "Your Favorite Publisher",0)
    self.publisherB = Publisher("3468", "Prentice Hall",0)
    self.publisherC = Publisher("8372", "Rose-Hulman",0)

    self.userA = User("0","Otis Redding", ["1", "3"],["Andrew Davidson","Jonathan Jenkins"], [self.paperA, self.paperB, self.paperC], [self.authorA, self.authorB, self.authorC], [self.tagA, self.tagC, self.tagB, self.tagD], "45", "005792830123")
Ejemplo n.º 2
0
 def test_that_pencil_that_starts_dull_only_writes_empty_spaces(self):
     point_durability = 0
     pencil = Pencil(point_durability=point_durability)
     paper = Paper()
     pencil.write(paper, 'test')
     self.assertEqual('    ',
                      paper.display_page())  # paper should contain 4 spaces
Ejemplo n.º 3
0
 def setUp(self):
     durability = 10
     self.eraser = Eraser(durability=durability)
     point_durability = 100
     initial_length = 5
     self.pencil = Pencil(point_durability=point_durability, initial_length=initial_length, eraser=self.eraser)
     self.paper = Paper()
Ejemplo n.º 4
0
 def __init__(self, citFile='', refFile=''):
     Paper.__init__(self)
     self.citFile = citFile
     self.refFile = refFile
     if self.citFile and self.refFile:
         self.loadCitFile(self.citFile)
         self.loadRefFile(self.refFile)
Ejemplo n.º 5
0
 def __init__(self, citFile = '', refFile = ''):
     Paper.__init__(self)
     self.citFile = citFile
     self.refFile = refFile
     if self.citFile and self.refFile:
         self.loadCitFile(self.citFile)
         self.loadRefFile(self.refFile)
Ejemplo n.º 6
0
 def test_that_pencil_writes_where_it_left_off(self):
     paper = Paper()
     point_durability = 50
     pencil = Pencil(point_durability=point_durability)
     pencil.write(paper, 'She sells sea shells')
     pencil.write(paper, ' down by the sea shore')
     self.assertEqual('She sells sea shells down by the sea shore',
                      paper.display_page())
Ejemplo n.º 7
0
 def test_that_pencil_that_becomes_dull_during_writing_writes_empty_spaces(
         self):
     point_durability = 4
     pencil = Pencil(point_durability=point_durability)
     paper = Paper()
     pencil.write(paper, 'Test')
     self.assertEqual(
         'Tes ',
         paper.display_page())  # paper should contain one space at end
Ejemplo n.º 8
0
 def test_that_eraser_erases_word_in_opposite_order(self):
     durability = 3
     eraser = Eraser(durability=durability)
     point_durability = 100
     initial_length = 5
     pencil = Pencil(point_durability=point_durability, initial_length=initial_length, eraser=eraser)
     paper = Paper()
     pencil.write(paper, 'Buffalo Bill')
     pencil.erase(paper, 'Bill')
     self.assertEqual('Buffalo B   ', paper.display_page())
Ejemplo n.º 9
0
 def test_that_pencil_that_is_sharpened_before_going_dull_continues_to_write(
         self):
     point_durability = 20
     initial_length = 5
     pencil = Pencil(point_durability=point_durability,
                     initial_length=initial_length)
     paper = Paper()
     pencil.write(paper, 'Testing sharpening.')
     pencil.sharpen()
     pencil.write(paper, ' Testing sharpening.')
     self.assertEqual('Testing sharpening. Testing sharpening.',
                      paper.display_page())
Ejemplo n.º 10
0
 def test_that_pencil_that_becomes_dull_picks_up_after_spaces_when_sharpened(
         self):
     point_durability = 5
     initial_length = 5
     pencil = Pencil(point_durability=point_durability,
                     initial_length=initial_length)
     paper = Paper()
     pencil.write(paper, 'Test sharpening')
     self.assertEqual(0, pencil.point_durability)
     pencil.sharpen()
     pencil.write(paper, 'Test sharpening')
     self.assertEqual('Test           Test           ',
                      paper.display_page())
Ejemplo n.º 11
0
 def test_that_pencil_not_dull_when_upper_case_text_should_not_use_up_point_durability(
         self):
     point_durability = 10
     pencil = Pencil(point_durability=point_durability)
     paper = Paper()
     pencil.write(paper, 'TEST')
     self.assertEqual(2, pencil.point_durability)
Ejemplo n.º 12
0
 def test_that_pencil_not_dull_when_lower_case_text_len_is_less_than_point_durability(
         self):
     point_durability = 10
     pencil = Pencil(point_durability=point_durability)
     paper = Paper()
     pencil.write(paper, 'test')
     self.assertEqual(6, pencil.point_durability)
Ejemplo n.º 13
0
 def read_csv_file(self, csv_name):
     with open(csv_name) as csvfile:
         # readCSV = csv.reader(csvfile, delimiter='\n')
         for row in csvfile:
             paper = Paper(row.split('/')[-1].split('\n')[0])
             self.papers_dict[paper.pmid] = paper
             self.csv_papers.append(paper.pmid)
 def getPaper(self, paperID):
   title = self.redisDB.get("Paper:"+paperID+":Title")
   if title == None:
     return None
   authorIDs = list(self.redisDB.smembers("Paper:"+paperID+":Authors"))
   tags = list(self.redisDB.smembers("Paper:"+paperID+":Tags"))
   abstract = self.redisDB.get("Paper:"+paperID+":Abstract")
   publisherID = self.redisDB.get("Paper:"+paperID+":PublisherID")
   viewCount = self.redisDB.get("Paper:"+paperID+":ViewCount")
   datePosted = datetime.strptime(self.redisDB.get("Paper:"+paperID+":DatePosted"), "%Y-%m-%d %H:%M:%S.%f")
   # TODO: fix this later
   datePublished = self.redisDB.get("Paper:"+paperID+":DatePublished")
   print "datePublished for paper", paperID, title, ":", datePublished
   datePublished = date(int(datePublished[0:4]), int(datePublished[5:7]), int(datePublished[8:10]))
   postedByUserID = self.redisDB.get("Paper:"+paperID+":PostedByUserID")
   references = list(self.redisDB.smembers("Paper:"+paperID+":References"))
   citedBys = list(self.redisDB.smembers("Paper:"+paperID+":CitedBys"))
   isUploaded = self.redisDB.get("Paper:"+paperID+":IsUploaded") =='True'
   authorNames = []
   for authorID in authorIDs:
     authorNames.append(self.redisDB.get("Author:"+authorID+":Name"))
   publisherGuy = self.getPublisher(publisherID)
   if publisherGuy is None:
     publisherName = "No Publisher Name"
   else:
     publisherName = publisherGuy.name
   return Paper(paperID, title, authorIDs, tags, abstract, publisherID, datePublished, datePosted, postedByUserID, references, viewCount, citedBys, publisherName, authorNames, isUploaded)
Ejemplo n.º 15
0
 def test_that_pencil_point_durability_decrements_by_one_for_special_characters(
         self):
     point_durability = 30
     pencil = Pencil(point_durability=point_durability)
     paper = Paper()
     pencil.write(paper, '~!@#$%^&*()_+`-=:",./<>?{}[]|'
                  )  # 29 special chars written to paper
     self.assertEqual(1, pencil.point_durability)
Ejemplo n.º 16
0
 def test_that_pencil_point_durability_does_not_change_for_newline_characters(
         self):
     point_durability = 10
     pencil = Pencil(point_durability=point_durability)
     paper = Paper()
     pencil.write(paper,
                  '\ntest\ntest\n\n\n')  # 4 newlines written to paper
     self.assertEqual(2, pencil.point_durability)
Ejemplo n.º 17
0
 def test_that_pencil_that_becomes_dull_returns_to_start_point_durability_when_sharpened(
         self):
     point_durability = 15
     initial_length = 5
     pencil = Pencil(point_durability=point_durability,
                     initial_length=initial_length)
     paper = Paper()
     pencil.write(paper, 'Test sharpening')
     self.assertEqual(0, pencil.point_durability)
     pencil.sharpen()
     self.assertEqual(15, pencil.point_durability)
Ejemplo n.º 18
0
    def recursion_search_citations(self, paper_pmid, k):
        """
        recursion function for search the papers that cited the original paper
        :param paper_pmid: the original paper pmid
        :param k: the number of recursion iterations
        :return: None (append all papers to self.papers_dict)
        """
        if k == 0: return

        original_paper = self.papers_dict[paper_pmid]
        if original_paper == None or original_paper.pm_cited == None: return

        for new_paper_pmid in original_paper.pm_cited:
            if new_paper_pmid not in self.papers_dict:
                new_paper = Paper(new_paper_pmid)
                new_paper.add_to_pm_cite(paper_pmid)
                self.papers_dict[new_paper.pmid] = new_paper
            else:
                self.papers_dict[new_paper_pmid].add_to_pm_cite(paper_pmid)
            self.recursion_search_citations(new_paper_pmid, k - 1)
Ejemplo n.º 19
0
    def create_paper(self, field):
        '''
        Creates a paper in the given field.

        :param field: string field of new paper
        :return: tuple with the paper object and other authors
        '''
        paper = Paper(field, self.id)
        self.add_paper(paper.id, True, field, paper)

        other_authors = self.choose_authors(self.number_of_coauthors(), field)

        for author in other_authors:
            self.simulation.dictionary[author].add_paper(
                paper.id, False, field, paper)

            if 'agent' in self.day_actions:
                self.day_actions['agent'].append(author)
            else:
                self.day_actions['agent'] = [author]
Ejemplo n.º 20
0
def f():

    f = open('input.txt')

    # read dots
    dots = set()
    line = f.readline()
    while line != '\n':
        line = line.replace('\n', '').split(',')
        dots.add((int(line[0]), int(line[1])))
        line = f.readline()
    
    # read fold instructions
    fold_instructions = list()
    line = f.readline()
    while line != '':
        line = line.replace('\n', '').split('=')
        fold_instructions.append((line[0][-1], int(line[1])))
        line = f.readline()
    print(fold_instructions)

    f.close()

    # PART I
    # fold with the first instruction
    paper = Paper(dots)
    paper.fold(fold_instructions.pop(0))
    # print the number of visible dot
    print('[f]: Dot count = %d' % (paper.count_dots()))

    # PART II
    # fold with the other instructions
    for fold_instruction in fold_instructions:
        paper.fold(fold_instruction)
    # print the result paper
    print(paper.to_string())

    return 0
Ejemplo n.º 21
0
                            break
            if article_idx == -1:
                continue
            pmid = ids[article_idx]
            if pmid == -1:
                continue
            rob_map = {}
            conflict_d = set()
            for rob in robs:
                d = dmap.get(rob[0].lower(), '')
                if d in d2idx and d not in conflict_d:
                    label = get_judgement_label(rob[1].lower())
                    if label == -1:
                        print '=====', 'Invalid label', rob
                        continue
                    if d in rob_map:
                        label_prev = rob_map[d][0]
                        if label_prev != label:
                            rob_map.pop(d)
                            conflict_d.add(d)
                            continue
                        else:
                            rob_map[d] = (label, rob_map[d][1] + ' ' + rob[2])
                    else:
                        rob_map[d] = (label, rob[2])
            p = Paper(pmid=pmid, rob=rob_map)
            p.extractQuotes()
            papers.append(p)
        # break
    pickle.dump(papers, open(pickle_folder + 'papers.pickle', 'wb'))
Ejemplo n.º 22
0
 def __init__(self, citFile = '', refFile = ''):
     Paper.__init__(self)
     self.citFile = citFile
     self.refFile = refFile
Ejemplo n.º 23
0
 def test_that_pencil_point_durability_does_not_change_for_spaces(self):
     point_durability = 10
     pencil = Pencil(point_durability=point_durability)
     paper = Paper()
     pencil.write(paper, 'test    ')  # 4 spaces written to paper
     self.assertEqual(6, pencil.point_durability)
Ejemplo n.º 24
0
 def test_that_eraser_does_not_erase_when_it_is_given_an_empty_string(self):
     input_text = 'How much wood...'
     paper = Paper(input_text)
     self.pencil.erase(paper, '')
     self.assertEqual('How much wood...', paper.display_page())
Ejemplo n.º 25
0
 def test_that_eraser_raises_value_error_if_text_to_erase_is_not_present(self):
     input_text = 'How much wood would a woodchuck chuck if a woodchuck could chuck wood?'
     paper = Paper(input_text)
     with self.assertRaises(ValueError):
         self.pencil.erase(paper, 'how')
Ejemplo n.º 26
0
class TestEraser(unittest.TestCase):
    def setUp(self):
        durability = 10
        self.eraser = Eraser(durability=durability)
        point_durability = 100
        initial_length = 5
        self.pencil = Pencil(point_durability=point_durability, initial_length=initial_length, eraser=self.eraser)
        self.paper = Paper()

    def test_that_eraser_erases_last_occurance_of_text_on_paper(self):
        self.pencil.write(self.paper, 'test the eraser test the eraser')
        self.pencil.erase(self.paper, 'eraser')
        self.assertEqual('test the eraser test the       ', self.paper.display_page())

    def test_that_eraser_can_be_initialized_with_a_durability(self):
        durability = 10
        eraser = Eraser(durability=durability)
        self.assertEqual(10, eraser.durability)

    def test_that_eraser_degrades_correct_amount_for_non_whitespace_characters(self):
        self.pencil.write(self.paper, 'test that eraser degrades correct amount')
        self.pencil.erase(self.paper, 'eraser')
        self.assertEqual(4, self.eraser.durability)

    def test_that_eraser_degrades_correct_amount_for_whitespace_characters(self):
        self.pencil.write(self.paper, 'test that eraser degrades correct amount')
        self.pencil.erase(self.paper, 'test that')
        self.assertEqual(2, self.eraser.durability)

    def test_that_eraser_erases_multiple_words_at_one_time_correctly(self):
        self.pencil.write(self.paper, 'test that eraser degrades correct amount')
        self.pencil.erase(self.paper, 'test that')
        expected_text = '          eraser degrades correct amount'
        self.assertEqual(expected_text, self.paper.display_page())

    def test_that_eraser_erases_multiple_words_one_after_another_correctly(self):
        input_text = 'How much wood would a woodchuck chuck if a woodchuck could chuck wood?'
        self.pencil.write(self.paper, input_text)
        self.pencil.erase(self.paper, 'chuck')
        self.pencil.erase(self.paper, 'chuck')
        expected_text = 'How much wood would a woodchuck chuck if a wood      could       wood?'
        self.assertEqual(expected_text, self.paper.display_page())

    def test_that_eraser_erases_word_in_opposite_order(self):
        durability = 3
        eraser = Eraser(durability=durability)
        point_durability = 100
        initial_length = 5
        pencil = Pencil(point_durability=point_durability, initial_length=initial_length, eraser=eraser)
        paper = Paper()
        pencil.write(paper, 'Buffalo Bill')
        pencil.erase(paper, 'Bill')
        self.assertEqual('Buffalo B   ', paper.display_page())

    def test_that_eraser_raises_value_error_if_text_to_erase_is_not_present(self):
        input_text = 'How much wood would a woodchuck chuck if a woodchuck could chuck wood?'
        paper = Paper(input_text)
        with self.assertRaises(ValueError):
            self.pencil.erase(paper, 'how')

    def test_that_eraser_does_not_erase_when_it_is_given_an_empty_string(self):
        input_text = 'How much wood...'
        paper = Paper(input_text)
        self.pencil.erase(paper, '')
        self.assertEqual('How much wood...', paper.display_page())
Ejemplo n.º 27
0
class TestPencil(unittest.TestCase):
    def setUp(self):
        self.paper = Paper()
        point_durability = 50
        eraser_durability = 20
        eraser = Eraser(eraser_durability)
        self.pencil = Pencil(point_durability=point_durability, eraser=eraser)

    def test_that_pencil_writes_on_paper(self):
        paper = Paper()
        point_durability = 50
        pencil = Pencil(point_durability=point_durability)
        pencil.write(paper, 'She sells sea shells')
        self.assertEqual('She sells sea shells', paper.display_page())

    def test_that_pencil_writes_where_it_left_off(self):
        paper = Paper()
        point_durability = 50
        pencil = Pencil(point_durability=point_durability)
        pencil.write(paper, 'She sells sea shells')
        pencil.write(paper, ' down by the sea shore')
        self.assertEqual('She sells sea shells down by the sea shore',
                         paper.display_page())

    def test_that_point_durability_can_be_set_on_construction(self):
        point_durability = 10
        pencil = Pencil(point_durability=point_durability)
        self.assertEqual(point_durability, pencil.point_durability)

    def test_that_pencil_not_dull_when_lower_case_text_len_is_less_than_point_durability(
            self):
        point_durability = 10
        pencil = Pencil(point_durability=point_durability)
        paper = Paper()
        pencil.write(paper, 'test')
        self.assertEqual(6, pencil.point_durability)

    def test_that_pencil_not_dull_when_upper_case_text_should_not_use_up_point_durability(
            self):
        point_durability = 10
        pencil = Pencil(point_durability=point_durability)
        paper = Paper()
        pencil.write(paper, 'TEST')
        self.assertEqual(2, pencil.point_durability)

    def test_that_pencil_point_durability_does_not_change_for_spaces(self):
        point_durability = 10
        pencil = Pencil(point_durability=point_durability)
        paper = Paper()
        pencil.write(paper, 'test    ')  # 4 spaces written to paper
        self.assertEqual(6, pencil.point_durability)

    def test_that_pencil_point_durability_does_not_change_for_newline_characters(
            self):
        point_durability = 10
        pencil = Pencil(point_durability=point_durability)
        paper = Paper()
        pencil.write(paper,
                     '\ntest\ntest\n\n\n')  # 4 newlines written to paper
        self.assertEqual(2, pencil.point_durability)

    def test_that_pencil_point_durability_decrements_by_one_for_special_characters(
            self):
        point_durability = 30
        pencil = Pencil(point_durability=point_durability)
        paper = Paper()
        pencil.write(paper, '~!@#$%^&*()_+`-=:",./<>?{}[]|'
                     )  # 29 special chars written to paper
        self.assertEqual(1, pencil.point_durability)

    def test_that_pencil_that_starts_dull_only_writes_empty_spaces(self):
        point_durability = 0
        pencil = Pencil(point_durability=point_durability)
        paper = Paper()
        pencil.write(paper, 'test')
        self.assertEqual('    ',
                         paper.display_page())  # paper should contain 4 spaces

    def test_that_pencil_that_becomes_dull_during_writing_writes_empty_spaces(
            self):
        point_durability = 4
        pencil = Pencil(point_durability=point_durability)
        paper = Paper()
        pencil.write(paper, 'Test')
        self.assertEqual(
            'Tes ',
            paper.display_page())  # paper should contain one space at end

    def test_that_pencil_that_becomes_dull_returns_to_start_point_durability_when_sharpened(
            self):
        point_durability = 15
        initial_length = 5
        pencil = Pencil(point_durability=point_durability,
                        initial_length=initial_length)
        paper = Paper()
        pencil.write(paper, 'Test sharpening')
        self.assertEqual(0, pencil.point_durability)
        pencil.sharpen()
        self.assertEqual(15, pencil.point_durability)

    def test_that_pencil_that_becomes_dull_picks_up_after_spaces_when_sharpened(
            self):
        point_durability = 5
        initial_length = 5
        pencil = Pencil(point_durability=point_durability,
                        initial_length=initial_length)
        paper = Paper()
        pencil.write(paper, 'Test sharpening')
        self.assertEqual(0, pencil.point_durability)
        pencil.sharpen()
        pencil.write(paper, 'Test sharpening')
        self.assertEqual('Test           Test           ',
                         paper.display_page())

    def test_that_creation_of_pencil_with_initial_length_creates_pencil_with_correct_length(
            self):
        initial_length = 5
        pencil = Pencil(initial_length=initial_length)
        self.assertEqual(5, pencil.length)

    def test_that_sharpening_pencil_reduces_length_by_one(self):
        initial_length = 5
        pencil = Pencil(initial_length=initial_length)
        pencil.sharpen()
        self.assertEqual(4, pencil.length)

    def test_that_trying_to_sharpen_zero_length_pencil_raises_error(self):
        initial_length = 0
        pencil = Pencil(initial_length=initial_length)
        with self.assertRaises(ValueError):
            pencil.sharpen()

    def test_that_pencil_that_is_sharpened_before_going_dull_continues_to_write(
            self):
        point_durability = 20
        initial_length = 5
        pencil = Pencil(point_durability=point_durability,
                        initial_length=initial_length)
        paper = Paper()
        pencil.write(paper, 'Testing sharpening.')
        pencil.sharpen()
        pencil.write(paper, ' Testing sharpening.')
        self.assertEqual('Testing sharpening. Testing sharpening.',
                         paper.display_page())

    def test_that_pencil_can_edit_paper_at_specific_location_with_at_symbol_if_char_present(
            self):
        self.pencil.write(self.paper, 'Testing sharpening.')
        self.pencil.edit(self.paper,
                         replacement_text='R',
                         method='index',
                         location_index=0)
        self.assertEqual('@esting sharpening.', self.paper.display_page())

    def test_that_pencil_can_edit_paper_at_specific_location_with_char_if_space_present(
            self):
        self.pencil.write(self.paper, 'Testing sharpening.')
        self.pencil.edit(self.paper,
                         replacement_text='a',
                         method='index',
                         location_index=7)
        self.assertEqual('Testingasharpening.', self.paper.display_page())

    def test_that_pencil_can_edit_paper_at_specific_location_with_space_if_space_present(
            self):
        self.pencil.write(self.paper, 'Testing sharpening.')
        self.pencil.edit(self.paper,
                         replacement_text=' ',
                         method='index',
                         location_index=7)
        self.assertEqual('Testing sharpening.', self.paper.display_page())

    def test_that_pencil_edit_with_overflow_replaces_non_whitespace_chars_with_at_symbols(
            self):
        self.pencil.write(self.paper, 'An       a day keeps the doctor away')
        self.pencil.edit(self.paper,
                         replacement_text='artichoke',
                         method='index',
                         location_index=3)
        self.assertEqual('An artich@k@ay keeps the doctor away',
                         self.paper.display_page())

    def test_that_pencil_edit_with_no_overflow_replaces_empty_spaces_with_edit_text(
            self):
        self.pencil.write(self.paper, 'An       a day keeps the doctor away')
        self.pencil.edit(self.paper,
                         replacement_text='onion',
                         method='index',
                         location_index=3)
        self.assertEqual('An onion a day keeps the doctor away',
                         self.paper.display_page())

    def test_that_pencil_edit_of_less_empty_spaces_than_original_empty_space_results_in_original_empty_spaces(
            self):
        self.pencil.write(self.paper,
                          'An       a day keeps the doctor away')  # 7 spaces
        self.pencil.edit(self.paper,
                         replacement_text='   ',
                         method='index',
                         location_index=3)  # edit 3 empty spaces
        self.assertEqual('An       a day keeps the doctor away',
                         self.paper.display_page())

    def test_that_pencil_can_edit_paper_where_erasure_happened_with_at_symbol_if_char_present(
            self):
        self.pencil.write(self.paper, 'Testing erase functionality')
        self.pencil.erase(self.paper, 'Testing')
        self.pencil.edit(self.paper,
                         replacement_text='Pineapple',
                         method='erase',
                         erase_number=1)
        self.assertEqual('Pineappl@rase functionality',
                         self.paper.display_page())

    def test_that_pencil_can_edit_paper_where_third_erasure_was_done(self):
        self.pencil.write(self.paper, 'test the edit test the edit test.')
        self.pencil.erase(self.paper, 'test')
        self.pencil.erase(self.paper, 'test')
        self.pencil.erase(self.paper, 'test')
        self.pencil.edit(self.paper,
                         replacement_text='Pineapple',
                         method='erase',
                         erase_number=3)
        self.assertEqual('Pinea@@@eedit      the edit     .',
                         self.paper.display_page())

    def test_that_pencil_can_edit_past_text_into_more_empty_spaces_from_another_erasure(
            self):
        self.pencil.write(self.paper, 'test the edit test a edit rest.')
        self.pencil.erase(self.paper, 'edit')
        self.pencil.erase(self.paper, 'test')
        self.pencil.edit(self.paper,
                         replacement_text='Pineapple',
                         method='erase',
                         erase_number=2)
        self.assertEqual('test the edit Pinea@ple   rest.',
                         self.paper.display_page())

    def test_that_pencil_correctly_edits_past_end_of_document_with_replacement_text(
            self):
        self.pencil.write(self.paper, 'test edit past end of page')
        self.pencil.erase(self.paper, 'page')
        self.pencil.edit(self.paper,
                         replacement_text='Pineapple',
                         method='erase',
                         erase_number=1)
        self.assertEqual('test edit past end of Pineapple',
                         self.paper.display_page())

    def test_that_pencil_correctly_writes_new_line_symbols_past_end_of_document(
            self):
        self.pencil.write(self.paper, 'test newline edit past end of page')
        self.pencil.erase(self.paper, 'page')
        self.pencil.edit(self.paper,
                         replacement_text='Pine\n\n\n',
                         method='erase',
                         erase_number=1)
        self.assertEqual('test newline edit past end of Pine\n\n\n',
                         self.paper.display_page())
Ejemplo n.º 28
0
 def setUp(self):
     self.paper = Paper()
     point_durability = 50
     eraser_durability = 20
     eraser = Eraser(eraser_durability)
     self.pencil = Pencil(point_durability=point_durability, eraser=eraser)
Ejemplo n.º 29
0
    def __do_search_paper(self, paper_name):
        if not any(self.__collection_enable_list):
            print('enable at least one collection first!')
            error = 'no collection'
            return error
        form_data = self.__form_data
        form_data['value(input1)'] = paper_name
        form_data['value(select1)'] = 'TI'
        s = requests.Session()
        r = s.post(self.__search_root, data=form_data, headers=self.__hearder)
        self.__require_number += 1
        soup = BeautifulSoup(r.text, 'html.parser')
        # print(soup)
        paper = None
        # 在搜索结果第一页中找title相等的论文,第一页没有匹配的就算找不到
        for all_paper_info in soup.select('div.search-results-item'):
            # title
            title = all_paper_info.select(
                'a.smallV110 value')[0].get_text().replace(' ', '|||')
            title = title.strip()
            title = title.replace('|||', ' ')
            if not ''.join(filter(str.isalnum, title)).lower() == ''.join(
                    filter(str.isalnum, paper_name)).lower():
                continue
            else:
                if paper:
                    error = 'more than one paper founded'
                    return paper, error
            # Times Cited
            cited_times_str = findall(
                r'\d',
                all_paper_info.select('div.search-results-data-cite')
                [0].get_text())[0]
            cited_times = int(cited_times_str)
            if cited_times > 0:
                cited_url = self.__root + all_paper_info.select(
                    'div.search-results-data-cite a')[0]['href']
            else:
                cited_url = ''

            paper_url = self.__root + all_paper_info.select(
                'a.smallV110')[0]['href']
            r = s.get(paper_url)
            self.__require_number += 1
            paper_soup = BeautifulSoup(r.text, 'html.parser')
            # journal
            journal = paper_soup.select('p.sourceTitle value')[0].get_text()
            authors = year = ids = None
            for possible_field in paper_soup.select('p.FR_field'):
                possible_str = possible_field.get_text()
                if not authors and possible_str.find(
                        'By:') >= 0 or possible_str.find(u'作者:') >= 0:
                    authors = findall('(?<=\\()(.+?)(?=\\))', possible_str)
                if not year and possible_str.find(
                        'Published:') >= 0 or possible_str.find(u'出版年:') >= 0:
                    year_str = findall(r'\d+', possible_str)[-1]
                    year = int(year_str)
                if not ids and possible_str.find(
                        'IDS Number:') >= 0 or possible_str.find(
                            u'IDS 号:') >= 0:
                    ids = findall(r'\w+', possible_str)[-1]
            paper = Paper(title, authors, journal, year, ids, cited_times,
                          cited_url)
        if paper:
            error = 'no error'
        else:
            error = 'no such paper'
        return paper, error
Ejemplo n.º 30
0
    def __do_search_cite_papers(self, paper, collection):
        cite_papers = []
        cite_url = paper.cited_url
        # 获得引用页面
        s = requests.Session()
        r = s.get(cite_url)
        self.__require_number += 1
        cite_soup = BeautifulSoup(r.text, 'html.parser')
        if collection is not None:
            span = cite_soup.select('span#CAScorecard_count_WOS' +
                                    COLLECTION_CN[collection])[0]
            if int(span.get_text()) is 0:
                return cite_papers, cite_url
            else:
                cite_url = self.__root + '/' + span.a['href'].replace(
                    ';jsessionid=' + r.cookies['JSESSIONID'], '')
                r = s.get(cite_url)
                self.__require_number += 1
                cite_soup = BeautifulSoup(r.text, 'html.parser')
        # 获得引用论文信息
        while True:  # 翻页直到最后一页
            papers_info = cite_soup.select('div.search-results-item')
            for paper_info in papers_info:
                title = paper_info.select(
                    'a.smallV110 value')[0].get_text().replace(' ', '|||')
                title = title.strip()
                title = title.replace('|||', ' ')
                # Times Cited
                cited_times_str = findall(
                    r'\d',
                    paper_info.select('div.search-results-data-cite')
                    [0].get_text())[0]
                cited_times = int(cited_times_str)
                if cited_times > 0:
                    cited_url = self.__root + paper_info.select(
                        'div.search-results-data-cite a')[0]['href']
                else:
                    cited_url = ''

                paper_url = self.__root + paper_info.select(
                    'a.smallV110')[0]['href']
                r = s.get(paper_url)
                self.__require_number += 1
                paper_soup = BeautifulSoup(r.text, 'html.parser')
                # journal
                journal = paper_soup.select(
                    'p.sourceTitle value')[0].get_text()
                # authors, years & IDS
                authors = year = ids = None
                for possible_field in paper_soup.select('p.FR_field'):
                    possible_str = possible_field.get_text()
                    if not authors and possible_str.find(
                            'By:') >= 0 or possible_str.find(u'作者:') >= 0:
                        authors = findall('(?<=\\()(.+?)(?=\\))', possible_str)
                    if not year and possible_str.find(
                            'Published:') >= 0 or possible_str.find(
                                u'出版年:') >= 0:
                        year_str = findall(r'\d+', possible_str)[-1]
                        year = int(year_str)
                    if not ids and possible_str.find(
                            'IDS Number:') >= 0 or possible_str.find(
                                u'IDS 号:') >= 0:
                        ids = findall(r'\w+', possible_str)[-1]
                paper = Paper(title, authors, journal, year, ids, cited_times,
                              cited_url)
                print(paper)
                cite_papers.append(paper)
            # 翻页直到最后一页
            total_page = int(
                cite_soup.select('span[id="pageCount.top"]')[0].get_text())
            current_page = int(
                cite_soup.select('input.goToPageNumber-input')[0]['value'])
            print('%d of % d' % (current_page, total_page))
            if current_page < total_page:
                r = s.get(cite_soup.select('a.paginationNext')[0]['href'])
                self.__require_number += 1
                cite_soup = BeautifulSoup(r.text, 'html.parser')
            else:
                break
        return cite_papers, cite_url
Ejemplo n.º 31
0
 def __init__(self, lock, logger):
     self.twitter = Twitter(logger)
     translate = Translate(logger)
     self.paper = Paper(logger, translate)
     self.lock = lock
     self.logger = logger
Ejemplo n.º 32
0
class EigoyurusanBot():
    def __init__(self, lock, logger):
        self.twitter = Twitter(logger)
        translate = Translate(logger)
        self.paper = Paper(logger, translate)
        self.lock = lock
        self.logger = logger

    def make_papers_text(self, titles: list, urls: list, *, prefix=''):
        if len("".join(titles)) == 0: prefix += "\n翻訳に失敗しました."

        length = len("\n".join([prefix] + [f'>{u}\n' for u in urls]))
        title_length = (280 - length) // 8
        trim = lambda title: title if len(
            title) <= title_length else title[:title_length - 2] + '..'
        text = "\n".join([prefix] +
                         [f'>{trim(t)}\n{u}' for u, t in zip(titles, urls)])
        return text

    def auto_follow(self):
        self.twitter.followback()

    def auto_tweet(self):
        '''
        Automatically tweets the content of papers
        searched for in random categories
        '''
        category, ret_list = self.paper.getOutputByRandom()
        urls, titles = zip(*ret_list)

        text = self.make_papers_text(titles, urls, prefix=f'#英許_{category[1]}')
        media_ids = self.twitter.upload_papers('./images/auto/eigoyurusan/')
        self.twitter.tweet(text, media_ids=media_ids)

    def reply(self):
        '''
        Get 200 replies to yourself in a tweet on
        the timeline and tweet an image of the
        resulting translation.
        '''
        timeline = self.twitter.get_mentions_custom()
        if len(timeline) == 0:
            self.logger.info("reply tweets doesn't exist.")
            return

        for status in reversed(timeline):
            self.twitter.try_create_favorite(status.id)
        for status in reversed(timeline):
            self.twitter.follow_if_not(status.author.id)
            screen_name = status.author.screen_name
            prefix = f"@{screen_name} "

            #inpが相手の返信内容
            keywords = status.text.replace('\n', ' ').split(" ")
            keywords = [k for k in keywords if "@" not in k]
            keywords = " ".join(keywords)
            keywords = "".join(
                [c for c in keywords if c not in emoji.UNICODE_EMOJI])
            self.logger.info(f"keywords {keywords} are sent by {screen_name}")

            #Keyword search Module
            ret_list, t_keyword = self.paper.getOutputByKeyword(
                screen_name, keywords)

            if len(ret_list) == 0:
                self.logger.info(f"no retlist, {t_keyword}")
                prefix += f'sorry no result for {t_keyword}'
                self.twitter.tweet(prefix, reply_to=status.id)
                continue

            urls, titles = zip(*ret_list)

            if t_keyword != keywords:
                prefix += f":{t_keyword}" if len(
                    t_keyword) < 40 else f":{t_keyword[:37]}..."
            text = self.make_papers_text(titles, urls, prefix=prefix)
            self.logger.info(text)
            media_ids = self.twitter.upload_papers(
                f'./images/reply/{screen_name}/')
            self.twitter.tweet(text, media_ids=media_ids, reply_to=status.id)
Ejemplo n.º 33
0
def get_info(pdf_file, txt_file):

    laparams = LAParams()
    pagenos = set()
    rsrcmgr = PDFResourceManager(caching=True)

    outtype = 'text'
    pages_to_extract = 1
    current_page = 0
    temp_author = None

    fp = file(pdf_file, 'rb')
    file_name = txt_file[:-3] + 'firstpage.txt'
    out_file = file(file_name, 'w')

    device = TextConverter(rsrcmgr,
                           out_file,
                           codec='utf-8',
                           laparams=laparams,
                           imagewriter=None)
    interpreter = PDFPageInterpreter(rsrcmgr, device)

    paper = Paper()
    paper.set_pages(
        PDFPage.get_num(fp,
                        pagenos,
                        maxpages=pages_to_extract,
                        password='',
                        caching=True,
                        check_extractable=True))
    laparams = LAParams()
    rsrcmgr = PDFResourceManager(caching=True)

    for page in PDFPage.get_pages(fp,
                                  pagenos,
                                  maxpages=pages_to_extract,
                                  password='',
                                  caching=True,
                                  check_extractable=True):
        page.rotate = (page.rotate) % 360
        interpreter.process_page(page)

    out_file.close()
    fp.close()
    device.close()

    paper.set_author(find_author(file_name))
    paper.set_page_range(find_range(file_name))
    paper.set_doi(find_doi(file_name))
    line = author_line_num(file_name)
    paper.set_title(find_title(file_name, line))
    paper.set_year(find_year(file_name))
    paper.set_publisher(find_publisher(file_name))

    os.remove(file_name)
    """
	info_filename = txt_file[:-3]+'info.txt'
 	paper.generate_citations(info_filename)
 		"""

    return paper