Python(英文のデータベース化005: SQLiteの組みこみ)

■英文のデータベース化のコードへSQLiteを組みこむ。
今回、英文のデータベース化004までで作成したコードにデータベースのコードを組みこむ。
以前考えたテーブルの構成をもとに、次のようにした。

文テーブル(文のID、文のカテゴリー、文本体、備考)
単語テーブル(単語のID、単語本体、文のID001、文のID001での意味、文のID002、文のID002での意味、文のID003、文のID003での意味、(途中省略)、 文のID020、文のID020での意味、備考)

テーブルの作成のコードは次の通り。文のID、単語のIDは、自動的に連番が入るようにする。
テーブル作成は普通にCREATE TABLEを使うだけ。テーブルが存在する場合は、IF NOT EXISTSを使ってSQLの中で避けるようにする。

def create_table():
    connect = sqlite3.connect(DB_NAME)
    cursor = connect.cursor()
    cursor.execute(
        'CREATE TABLE IF NOT EXISTS sentences(sID INTEGER PRIMARY KEY, Category text, Sentence text, Note text)')
    cursor.execute(
        'CREATE TABLE IF NOT EXISTS words(ID INTEGER PRIMARY KEY, Word text, sID001 nchar(4), mean_sID001 text, sID002 nchar(4), mean_sID002 text, sID003 nchar(4), mean_sID003 text, sID004 nchar(4), mean_sI004 text ,sID005 nchar(4), mean_sI005 text,sID006 nchar(4), mean_sI006 text ,sID007 nchar(4), mean_sI007 text,sID008 nchar(4), mean_sI008 text ,sID009 nchar(4), mean_sI009 text ,sID010 nchar(4), mean_sI010 text ,sID011 nchar(4), mean_sI011 text ,sID012 nchar(4), mean_sI012 text ,sID013 nchar(4), mean_sI013 text ,sID014 nchar(4), mean_sI014 text ,sID015 nchar(4), mean_sI015 text ,sID016 nchar(4), mean_sI016 text ,sID017 nchar(4), mean_sI017 text ,sID018 nchar(4), mean_sI018 text ,sID019 nchar(4), mean_sI019 text ,sID020 nchar(4), mean_sI020 text ,Note text)')
    connect.close()


文をテーブルに入れるコードは次の通り。
文のリストとカテゴリーを引数として、テーブルにデータを入力するとともに文のリストに対するIDを返す。IDはテーブルの中で自動連番されるので、テーブルから取得する必要がある。

def linelist_to_DB(linelist, category):
    connect = sqlite3.connect(DB_NAME)
    cursor = connect.cursor()
    for line in linelist:
        lineData = [(line, category)]
        cursor.executemany('INSERT INTO sentences values(null,?,?,"")', lineData)
    connect.commit()

    reversed_sID_list = []
    sID_list = []
    for row in cursor.execute('SELECT sID FROM sentences order by sID desc limit ' + str(len(linelist))):
        reversed_sID_list.append(str(row[0]))
    connect.close()

    for sID in reversed(reversed_sID_list):
        sID_list.append(sID)

    print(sID_list)
    return sID_list

テーブルからIDを取得するために下記SQL文を使っている。order by descで降順にし、limitで取得するレコードの数を文のリストだけに制限する。これで、今回の登録分だけのIDが降順に取得できる。
cursor.execute('SELECT sID FROM sentences order by sID desc limit ' + str(len(linelist)))

その後の処理として、reversedをfor文の中で使って、リストを逆順に操作する。普通は [0, 1, 2, 3] というリストがあれば、0から順に操作されるけど、reversed(list)とすれば、3から順に操作される。上のSQL文で、[3, 2, 1, 0]のように降順にデータが入っているので、[0, 1, 2, 3]の順に戻す。

文に対する処理が終わったら、単語に分解して、文と同様にデータベースへ登録する。

サンプルとして次の ISO27001 Introduction を試してみると、下の画面のように登録された。

This International Standard has been prepared to provide requirements for establishing, implementing, maintaining and continually improving an information security management system. The adoption of an information security management system is a strategic decision for an organization. The establishment and implementation of an organization’s information security management system is influenced by the organization’s needs and objectives, security requirements, the organizational processes used and the size and structure of the organization. All of these influencing factors are expected to change over time.
The information security management system preserves the confidentiality, integrity and availability of information by applying a risk management process and gives confidence to interested parties that risks are adequately managed.
It is important that the information security management system is part of and integrated with the organization’s processes and overall management structure and that information security is considered in the design of processes, information systems, and controls. It is expected that an information security management system implementation will be scaled in accordance with the needs of the organization.
This International Standard can be used by internal and external parties to assess the organization's ability to meet the organization’s own information security requirements.
The order in which requirements are presented in this International Standard does not reflect their importance or imply the order in which they are to be implemented. The list items are enumerated for reference purpose only.
ISO/IEC 27000 describes the overview and the vocabulary of information security management systems, referencing the information security management system family of standards (including ISO/IEC 27003[2], ISO/IEC 27004[3] and ISO/IEC 27005[4]), with related terms and definitions.


文テーブル(sentences)
Category部分は、今のところsampleで固定。

単語テーブル(words)
1文目(sID001)の部分のみ。三人称単数の s などの処理のため単語を編集するコードを入れているので、少しおかしくなっている部分もあるけど、単語ごとに登録できている。今回はデータベースへの登録の確認のため、同じ単語も別レコードとして登録している。

文と単語をデータベースに登録するところまでできた。次は、同じ単語の場合は、別レコードへの登録でなく、すでに登録されている単語のレコードを更新(update)する形にできないか考えてみよう。