From 66ecf28d15403716adc511137691d86bbc6dedab Mon Sep 17 00:00:00 2001 From: zj3D Date: Thu, 7 Mar 2024 19:26:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9D=8E=E5=85=B5=E6=8F=90=E8=AE=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 计算设备/数据库/ORM/DataQuery.py | 28 ++++++++++ .../ORM/__pycache__/createDb.cpython-38.pyc | Bin 0 -> 1469 bytes 计算设备/数据库/ORM/createDb.py | 43 +++++++++++++++ 计算设备/数据库/ORM/processData.py | 49 ++++++++++++++++++ 4 files changed, 120 insertions(+) create mode 100644 计算设备/数据库/ORM/DataQuery.py create mode 100644 计算设备/数据库/ORM/__pycache__/createDb.cpython-38.pyc create mode 100644 计算设备/数据库/ORM/createDb.py create mode 100644 计算设备/数据库/ORM/processData.py diff --git a/计算设备/数据库/ORM/DataQuery.py b/计算设备/数据库/ORM/DataQuery.py new file mode 100644 index 0000000..73830b8 --- /dev/null +++ b/计算设备/数据库/ORM/DataQuery.py @@ -0,0 +1,28 @@ +from cppy.cp_util import testfilepath + +from sqlalchemy.orm import sessionmaker +from createDb import TextFile, WordFrequency,engine + + +def get_top_n_word_frequencies(filepath, n=10): + Session = sessionmaker(bind=engine) + session = Session() + textfile = session.query(TextFile).filter_by(filepath=filepath).first() + if textfile: + # 查询词频并按频率降序排序,然后取前N个 + word_freqs = (session.query(WordFrequency) + .filter_by(textfile=textfile) + .order_by(WordFrequency.frequency.desc()) + .limit(n) + .all()) + # 输出词频最高的N个词 + for wf in word_freqs: + print(f"{wf.word}: {wf.frequency}") + else: + print(f"File {filepath} not found in the database.") + session.close() + + +if __name__ == '__main__': + get_top_n_word_frequencies( testfilepath ) + \ No newline at end of file diff --git a/计算设备/数据库/ORM/__pycache__/createDb.cpython-38.pyc b/计算设备/数据库/ORM/__pycache__/createDb.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..9de75d70923e09028b5d2049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