Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

Β 

History

7 Commits

Folders and files

Repository files navigation

Text Query Engine

A lightweight Boolean Query Search Engine implemented in Python.


πŸ‡°πŸ‡· ν•œκ΅­μ–΄

ν”„λ‘œμ νŠΈ μ†Œκ°œ

Text Query Engine은 μ—¬λŸ¬ 개의 ν…μŠ€νŠΈ λ¬Έμ„œλ₯Ό λΉ λ₯΄κ²Œ κ²€μƒ‰ν•˜κΈ° μœ„ν•œ Boolean Query 기반 검색 μ—”μ§„μž…λ‹ˆλ‹€.

맀번 λͺ¨λ“  λ¬Έμ„œλ₯Ό 순차적으둜 κ²€μƒ‰ν•˜λŠ” λŒ€μ‹ , λ¬Έμ„œλ₯Ό 미리 μ „μ²˜λ¦¬ν•˜μ—¬ 역색인(Inverted Index) 을 μƒμ„±ν•œ ν›„ 이λ₯Ό μ΄μš©ν•΄ λΉ λ₯΄κ²Œ κ²€μƒ‰ν•©λ‹ˆλ‹€.

λ˜ν•œ λ³€κ²½λœ λ¬Έμ„œλ§Œ λ‹€μ‹œ μΈλ±μ‹±ν•˜λŠ” 증뢄 인덱싱(Incremental Indexing) 을 μ§€μ›ν•˜μ—¬ λŒ€λŸ‰μ˜ λ¬Έμ„œμ—μ„œλ„ 효율적으둜 λ™μž‘ν•©λ‹ˆλ‹€.


μ£Όμš” κΈ°λŠ₯

  • μ—¬λŸ¬ 개의 ν…μŠ€νŠΈ λ¬Έμ„œ 인덱싱
  • 증뢄 인덱싱(λ³€κ²½λœ λ¬Έμ„œλ§Œ μž¬μΈλ±μ‹±)
  • Boolean Query 검색
    • AND (&&)
    • OR (||)
    • NOT (!)
    • κ΄„ν˜Έ (())
  • 파일λͺ…, 라인 번호, 원본 λ¬Έμž₯ 좜λ ₯
  • Stopword 제거
  • μœ μ§€λ³΄μˆ˜κ°€ μ‰¬μš΄ λͺ¨λ“ˆ ꡬ쑰

ν”„λ‘œμ νŠΈ ꡬ쑰

text-query-engine/
β”‚
β”œβ”€β”€ main.py              # ν”„λ‘œκ·Έλž¨ μ‹œμž‘μ 
β”œβ”€β”€ build_index.py       # 인덱슀 생성 및 μ—…λ°μ΄νŠΈ
β”œβ”€β”€ search.py            # Boolean Query 검색
β”œβ”€β”€ indexer.py           # 역색인 생성
β”œβ”€β”€ storage.py           # 데이터 μ €μž₯/뢈러였기
β”œβ”€β”€ utils.py             # μ „μ²˜λ¦¬ μœ ν‹Έλ¦¬ν‹°
β”œβ”€β”€ stopwords.py         # λΆˆμš©μ–΄ λͺ©λ‘
β”‚
β”œβ”€β”€ documents/           # 원본 λ¬Έμ„œ
β”‚
└── index/
    β”œβ”€β”€ master.pkl       # 전체 역색인
    β”œβ”€β”€ metadata.pkl     # 파일 메타데이터
    └── files/           # λ¬Έμ„œλ³„ 인덱슀

ν”„λ‘œκ·Έλž¨ λ™μž‘ κ³Όμ •

1. 인덱싱

λ¬Έμ„œλ₯Ό 읽은 ν›„ λ‹€μŒ 과정을 μˆ˜ν–‰ν•©λ‹ˆλ‹€.

λ¬Έμ„œ 읽기
    β”‚
    β–Ό
