{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":59575,"databundleVersionId":8060720,"sourceType":"competition"},{"sourceId":7731449,"sourceType":"datasetVersion","datasetId":4517815},{"sourceId":166738328,"sourceType":"kernelVersion"}],"dockerImageVersionId":30664,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import whoosh_utils","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-03-11T23:25:56.491770Z","iopub.execute_input":"2024-03-11T23:25:56.492150Z","iopub.status.idle":"2024-03-11T23:26:07.373416Z","shell.execute_reply.started":"2024-03-11T23:25:56.492117Z","shell.execute_reply":"2024-03-11T23:26:07.372641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Basic setup","metadata":{}},{"cell_type":"code","source":"train_idx = whoosh_utils.load_index('/kaggle/input/uspto-explainable-ai/train_index')\nsearcher = whoosh_utils.get_searcher(train_idx)\nqp = whoosh_utils.get_query_parser()","metadata":{"execution":{"iopub.status.busy":"2024-03-11T23:30:17.867339Z","iopub.execute_input":"2024-03-11T23:30:17.867958Z","iopub.status.idle":"2024-03-11T23:30:26.752225Z","shell.execute_reply.started":"2024-03-11T23:30:17.867870Z","shell.execute_reply":"2024-03-11T23:30:26.751456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Run a simple query","metadata":{}},{"cell_type":"code","source":"query = 'ti:balloons OR ti:string'\nwhoosh_utils.execute_query(query, qp, searcher)[:5]","metadata":{"execution":{"iopub.status.busy":"2024-03-11T23:31:28.915705Z","iopub.execute_input":"2024-03-11T23:31:28.915997Z","iopub.status.idle":"2024-03-11T23:31:28.925657Z","shell.execute_reply.started":"2024-03-11T23:31:28.915974Z","shell.execute_reply":"2024-03-11T23:31:28.924689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Verify how a query will be parsed","metadata":{"execution":{"iopub.status.busy":"2024-03-11T23:30:57.835276Z","iopub.execute_input":"2024-03-11T23:30:57.835916Z","iopub.status.idle":"2024-03-11T23:30:57.840362Z","shell.execute_reply.started":"2024-03-11T23:30:57.835863Z","shell.execute_reply":"2024-03-11T23:30:57.839648Z"}}},{"cell_type":"code","source":"# This works as intended\nquery = 'ti:bread ADJ5 cheese'\nqp.parse(query)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T23:31:40.450337Z","iopub.execute_input":"2024-03-11T23:31:40.450645Z","iopub.status.idle":"2024-03-11T23:31:40.457098Z","shell.execute_reply.started":"2024-03-11T23:31:40.450621Z","shell.execute_reply":"2024-03-11T23:31:40.456059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Specifying the field separately for each term does not\nquery = 'ti:bread ADJ5 ti:cheese'\nqp.parse(query)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Setting the adjacency value too high breaks the operator\nquery = 'ti:bread ADJ1234 cheese'\nqp.parse(query)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T23:32:50.277452Z","iopub.execute_input":"2024-03-11T23:32:50.277755Z","iopub.status.idle":"2024-03-11T23:32:50.284571Z","shell.execute_reply.started":"2024-03-11T23:32:50.277733Z","shell.execute_reply":"2024-03-11T23:32:50.283693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Basic wildcards work as expected\nquery = 'ti:br* OR ti:cheese'\nqp.parse(query)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T23:33:57.713819Z","iopub.execute_input":"2024-03-11T23:33:57.714145Z","iopub.status.idle":"2024-03-11T23:33:57.721115Z","shell.execute_reply.started":"2024-03-11T23:33:57.714122Z","shell.execute_reply":"2024-03-11T23:33:57.720108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Wildcards don't work in combination with adjacency operators: near gets parsed as a string literal.\n# The query will generate results, but not as expected.\nquery = 'ti:br* NEAR cheese'\nqp.parse(query)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T23:34:46.551304Z","iopub.execute_input":"2024-03-11T23:34:46.551632Z","iopub.status.idle":"2024-03-11T23:34:46.557431Z","shell.execute_reply.started":"2024-03-11T23:34:46.551605Z","shell.execute_reply":"2024-03-11T23:34:46.556831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Be warned that the query parser isn't perfectly accurate. In this case it fails to warn that proximity operators won't work on the CPC field.","metadata":{}},{"cell_type":"code","source":"query = 'cpc:B60H1/00357 NEAR cpc:B60H1/00885'\nqp.parse(query)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"try:\n    whoosh_utils.execute_query(query, qp, searcher)\nexcept:\n    print(\"You can't use ADJ or NEAR on the CPC field.\")","metadata":{},"execution_count":null,"outputs":[]}]}