ํ์ฌ ํ๋ก์ ํธ ์ค .. ํฌ๋กค๋ง์ ํ๊ณ ์์๋ค
๋ด์ค ๊ธฐ์ฌ๋ค์ ๊ฐ์ง๊ณ ์ค๋ ํฌ๋กค๋ง์ด์๋๋ฐ,
๊ธฐ์ฌ ๋ด์ pdf๊ฐ ์์ผ๋ฉด ํ์ผ์ ๋ค์ด๋ฐ์์ ๊ฐ๋จํ๊ฒ text๋ง ์ถ์ถํด์ DB์ ๋ฃ์ด์ค๋ค.
๊ทผ๋ฐ pdf๊ฐ ์๋ ๊ธฐ์ฌ์ ์๋ ๊ธฐ์ฌ๋ฅผ ์คํฌ๋ฆฌํํ๋๋ฐ ์๊ฐ ์ฐจ์ด๊ฐ ๋ง์ด ๋๋ค๊ณ ๋๊ผ๋ค.
๊ทธ๋์ ํจ์๋ฅผ ์ฐพ์๊ฐ๋ค.
๊ธฐ์กด์ ์๋ ์ฝ๋:
def current_extract_pdf_text(pdf_url):
start_time = perf_counter()
response = requests.get(pdf_url, headers={'User-Agent': random.choice(user_agents)})
response.encoding = response.apparent_encoding
response.raise_for_status()
pdf_text = ""
with fitz.open(stream=response.content, filetype="pdf") as doc:
for i, page in enumerate(doc):
pdf_text += page.get_text() + "\n"
duration_ms = int((perf_counter() - start_time) * 1000)
print(f"current_extract_pdf_text duration_ms: {duration_ms}")
return pdf_text.strip()
๋ฑ 100์ฅ์ง๋ฆฌ pdf_url๋ก ํ ์คํธ๋ฅผ ํ๋ค.
duration์ ์ธก์ ํ๋ค.
current_extract_pdf_text duration_ms: 40476
์ถฉ๊ฒฉ์ ์ด์๋ค.
์๋ pdf์ ์๋ text๋ง ๊ฐ์ง๊ณ ์ค๋๋ฐ 40์ด๊ฐ ๊ฑธ๋ฆฐ๋ค๊ณ ?
์ผ๋ง์ ๊ต๋ณด๋ฌธ๊ณ ์ ๊ฐ๋ค๊ฐ ๋ฌด์จ ๋ฐ๋์ด ๋ถ์๋๋ python ์๋ฃ๊ตฌ์กฐ ์ฑ ์ด ๋๋ฌด ์ฌ๋ฐ์ด๋ณด์ฌ์ ์ฌ๋ฒ๋ ธ๋ค.
์ค๊ฐ๊น์ง๋ง ์ฝ๋ค๊ฐ ์ด๋์๋์ง ๋ชจ๋ฅด๊ฒ ์ง๋ง
์ผ๋ง์ ์ ์ฝ์ ์ฑ ์์ ๋ดค๋ค.
์ง๊ธ ๋ฌธ์ ๊ฐ ๋๋๊ฑด += ๋๋ฌธ์ด๋ผ๋ ๊ฑธ ๋ฐ๋ก ์์๋ค.
โ๏ธ ์ += ๋๋ฌธ์ ์ค๋ ๊ฑธ๋ฆฌ๋๊ฐ?
1. ๋ฃจํ๊ฐ ๋ ๋๋ง๋ค page.get_text() ๋ก ์๋ก์ด ๋ฌธ์์ด์ด ์๊ธด๋ค.
2. pdf_text์ ์ด ์๋ก์ด ๋ฌธ์์ด์ ๋ํ๋ ค๋ฉด, ํ์ด์ฌ์ ๊ธฐ์กด pdf_text์ ๋ด์ฉ๊ณผ ์๋ก์ด ๋ฌธ์์ด์ ํฉ์น ์์ ํ ์๋ก์ด ๋ฌธ์์ด์ ์ํ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ ํ ๋นํด์ผํ๋ค.
3. ๊ทธ๋ฆฌ๊ณ ์ด์ pdf_text์ ๋ชจ๋ ๋ด์ฉ์ ์๋ก ํ ๋น๋ ๊ณต๊ฐ์ ๋ณต์ฌํ ๋ค, ๊ทธ ๋ค์ ์๋ก์ด ๋ฌธ์์ด์ ๋ง๋ถ์ธ๋ค.
4. pdf_text ๋ณ์๋ ์ด์ ์ด '์๋ก์ด' ๋ฌธ์์ด์ ๊ฐ๋ฆฌํค๊ฒ ๋๋ค.
→ ์ด ๊ณผ์ ์ ํ์ด์ง๊ฐ ๋์ ๋ ์๋ก pdf_text์ ๊ธธ์ด๊ฐ ๊ธธ์ด์ง๊ธฐ ๋๋ฌธ์, ๋ฃจํ ํ๋ฐ๋ถ๋ก ๊ฐ์๋ก ๋ณต์ฌํด์ผ ํ ๋ฐ์ดํฐ์ ์์ด ๊ธฐํ๊ธ์์ ์ผ๋ก ๋์ด๋๋ค.
→ O(n²)์ ์๊ฐ ๋ณต์ก๋๋ฅผ ๊ฐ์ง๋ฉฐ ๋งค์ฐ ๋นํจ์จ์ !
์ด๋ ๊ฒ ๊ฐ์ ํ์ด์
→ list.append() ์ฌ์ฉ !!!
def new_extract_pdf_text(pdf_url: str) -> str:
start_time = perf_counter()
try:
response = requests.get(pdf_url, headers={'User-Agent': random.choice(user_agents)})
response.raise_for_status()
pdf_bytes = response.content
pdf_document = fitz.open(stream=pdf_bytes, filetype="pdf")
full_text = []
for page_num, page in enumerate(pdf_document):
full_text.append(page.get_text())
pdf_document.close()
duration_ms = int((perf_counter() - start_time) * 1000)
print(f"new_extract_pdf_text duration_ms: {duration_ms}")
return full_text
except requests.exceptions.RequestException as e:
return f"URL ์์ฒญ ์ค ์๋ฌ ๋ฐ์: {e}"
except Exception as e:
return f"PDF ์ฒ๋ฆฌ ์ค ์๋ฌ ๋ฐ์: {e}"
new_extract_pdf_text duration_ms: 1756
๊ฐ์ pdf_url๋ก ์์ฒญ์ ํ๋๋ฐ, 1.75์ด๊ฐ ๋์๋ฐ
40476ms์์ 1756ms๋ก ๊ฐ์ ํ์ผ๋,, 95.7% ๊ฐ์ ํ ๊ฒ,, ๋ ๋ฟ๋ฏํ๋ค
โ๏ธ list.append()๋ ์ ๋น ๋ฅธ๊ฐ!
1. ํ์ด์ฌ์ list๋ ๊ฐ๋ณ๊ฐ์ฒด๋ค. ๊ธฐ์กด ๋ฆฌ์คํธ์ ์์๋ฅผ ์ถ๊ฐํ๋ append() ์ฐ์ฐ์ ๋งค์ฐ ๋น ๋ฅด๋ค. O(1)
2. ๋ฃจํ๊ฐ ๋๋ ํ, "\n".join(full_text)๋ ์ต์ข ์ ์ผ๋ก ๋ง๋ค์ด์ง ๋ฌธ์์ด์ ์ ์ฒด ๊ธธ์ด๋ฅผ ๋ฏธ๋ฆฌ ๊ณ์ฐํ๋ค.
3. ๊ทธ ํฌ๊ธฐ์ ๋ง๋ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ ๋จ ํ ๋ฒ๋ง ํ ๋นํ๊ณ , ๋ฆฌ์คํธ์ ๋ชจ๋ ๋ฌธ์์ด์ ๊ทธ ๊ณต๊ฐ์ ์์๋๋ก ๋ณต์ฌํ์ฌ ์ต์ข ๋ฌธ์์ด์ ๋ง๋ ๋ค.