μ†Œλ¬Έμž λ³€ν™˜
    β”‚
    β–Ό
특수문자 제거
    β”‚
    β–Ό
토큰화(Tokenize)
    β”‚
    β–Ό
λΆˆμš©μ–΄ 제거
    β”‚
    β–Ό
역색인 생성
    β”‚
    β–Ό
PKL 파일 μ €μž₯

2. 검색

μ‚¬μš©μžκ°€ 검색어λ₯Ό μž…λ ₯ν•˜λ©΄

(apple || banana) && !orange

λ‹€μŒ 과정을 μˆ˜ν–‰ν•©λ‹ˆλ‹€.

  1. Query Tokenize
  2. Boolean μ—°μ‚° 계산
  3. 역색인 검색
  4. κ²°κ³Ό 좜λ ₯

좜λ ₯ μ˜ˆμ‹œ

book1.txt
(3) Apple is a fruit.

book2.txt
(15) Banana and apple are popular.

역색인(Inverted Index)

일반적인 λ¬Έμ„œλŠ”

book1

apple banana

book2

banana orange

처럼 μ €μž₯λ©λ‹ˆλ‹€.

ν•˜μ§€λ§Œ 검색 μ—”μ§„μ—μ„œλŠ”

apple
    β†’ (book1, line 1)

banana
    β†’ (book1, line 1)
    β†’ (book2, line 1)

orange
    β†’ (book2, line 1)

처럼 단어 β†’ λ¬Έμ„œ μœ„μΉ˜ ν˜•νƒœλ‘œ μ €μž₯ν•©λ‹ˆλ‹€.

이λ₯Ό 역색인(Inverted Index) 이라고 ν•˜λ©° 검색 속도λ₯Ό 크게 ν–₯μƒμ‹œν‚΅λ‹ˆλ‹€.


증뢄 인덱싱

ν”„λ‘œκ·Έλž¨μ€ λ³€κ²½λœ λ¬Έμ„œλ§Œ λ‹€μ‹œ μΈλ±μ‹±ν•©λ‹ˆλ‹€.

예λ₯Ό λ“€μ–΄

book1.txt   λ³€κ²½ μ—†μŒ
book2.txt   μˆ˜μ •λ¨
book3.txt   λ³€κ²½ μ—†μŒ

이라면

book2.txt

만 λ‹€μ‹œ μΈλ±μ‹±ν•©λ‹ˆλ‹€.

이λ₯Ό 톡해 λΆˆν•„μš”ν•œ μž‘μ—…μ„ 쀄이고 μ‹€ν–‰ μ‹œκ°„μ„ λ‹¨μΆ•ν•©λ‹ˆλ‹€.


Boolean Query 지원

지원 μ—°μ‚°μž

μ—°μ‚°μž μ„€λͺ…
&& AND
|| OR
! NOT
() κ΄„ν˜Έ

μ˜ˆμ‹œ

apple
apple && banana
apple || orange
apple && !banana
(apple || banana) && !orange

각 λͺ¨λ“ˆ μ„€λͺ…

main.py

ν”„λ‘œκ·Έλž¨μ˜ μ‹œμž‘μ μž…λ‹ˆλ‹€.

메뉴λ₯Ό 좜λ ₯ν•˜κ³  각 κΈ°λŠ₯을 μ‹€ν–‰ν•©λ‹ˆλ‹€.


build_index.py

documents 폴더λ₯Ό κ²€μ‚¬ν•˜μ—¬

  • μƒˆ λ¬Έμ„œ
  • μˆ˜μ •λœ λ¬Έμ„œ
  • μ‚­μ œλœ λ¬Έμ„œ

λ₯Ό ν™•μΈν•˜κ³  인덱슀λ₯Ό κ°±μ‹ ν•©λ‹ˆλ‹€.


indexer.py

λ¬Έμ„œλ₯Ό 읽고

  • μ „μ²˜λ¦¬
  • 단어 λΉˆλ„ 계산
  • 역색인 생성

