def init(): ChitChat.processor = ChitChatProcessor(Filter()) #initialize dictionary of chat words db_conn = SQLiteConn('db/ccie.db') query = "SELECT sani_utterance FROM DATA_PHRASES;" chat_phrases = db_conn.query(query) for phrase in chat_phrases: for entry in phrase: words = entry.split(" ") for word in words: ChitChat.chat_words[word] = 1 #initialize the syntactic pattern matching pattern_query = "SELECT * FROM DATA_POS_PATTERNS;" pos_patterns = db_conn.query(pattern_query) for pos_pattern in pos_patterns: ChitChat.pos_phrases[pos_pattern[0]] = pos_pattern[0] db_conn.close()
def add_to_db(self, msg_pairs): db_conn = SQLiteConn('db/ccie.db') ids = [] for msg_p in msg_pairs: insert_statement1 = ('INSERT INTO DATA_PHRASES (orig_utterance, sani_utterance) VALUES ("%s", "%s");' % (msg_p[0], msg_p[1])) try: db_conn.query(insert_statement1, True) except sqlite3.IntegrityError: pass #print insert_statement1 query = ("SELECT id FROM DATA_PHRASES WHERE sani_utterance = '" + msg_p[1] + "';") results = None try: result = db_conn.query(query) except sqlite3.IntegrityError: pass if result: ids.append((result[0][0], msg_p[1])) # print ids for x in range(len(msg_pairs)-1): # print ids[x], ids[x+1] update_query = ("INSERT INTO DATA_CONVO_PAIRS ( utterance_id, response_id )" " VALUES (%s, '%s');" % (ids[x][0], ids[x+1][0])) try: db_conn.query(update_query, True) except sqlite3.IntegrityError: pass