Skip to content

Commit

Permalink
add two new verbosity options and show detected Language
Browse files Browse the repository at this point in the history
release 0.8.0
  • Loading branch information
Findus23 committed Jul 19, 2019
1 parent b1265f0 commit b94494b
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 69 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
Changelog
=========

`0.7.2` (unreleased)
`0.8.0` (2019-07-19)
--------------------
* don't use goo.gl in the help text
* add new option `-r`/`--rules` to show details for matching rules
* add new option `--rule-categories` to show categories of matching rules
* print autodetected language


`0.7.1` (2019-06-14)
Expand Down
45 changes: 37 additions & 8 deletions pylanguagetool/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ def init_config():
choices=["txt", "html", "md", "rst", "ipynb"],
help="if not plaintext")
p.add_argument('input file', help='input file', nargs='?')

p.add_argument("-r", "--rules", env_var="RULES", action='store_true', default=False,
help="show the matching rules")
p.add_argument("--rule-categories", env_var="RULE_CATEGORIES", action='store_true', default=False,
help="show the the categories of the matching rules")
p.add_argument('-l', '--lang', env_var='TEXTLANG', default="auto",
help="A language code like en or en-US, or auto to guess the language automatically (see preferredVariants below). For languages with variants (English, German, Portuguese) spell checking will only be activated when you specify the variant, e.g. en-GB instead of just en."
)
Expand Down Expand Up @@ -122,14 +125,28 @@ def get_input_text(config):
sys.exit(2)


def print_errors(matches, api_url, version, print_color=True):
def print_errors(response, api_url, print_color=True, rules=False, rule_categories=False):
matches = response["matches"]
language = response["language"]
version = response["software"]["name"] + " " + response["software"]["version"]

def colored(text, color):
if print_color:
init_colors()
return color + text + Fore.RESET
else:
return text

print(colored(
"{} detected ({:.0f}% confidence)".format(language["detectedLanguage"]["name"],
language["detectedLanguage"]["confidence"] * 100)
, Fore.LIGHTBLACK_EX))
if language["detectedLanguage"]["code"] != language["code"]:
print(colored(
"checking as {} text because of setting".format(language["name"])
, Fore.LIGHTBLACK_EX))
print()

tick = colored(u"\u2713", Fore.LIGHTGREEN_EX) + " "
cross = colored(u"\u2717", Fore.LIGHTRED_EX) + " "

Expand Down Expand Up @@ -165,6 +182,16 @@ def colored(text, color):
colored(replacement["value"], Fore.LIGHTGREEN_EX) +
colored(context[endpostion:], Fore.LIGHTBLACK_EX)
)
rule = error["rule"]
if rules:
print(
indention[:2] + colored(rule["id"] + ": ", Fore.LIGHTBLACK_EX) + rule["description"]
)
if rule_categories:
category = rule["category"]
print(
indention[:2] + colored(category["id"] + ": ", Fore.LIGHTBLACK_EX) + category["name"]
)
print()
print(colored("Text checked by {url} ({version})".format(url=api_url, version=version), Fore.LIGHTBLACK_EX))