을 μˆ˜ν–‰ν•©λ‹ˆλ‹€.


search.py

Boolean Queryλ₯Ό κ³„μ‚°ν•˜μ—¬

  • 검색
  • κ²°κ³Ό 좜λ ₯

을 λ‹΄λ‹Ήν•©λ‹ˆλ‹€.


storage.py

PKL 파일 μ €μž₯ 및 λ‘œλ“œλ₯Ό λ‹΄λ‹Ήν•©λ‹ˆλ‹€.


utils.py

ν”„λ‘œμ νŠΈ μ „μ²΄μ—μ„œ μ‚¬μš©ν•˜λŠ”

  • μ „μ²˜λ¦¬
  • 토큰화
  • 파일λͺ… λ³€ν™˜

λ“±μ˜ 곡톡 ν•¨μˆ˜λ₯Ό μ œκ³΅ν•©λ‹ˆλ‹€.


stopwords.py

검색 μ„±λŠ₯ ν–₯상을 μœ„ν•΄ λΆˆν•„μš”ν•œ 단어λ₯Ό μ œκ±°ν•˜λŠ” Stopword λͺ©λ‘μž…λ‹ˆλ‹€.


μ‹€ν–‰ 방법

인덱슀 생성

python build_index.py

검색 μ‹€ν–‰

python search.py

λ˜λŠ”

python main.py

ν–₯ν›„ κ°œμ„  μ˜ˆμ •

  • Phrase Query
  • Wildcard Search
  • TF-IDF Ranking
  • BM25 Ranking
  • PDF 검색
  • DOCX 검색
  • μ›Ή μΈν„°νŽ˜μ΄μŠ€
  • λ©€ν‹°μŠ€λ ˆλ“œ 인덱싱

πŸ‡ΊπŸ‡Έ English

Overview

Text Query Engine is a lightweight Boolean Query Search Engine written in Python.

Instead of scanning every document for every search, the engine preprocesses documents once, builds an Inverted Index, and performs fast searches using indexed data.

It also supports Incremental Indexing, allowing only newly added or modified documents to be re-indexed.


Features

  • Index multiple text documents
  • Incremental indexing
  • Boolean Query search
    • AND (&&)
    • OR (||)
    • NOT (!)
    • Parentheses (())
  • Display filename, line number, and original sentence
  • Stopword removal
  • Modular project architecture

Project Structure

text-query-engine/
β”‚
β”œβ”€β”€ main.py
β”œβ”€β”€ build_index.py
β”œβ”€β”€ search.py
β”œβ”€β”€ indexer.py
β”œβ”€β”€ storage.py
β”œβ”€β”€ utils.py
β”œβ”€β”€ stopwords.py
β”‚
β”œβ”€β”€ documents/
β”‚
└── index/
    β”œβ”€β”€ master.pkl
    β”œβ”€β”€ metadata.pkl
    └── files/

Workflow

Indexing

Document
    β”‚
    β–Ό
Normalize text
    β”‚
    β–Ό
Tokenize
    β”‚
    β–Ό
Remove stopwords
    β”‚
    β–Ό
Build inverted index
    β”‚
    β–Ό
Save document index

Searching

Example query

(apple || banana) && !orange

The engine

  1. Tokenizes the query
  2. Evaluates Boolean operators
  3. Searches the master index
  4. Displays matching results

Inverted Index

Instead of storing

book1

apple banana

book2

banana orange

the engine stores

apple
    β†’ (book1, line 1)

banana
    β†’ (book1, line 1)
    β†’ (book2, line 1)

orange
    β†’ (book2, line 1)

This structure enables much faster searching.


Future Work

  • Phrase Query
  • Wildcard Search
  • TF-IDF
  • BM25
  • PDF Search
  • DOCX Search
  • Web Interface
  • Multi-threaded Indexing

Author

Developed as a course project for learning Information Retrieval and Search Engine fundamentals.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages