ConDict

Check-in [70ae59edca]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:add spelling, add bug in utf-8
Timelines: family | ancestors | testing
Files: files | file ages | folders
SHA1:70ae59edca580bad60a48a5a5ee38f29502a21d7
User & Date: zorro 2012-12-05 19:11:20
Context
2012-12-05
19:11
add spelling, add bug in utf-8 Leaf check-in: 70ae59edca user: zorro tags: testing
2012-10-17
11:10
fixed bug in command .info check-in: e03a7f02c8 user: alzay tags: testing
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to aside.py.

     1      1   #!/usr/bin/env python3
     2      2   #-*- coding: utf-8 -*-
     3      3   
     4      4   import re, configparser, json, signal
     5      5   from urllib import request, parse
     6      6   
     7      7   YANDEX_TRANSLATE_JSON = "http://translate.yandex.net/api/v1/tr.json/translate?"
            8  +YANDEX_SPELL_JSON = "http://speller.yandex.net/services/spellservice.json/checkText?"
     8      9   TEST_CONNECT = "http://ya.ru/"
     9     10   CHECK_MANY_SPACE = re.compile(r"\s+")
    10     11   DEFCTEST = 10
    11     12   
    12     13   def get_config_data(filename):
    13     14       global DEFCTEST
    14     15       result = {'database': None, 'defuser': None, 'defctest': DEFCTEST, 'debug': False}
................................................................................
    58     59           try:
    59     60               from_url = conn.read().decode('utf-8')
    60     61               result = json.loads(from_url)
    61     62           except Exception as e:
    62     63               print(e)
    63     64       conn.close()
    64     65       return result
           66  +
           67  +def check_spell(for_spelling, spell_type='en'):
           68  +    global YANDEX_SPELL_JSON
           69  +    result = False
           70  +    # options = IGNORE_DIGITS(2) + IGNORE_DIGITS(4) + IGNORE_CAPITALIZATION(512) + [BY_WORDS(256)]
           71  +    params = {'lang': spell_type, 'text': for_spelling, 'format': 'plane', 'options': 518}
           72  +    prepate_url = parse.urlencode(params, encoding="utf-8")
           73  +    erro_codes = ("ERROR_UNKNOWN_WORD",
           74  +        "ERROR_REPEAT_WORD",
           75  +        "ERROR_CAPITALIZATION",
           76  +        "ERROR_TOO_MANY_ERRORS")
           77  +    try:
           78  +        conn = request.urlopen(YANDEX_SPELL_JSON + prepate_url, None, 1)
           79  +        if conn.status == 200:
           80  +            from_url = conn.read().decode('utf-8')
           81  +            result = json.loads(from_url)
           82  +    except Exception as e:
           83  +        print("Not connection\nError: ".format(e))
           84  +        return result
           85  +    else:
           86  +        conn.close()
           87  +    if len(result) > 0:
           88  +        print("Spelling: ", end="")
           89  +        for_result = []
           90  +        for res in result:
           91  +            for_result.append("{0} -> {1}".format(res['word'], res['s']))
           92  +        print("; ".join(for_result))
           93  +    return 0
    65     94   
    66     95   def get_test_connection():
    67     96       global TEST_CONNECT
    68     97       print("check connection...")
    69     98       try:
    70     99           conn = request.urlopen(TEST_CONNECT)
    71    100           result = True if conn.getcode() == 200 else False

Changes to condt.py.

   288    288           # found in offline DB
   289    289           alreadyEx = self.alreadyex(text, tr_type)
   290    290           if alreadyEx: 
   291    291               if DEBUG: print("[offline]")
   292    292               return alreadyEx[0]
   293    293           if not self.online:
   294    294               return "Offline, please test connect with '.connect' command"
          295  +        check_spell(text, tr_type)
   295    296           result = get_translate(text, tr_type)
   296    297           if not result or result['code'] != 200:
   297    298               self.command_connect()
   298    299               return "Error, not foud translate"
   299    300           return result['text']
   300    301   
   301    302       def command_list(self, pattern=None):

Changes to main.py.

     2      2   #-*- coding: utf-8 -*-
     3      3   
     4      4   from aside import *
     5      5   from condt import Condt
     6      6   import getpass, os
     7      7   
     8      8   CONF_NAME = 'condt.conf' 
     9         -PREFIX = "{0}@ConDict[{1}]$"
            9  +PREFIX = "{0}@condict[{1}]$"
    10     10   WELCOM = """****************************************************
    11     11   *** Good day.                                    ***
    12     12   *** For help, use the command ".help", nice work.***
    13     13   ****************************************************"""
    14     14   
    15     15   def main():
    16     16       global CONF_NAME, PREFIX