Expand Down Expand Up @@ -195,10 +222,11 @@ def main():
found = False
for line in check_text.splitlines():
response = api.check(line, **config)
print_errors(response["matches"],
print_errors(response,
config["api_url"],
response["software"]["name"] + " " + response["software"]["version"],
not config["no_color"]
not config["no_color"],
config["rules"],
config["rule_categories"]
)
if len(response["matches"]) > 0:
found = True
Expand All @@ -207,10 +235,11 @@ def main():

else:
response = api.check(check_text, **config)
print_errors(response["matches"],
print_errors(response,
config["api_url"],
response["software"]["name"] + " " + response["software"]["version"],
not config["no_color"]
not config["no_color"],
config["rules"],
config["rule_categories"]
)

if len(response["matches"]) > 0:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

setup(
name='pyLanguagetool',
version='0.7.1',
version='0.8.0',
packages=find_packages(),
url='https://pylanguagetool.lw1.at/',
license='MIT',
Expand Down
137 changes: 78 additions & 59 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,85 +4,104 @@

def test_cli(capsys):
response = {
'software': {
'buildDate': '2016-12-28 15:25',
'apiVersion': '1',
'version': '3.6',
'status': '',
'name': 'LanguageTool'
"software": {
"name": "LanguageTool",
"version": "4.6",
"buildDate": "2019-06-26 07:28",
"apiVersion": 1,
"premium": False,
"premiumHint": "You might be missing errors only the Premium version can find. Contact us at support<at>languagetoolplus.com.",
"status": "",
},
'matches': [
"warnings": {"incompleteResults": False},
"language": {
"name": "German (Austria)",
"code": "de-AT",
"detectedLanguage": {
"name": "German (Austria)",
"code": "de-AT",
"confidence": 0.99999136,
},
},
"matches": [
{
'length': 3,
'shortMessage': '',
'context': {
'length': 3,
'text': 'das ist ein est',
'offset': 0
"message": "Dieser Satz fängt nicht mit einem großgeschriebenen Wort an",
"shortMessage": "",
"replacements": [{"value": "Das"}],
"offset": 0,
"length": 3,
"context": {"text": "das ist ein est", "offset": 0, "length": 3},
"sentence": "das ist ein est",
"type": {"typeName": "Other"},
"rule": {
"id": "UPPERCASE_SENTENCE_START",
"description": "Großschreibung am Satzanfang",
"issueType": "typographical",
"category": {"id": "CASING", "name": "Groß-/Kleinschreibung"},
},
'replacements':
[{'value': 'Das'}],
'rule': {
'category': {
'name': 'Groß-/Kleinschreibung',
'id': 'CASING'
},
'issueType': 'typographical',
'description': 'Großschreibung am Satzanfang',
'id': 'UPPERCASE_SENTENCE_START'
"ignoreForIncompleteSentence": True,
"contextForSureMatch": -1,
},
{
"message": "Möglicher Rechtschreibfehler gefunden",
"shortMessage": "Rechtschreibfehler",
"replacements": [
{"value": "ist"},
{"value": "es"},
{"value": "erst"},
{"value": "fest"},
{"value": "West"},
{"value": "Ost"},
{"value": "Rest"},
{"value": "Nest"},
{"value": "Pest"},
{"value": "lest"},
{"value": "Ast"},
{"value": "Fest"},
{"value": "Gst"},
{"value": "Test"},
{"value": "best"},
{"value": "esst"},
{"value": "et"},
{"value": "äst"},
{"value": "des"},
{"value": "mit"},
],
"offset": 12,
"length": 3,
"context": {"text": "das ist ein est", "offset": 12, "length": 3},
"sentence": "das ist ein est",
"type": {"typeName": "UnknownWord"},
"rule": {
"id": "AUSTRIAN_GERMAN_SPELLER_RULE",
"description": "Möglicher Rechtschreibfehler",
"issueType": "misspelling",
"category": {"id": "TYPOS", "name": "Mögliche Tippfehler"},
},
'message': 'Dieser Satz fängt nicht mit einem großgeschriebenen Wort an',
'offset': 0
"ignoreForIncompleteSentence": False,
"contextForSureMatch": 0,
},
{'length': 3,
'shortMessage': 'Rechtschreibfehler',
'context':
{'length': 3,
'text': 'das ist ein est',
'offset': 12
},
'replacements':
[
{'value': 'esst'}, {'value': 'ist'}, {'value': 'es'}, {'value': 'erst'}, {'value': 'fest'},
{'value': 'Rest'}, {'value': 'Test'}, {'value': 'Fest'}, {'value': 'West'}, {'value': 'Nest'},
{'value': 'Ost'}, {'value': 'Ast'}, {'value': 'Gst'}, {'value': 'Pest'}, {'value': 'et'},
{'value': 'lest'}, {'value': 'äst'}, {'value': 'des'}, {'value': 'mit'}, {'value': 'ein'}
],
'rule': {
'category': {
'name': 'Mögliche Tippfehler',
'id': 'TYPOS'
},
'issueType': 'misspelling',
'description': 'Möglicher Rechtschreibfehler',
'id': 'GERMAN_SPELLER_RULE'
},
'message': 'Möglicher Rechtschreibfehler gefunden',
'offset': 12
}
],
'language': {
'code': 'de-AT',
'name': 'German (Austria)'
}
}

should_output = u"""Dieser Satz fängt nicht mit einem großgeschriebenen Wort an
should_output = u"""German (Austria) detected (100% confidence)
Dieser Satz fängt nicht mit einem großgeschriebenen Wort an
✗ das ist ein est
^^^
✓ Das ist ein est
Möglicher Rechtschreibfehler gefunden
✗ das ist ein est
^^^
✓ das ist ein esst
✓ das ist ein ist
✓ das ist ein es
✓ das ist ein erst
✓ das ist ein fest
✓ das ist ein West
Text checked by https://example.com/v2/ (Test 1.0)
Text checked by https://example.com/v2/ (LanguageTool 4.6)
"""
pylanguagetool.print_errors(response["matches"], "https://example.com/v2/", "Test 1.0", False)
pylanguagetool.print_errors(response, "https://example.com/v2/", False)
out, err = capsys.readouterr()
assert out == should_output

0 comments on commit b94494b

Please sign in to comment.