Skip to content
Snippets Groups Projects
Commit bed619e3 authored by Mesharo's avatar Mesharo
Browse files

filtering postgresql questions

parent e5ef3d39
No related merge requests found
# SQL-schemas
Bachelor's thesis - Database schema extraction from a collection of questions on StackOverflow using SQL analysis.
Extrakce schématu databáze z kolekce otázek na StackOverflow s využitím analýzy SQL
StackOverflow je jedna z největších question and answer sítí. Je zde statisíce otázek s tématem SQL a velké množství SQL dotazů. Pro další analýzu SQL dotazů by byla vhodná znalost schématu databáze, kterých se dotazy týkají. Schéma bývá v textu uvedeno v různých formách. Cílem této práce je zautomatizovat čtení schémat pro co největší množství SQL.
- Seznámení se s možnostmi parsrování SQL a extrakce schématu z SQL dotazu.
- Implementace knihovny, která pro vstupní SQL dotaz vrátí seznam tabulek spolu se seznamem atributů a případně i s návrhy datových typů jednotlivých atributů.
- Příprava trénovací kolekce pro umělou inteligenci, kde pro StackOverflow dotaz bude uveden očekávaný DDL skript. Kolekce bude mít alespoň sto prvků.
- Implementace ověřovací aplikace, která pro DDL skript vykoná dotaz.
main.py 0 → 100644
import re
from bigxml import Parser, xml_handle_element
import time
# wanna find postgresql and postgresql-version tags
def get_postgresql_tags(file_name: str) -> list:
result = []
file = open(file_name, 'r', encoding='utf8')
for row in file:
id_tagname = re.search('<row Id="(.+?)" TagName="(.+?)"', row)
if not id_tagname:
# Either meta data or <tags> element.
continue
tagname = id_tagname.group(2)
if tagname != 'postgresql':
postgresql_version = re.search('postgresql-(.+?)', tagname)
if not postgresql_version:
continue
try:
float(postgresql_version.group(1))
except ValueError:
# Not a version of postgresql.
continue
result.append(tagname)
file.close()
return result
@xml_handle_element('posts', 'row')
def handler(node):
id = node.attributes['Id']
body = node.attributes['Body']
try:
tags = node.attributes['Tags']
except KeyError:
tags = 'N/A'
try:
accepted_answer_id = node.attributes['AcceptedAnswerId']
except KeyError:
accepted_answer_id = 'N/A'
yield {
'id': id,
'body': body,
'tags': tags,
'accepted_answer_id': accepted_answer_id
}
# filter out wanted questions and save them in a separate file for quicker access
def filter_postgresql_questions(file_name: str) -> None:
postgresql_tags = get_postgresql_tags('D:\stackoverflow.com\Tags.xml')
output_file = open('D:\postgresql_questions.txt', 'a', encoding='utf8')
count = 0
start = time.time()
with open(file_name, 'rb') as XML_file:
for item in Parser(XML_file).iter_from(handler):
for correct_tag in postgresql_tags:
if correct_tag in item['tags']:
output_file.write(str(item) + '\n')
print(count)
count += 1
end = time.time()
print(f'Time elapsed: {(end - start) / 60}')
output_file.close()
if __name__ == "__main__":
filter_postgresql_questions('D:\stackoverflow.com\Posts.xml')
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment