def composeLinesAtIndexes(pageID, poem_form, dbconfig, search_groups, composed_lines, indexes, callback=None, user_info=None): dbconn = dbconnect.MySQLDatabaseConnection(dbconfig["database"], dbconfig["user"], dbconfig["host"], dbconfig["password"]) ret_composed_lines = copy.deepcopy(composed_lines) for idx in indexes: if ret_composed_lines[idx] is None: hard_constraints = hardConstraints(idx, poem_form, composed_lines) # print ("pid: " + str(os.getpid())) # for l in composed_lines: # print ("\t" + str(l)) # print ("\n") flexible_constraints = flexibleConstraints(idx, poem_form, ret_composed_lines) possible_lines = computePossibleLines(dbconn, hard_constraints, flexible_constraints, search_groups, ret_composed_lines) previous_line = None if not poem_form.lines[idx].starts and ret_composed_lines[idx-1]: previous_line = dbreader.textForLineID(dbconn, ret_composed_lines[idx-1]['id']) next_lines = getBestLines(dbconn, hard_constraints, possible_lines, poem_form, idx, previous_line=previous_line, count=1) if len(next_lines) > 0: ret_composed_lines[idx] = next_lines[0] composed_lines[idx] = ret_composed_lines[idx] if callback is not None: callback(ret_composed_lines, user_info) dbconn.close() return ret_composed_lines
def addTextToLines(dbconn, lines): for line in lines: line_text = dbreader.textForLineID(dbconn, line['id']) line['text'] = line_text
outf = None if args.output: outf = codecs.open(args.output, 'w', 'utf-8') dbconfig = dbconnect.MySQLDatabaseConnection.dbconfigForName('local') dbconn = dbconnect.MySQLDatabaseConnection.connectionWithConfiguration('local') cursor = dbconn.connection.cursor() query = ( """SELECT view_counts.id FROM view_counts INNER JOIN page_categories""" """ ON page_categories.id = view_counts.id WHERE view_counts.count < 202917""" """ ORDER BY view_counts.count DESC LIMIT %s;""" ) values = (10000, ) cursor.execute(query, values) res = cursor.fetchall() for i in range(args.count): random_id = random.sample(res, 1)[0][0] # random_id = 35607283 poem = wikibard.poemForPageID(random_id, 'elizabethan', dbconfig, multi=True) lines = [dbreader.textForLineID(dbconn, p['id']) for p in poem] if outf: outf.write(dbreader.pageTitleForPageID(dbconn, random_id)) outf.write(u'\n\n') for l in lines: outf.write(l) outf.write(u'\n') outf.write(u"\n\n\n") if outf: outf.close() t.end("poems") t.printTime()