์๊ฐ ์ธก์ ๋ ํ๊ณ , ์ถ์ถ๋ text ํ์ธ์ฐจ txtํ์ผ๋ก ๋ฝ์๋ดค๋ค

์ถ์ถ๋๋ text์ ํ์ผ๋ก ๋ณํํด์ ํ์ธํด๋ณด๋ ..
๊ธฐ์กด ์ฝ๋๋ก ์ถ์ถ๋ ์ ๋ชป ๋๊ณ ์์๋ค? ์๋.. ์.. ํ์..
ํญ์ ํ๋ํ๋ ์ ํ์ธํ๊ณ ๋์ ํ์ฃผ๊ณ ์ฝ๋ ๋ด์ผ์ง! ๋ด๊ฐ ๋ค ์์ ํ ๊บผ์ผ ~~

'๐ฉ๐ปโ๐ป > python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [python] Fastapi ์ ํ๋ฆฌ์ผ์ด์ exe ํ์ผ๋ก ๋ฐฐํฌํ๊ธฐ (0) | 2024.11.05 |
|---|---|
| [python] socket ๋์ ์ฐ๊ฒฐ (2) | 2024.10.22 |
| [python] fastapi ์ค์๊ฐ ์คํธ๋ฆฌ๋ฐ ์ด๋ฏธ์ง ์ก์ถ (1) | 2024.10.21 |
| [python] TypeError: 'type' object is not subscriptable (1) | 2024.05.02 |
| [python] Received response with content-encoding: gzip, but failed to decode it. (0) | 2024.02.19 |