0

我已经为此工作了一段时间,试图创建一个频率分布数据库端:

from itertools import permutations
import sqlite3

def populate_character_probabilities(connection, table="freq_dist", alphabet='abcdefghijklmnopqrstuvwxyz'):
    c = connection.cursor()
    c.execute("DROP TABLE IF EXISTS tablename".replace('tablename', table))
    c.execute("create table tablename (char1 text, char2 text, freq integer);".replace("tablename", table))    
    char_freq_tuples = [x + (1,) for x in list(permutations(alphabet, 2)) + [(alpha, alpha) for alpha in alphabet + 'S' + 'E']]
    c.executemany("insert into tablename values (?,?,?);".replace("tablename", table), char_freq_tuples)
    connection.commit()
    c.close()

def populate_word_list(connection, table="word_list"):
    cursor = connection.cursor()
    cursor.execute("DROP TABLE IF EXISTS tablename".replace('tablename', table))
    cursor.execute("create table tablename (word text);".replace('tablename', table))
    cursor.executemany("insert into tablename values (?)".replace('tablename', table), [[u'nabisco'], [u'usa'], [u'sharp'], [u'rise']])
    connection.commit()

def update_freq_dist(connection, word_list="word_list", freq_dist="freq_dist"):
    cursor = connection.cursor()
    subset = cursor.execute("select * from tablename;".replace("tablename", word_list)).fetchmany(5)
    for elem in subset: # want to process e.g.: 5 at a time
        elem = 'S' + elem[0] + 'E' # Start and end delimiters
        for i in xrange(len(elem) - 1):
            freq = cursor.execute("SELECT freq FROM tablename WHERE char1=? and char2=?;".replace("tablename", freq_dist), (elem[i], elem[i + 1])).fetchone()
            cursor.execute("UPDATE tablename SET freq=? WHERE char1=? and char2=?;".replace("tablename", freq_dist), (freq + 1, elem[i], elem[i + 1]))
    connection.commit() # seems inefficient having two queries here^
    cursor.close()

if __name__ == '__main__':
    connection = sqlite3.connect('array.db')
    populate_word_list(connection)
    populate_character_probabilities(connection)
    update_freq_dist(connection)
    cursor = connection.cursor()
    print cursor.execute("SELECT * FROM freq_dist;").fetchmany(10)

(哇,测试用例的 180 行代码库减少到 37 行!:D - 请注意,实际的单词列表是 2900 万而不是 4 !!!)

我意识到:

  • 我不应该update_freq_dist在内循环中需要两个查询
  • 有一种方法可以遍历数据库元素(行),例如:一次 5 个

但是我不确定如何解决这两个问题。

你能想出解决办法吗?

4

1 回答 1

1

您只想用 + 1 更新频率吗?

UPDATE tablename
SET freq = freq + 1
WHERE char1=? and char2=?;

或者,如果您从另一个表更新:

UPDATE tablename 
SET freq = t2.freq + 1 -- whatever your calc is
FROM tablename t1
JOIN othertable t2
ON t1.other_id = t2.id
WHERE t1.char1=? and t1.char2=? and t2.char1=? and t2.char2=?

至于一次迭代 5 个 - 您可以使用limit 和 offset子句来接近。

于 2012-06-11T13:36:18.530 回答