Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KeyError Solution w/o Library #53

Open
cho104 opened this issue Sep 27, 2024 · 1 comment
Open

KeyError Solution w/o Library #53

cho104 opened this issue Sep 27, 2024 · 1 comment

Comments

@cho104
Copy link

cho104 commented Sep 27, 2024

Seems to be working properly on a large dataset (used 40k sentence dataset) (Last time checked: Oct 2024)
This may not provide all the features offered by the original library.

  • You may need to implement a feature to skip sentence after a few rejections as the api refuses to spell check certain sentences.
import requests
import json
import re

class SpellChecker:
    def __init__(self):
        self.passport_key = None
        self.base_url = None

    def fetch_passport_key(self):
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36',
            'Referer': 'https://search.naver.com/',
        }
        response = requests.get("https://search.naver.com/search.naver?query=%EB%A7%9E%EC%B6%A9%EB%B2%95%20%EA%B2%80%EC%82%AC%EA%B8%B0", headers=headers)
        passport_key_match = re.search(r'(?<={new SpellingCheck\({API:{checker:").*?(?="},selector)', response.text)
        if not passport_key_match:
            return "Error: Unable to retrieve passport key"
        self.base_url, self.passport_key = passport_key_match.group(0).split("?passportKey=")

    def spell_check(self, text):
        if self.passport_key is None or self.base_url is None:
            self.fetch_passport_key()
        payload = {
            'passportKey': self.passport_key,
            'where': 'nexearch',
            'color_blindness': 0,
            'q': text
        }
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, Gecko) Chrome/129.0.0.0 Safari/537.36',
            'Referer': 'https://search.naver.com/',
        }
        result_response = requests.get(self.base_url, headers=headers, params=payload)
        return json.loads(result_response.text)['message']['result']['notag_html']

# Usage
checker = SpellChecker()
result = checker.spell_check("sentence for spell check")
print(result)
@yeong-hwan
Copy link

It still works til 25.01.12 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants