2017-03-03 12:42:07 +01:00
|
|
|
|
|
|
|
from typing import List, Tuple, Set, Pattern, Match
|
|
|
|
import re
|
|
|
|
|
|
|
|
from bs4 import BeautifulSoup
|
|
|
|
|
2017-03-10 11:47:06 +01:00
|
|
|
# The phrases in this list will be ignored. The longest phrase is
|
|
|
|
# tried first; this removes the chance of smaller phrases changing
|
|
|
|
# the text before longer phrases are tried.
|
|
|
|
# The errors shown by `tools/check-capitalization` can be added to
|
|
|
|
# this list without any modification.
|
|
|
|
IGNORED_PHRASES = [
|
2017-03-03 12:42:07 +01:00
|
|
|
# Proper nouns and acronyms
|
2017-05-26 07:38:24 +02:00
|
|
|
r"Android",
|
2017-03-03 12:42:07 +01:00
|
|
|
r"API",
|
2017-07-07 19:43:02 +02:00
|
|
|
r"APNS",
|
2017-07-01 00:41:50 +02:00
|
|
|
r"App Store",
|
2017-07-08 02:02:28 +02:00
|
|
|
r"Botserver",
|
2017-03-03 12:42:07 +01:00
|
|
|
r"Cookie Bot",
|
|
|
|
r"Dropbox",
|
|
|
|
r"GitHub",
|
2018-04-23 19:21:10 +02:00
|
|
|
r"G Suite",
|
2017-03-03 12:42:07 +01:00
|
|
|
r"Google",
|
2018-03-05 19:29:18 +01:00
|
|
|
r"Gravatar",
|
2017-11-17 04:31:37 +01:00
|
|
|
r"Hamlet",
|
2018-12-17 21:44:44 +01:00
|
|
|
r"Help Center",
|
2017-03-03 12:42:07 +01:00
|
|
|
r"HTTP",
|
|
|
|
r"ID",
|
|
|
|
r"IDs",
|
2018-04-30 21:04:01 +02:00
|
|
|
r"IP",
|
2017-03-03 12:42:07 +01:00
|
|
|
r"JIRA",
|
|
|
|
r"JSON",
|
|
|
|
r"Kerberos",
|
2017-09-25 07:45:17 +02:00
|
|
|
r"LDAP",
|
2017-03-03 12:42:07 +01:00
|
|
|
r"Mac",
|
2017-08-26 09:33:47 +02:00
|
|
|
r"macOS",
|
2017-03-03 12:42:07 +01:00
|
|
|
r"MiB",
|
2017-03-19 20:01:01 +01:00
|
|
|
r"OTP",
|
2017-03-03 12:42:07 +01:00
|
|
|
r"Pivotal",
|
2017-07-01 00:41:50 +02:00
|
|
|
r"Play Store",
|
2017-03-03 12:42:07 +01:00
|
|
|
r'REMOTE_USER',
|
2017-01-30 21:18:41 +01:00
|
|
|
r'Slack',
|
2017-03-03 12:42:07 +01:00
|
|
|
r"SSO",
|
|
|
|
r'Terms of Service',
|
2018-05-09 20:08:44 +02:00
|
|
|
r'Tuesday',
|
2017-03-03 12:42:07 +01:00
|
|
|
r"URL",
|
|
|
|
r"Ubuntu",
|
2017-07-21 01:55:10 +02:00
|
|
|
r"Updown",
|
2017-03-03 12:42:07 +01:00
|
|
|
r"V5",
|
|
|
|
r"Webathena",
|
|
|
|
r"Windows",
|
|
|
|
r"WordPress",
|
|
|
|
r"XML",
|
|
|
|
r"Zephyr",
|
2018-12-28 20:45:54 +01:00
|
|
|
r"Zoom",
|
2017-03-03 12:42:07 +01:00
|
|
|
r"Zulip",
|
2018-12-18 08:59:56 +01:00
|
|
|
r"Zulip Account Security",
|
2018-12-20 08:57:49 +01:00
|
|
|
r"Zulip Security",
|
|
|
|
r"Zulip Team",
|
2017-03-03 12:42:07 +01:00
|
|
|
r"iPhone",
|
2017-05-27 05:59:32 +02:00
|
|
|
r"iOS",
|
2017-04-25 08:15:39 +02:00
|
|
|
r"Emoji One",
|
2018-03-14 15:46:20 +01:00
|
|
|
r"mailinator.com",
|
2018-12-18 11:08:51 +01:00
|
|
|
r"HQ",
|
2017-03-03 12:42:07 +01:00
|
|
|
# Code things
|
|
|
|
r".zuliprc",
|
|
|
|
r"__\w+\.\w+__",
|
|
|
|
# Things using "I"
|
|
|
|
r"I say",
|
|
|
|
r"I want",
|
|
|
|
r"I'm",
|
|
|
|
# Specific short words
|
|
|
|
r"and",
|
|
|
|
r"bot",
|
|
|
|
r"e.g.",
|
|
|
|
r"etc.",
|
|
|
|
r"images",
|
2017-11-16 02:38:56 +01:00
|
|
|
r"enabled",
|
|
|
|
r"disabled",
|
2018-05-04 07:37:24 +02:00
|
|
|
r"zulip_org_id",
|
2018-05-26 12:15:47 +02:00
|
|
|
r"admins",
|
|
|
|
r"members",
|
2017-12-05 01:11:14 +01:00
|
|
|
# Placeholders
|
|
|
|
r"keyword",
|
|
|
|
r"streamname",
|
|
|
|
r"user@example.com",
|
2017-03-03 12:42:07 +01:00
|
|
|
# Fragments of larger strings
|
2018-02-05 09:21:58 +01:00
|
|
|
(r'your subscriptions on your Streams page'),
|
2017-03-10 11:47:06 +01:00
|
|
|
(r'Change notification settings for individual streams on your '
|
|
|
|
'<a href="/#streams">Streams page</a>.'),
|
2017-03-27 23:25:43 +02:00
|
|
|
(r'Looking for our '
|
2017-03-10 11:47:06 +01:00
|
|
|
'<a href="/integrations" target="_blank">Integrations</a> or '
|
2017-08-28 23:03:24 +02:00
|
|
|
'<a href="/api" target="_blank">API</a> documentation?'),
|
2017-03-10 11:47:06 +01:00
|
|
|
r'Most stream administration is done on the <a href="/#streams">Streams page</a>.',
|
2017-03-03 12:42:07 +01:00
|
|
|
r"one or more people...",
|
|
|
|
r"confirmation email",
|
|
|
|
r"invites remaining",
|
2017-03-10 11:47:06 +01:00
|
|
|
r"was too large; the maximum file size is 25MiB.",
|
2017-03-20 15:52:46 +01:00
|
|
|
r"selected message",
|
2017-06-23 02:06:54 +02:00
|
|
|
r"a-z",
|
2018-08-21 08:14:46 +02:00
|
|
|
r"organization administrator",
|
|
|
|
r"user",
|
2018-12-18 08:59:56 +01:00
|
|
|
r"an unknown operating system",
|
2017-03-03 12:42:07 +01:00
|
|
|
|
|
|
|
# SPECIAL CASES
|
|
|
|
# Enter is usually capitalized
|
|
|
|
r"Press Enter to send",
|
|
|
|
# Because topics usually are lower-case, this would look weird if it were capitalized
|
|
|
|
r"more topics",
|
|
|
|
# For consistency with "more topics"
|
|
|
|
r"more conversations",
|
2018-02-07 01:13:11 +01:00
|
|
|
# Capital 'i' looks weird in reminders popover
|
|
|
|
r"in 1 hour",
|
|
|
|
r"in 20 minutes",
|
|
|
|
r"in 3 hours",
|
2017-03-03 12:42:07 +01:00
|
|
|
# We should probably just delete this string from translations
|
|
|
|
r'activation key',
|
2017-04-27 00:03:21 +02:00
|
|
|
# this is used as a topic
|
|
|
|
r'^hello$',
|
2018-03-02 20:54:39 +01:00
|
|
|
# These are used as example short names (e.g. an uncapitalized context):
|
|
|
|
r"^marketing$",
|
|
|
|
r"^cookie$",
|
|
|
|
r"^new_emoji$",
|
2018-05-02 19:02:51 +02:00
|
|
|
# Used to refer custom time limits
|
|
|
|
r"\bN\b",
|
2017-03-03 12:42:07 +01:00
|
|
|
|
2018-12-16 20:34:31 +01:00
|
|
|
r"group private messages with __recipient__",
|
|
|
|
r"private messages with __recipient__",
|
|
|
|
r"private messages with yourself",
|
|
|
|
|
2017-03-03 12:42:07 +01:00
|
|
|
# TO CLEAN UP
|
|
|
|
# Just want to avoid churning login.html right now
|
|
|
|
r"or Choose a user",
|
|
|
|
# This is a parsing bug in the tool
|
|
|
|
r"argument ",
|
|
|
|
# I can't find this one
|
|
|
|
r"text",
|
2018-08-04 11:03:37 +02:00
|
|
|
r"GIF",
|
|
|
|
# Emoji name placeholder
|
|
|
|
r"leafy green vegetable",
|
2018-08-25 14:06:17 +02:00
|
|
|
# Subdomain placeholder
|
|
|
|
r"your-organization-url",
|
2017-03-10 11:47:06 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
# Sort regexes in descending order of their lengths. As a result, the
|
|
|
|
# longer phrases will be ignored first.
|
|
|
|
IGNORED_PHRASES.sort(key=lambda regex: len(regex), reverse=True)
|
|
|
|
|
|
|
|
# Compile regexes to improve performance. This also extracts the
|
|
|
|
# text using BeautifulSoup and then removes extra whitespaces from
|
|
|
|
# it. This step enables us to add HTML in our regexes directly.
|
|
|
|
COMPILED_IGNORED_PHRASES = [
|
|
|
|
re.compile(' '.join(BeautifulSoup(regex, 'lxml').text.split()))
|
|
|
|
for regex in IGNORED_PHRASES
|
|
|
|
]
|
2017-03-03 12:42:07 +01:00
|
|
|
|
|
|
|
SPLIT_BOUNDARY = '?.!' # Used to split string into sentences.
|
|
|
|
SPLIT_BOUNDARY_REGEX = re.compile(r'[{}]'.format(SPLIT_BOUNDARY))
|
|
|
|
|
|
|
|
# Regexes which check capitalization in sentences.
|
|
|
|
DISALLOWED_REGEXES = [re.compile(regex) for regex in [
|
|
|
|
r'^[a-z]', # Checks if the sentence starts with a lower case character.
|
|
|
|
r'^[A-Z][a-z]+[\sa-z0-9]+[A-Z]', # Checks if an upper case character exists
|
|
|
|
# after a lower case character when the first character is in upper case.
|
|
|
|
]]
|
|
|
|
|
2018-03-11 06:06:54 +01:00
|
|
|
BANNED_WORDS = {
|
|
|
|
'realm': ('The term realm should not appear in user-facing strings. '
|
|
|
|
'Use organization instead.'),
|
|
|
|
}
|
|
|
|
|
2017-03-03 12:42:07 +01:00
|
|
|
def get_safe_phrase(phrase):
|
|
|
|
# type: (str) -> str
|
|
|
|
"""
|
|
|
|
Safe phrase is in lower case and doesn't contain characters which can
|
|
|
|
conflict with split boundaries. All conflicting characters are replaced
|
|
|
|
with low dash (_).
|
|
|
|
"""
|
|
|
|
phrase = SPLIT_BOUNDARY_REGEX.sub('_', phrase)
|
|
|
|
return phrase.lower()
|
|
|
|
|
|
|
|
def replace_with_safe_phrase(matchobj):
|
|
|
|
# type: (Match[str]) -> str
|
|
|
|
"""
|
|
|
|
The idea is to convert IGNORED_PHRASES into safe phrases, see
|
|
|
|
`get_safe_phrase()` function. The only exception is when the
|
|
|
|
IGNORED_PHRASE is at the start of the text or after a split
|
|
|
|
boundary; in this case, we change the first letter of the phrase
|
|
|
|
to upper case.
|
|
|
|
"""
|
|
|
|
ignored_phrase = matchobj.group(0)
|
|
|
|
safe_string = get_safe_phrase(ignored_phrase)
|
|
|
|
|
|
|
|
start_index = matchobj.start()
|
|
|
|
complete_string = matchobj.string
|
|
|
|
|
|
|
|
is_string_start = start_index == 0
|
|
|
|
# We expect that there will be one space between split boundary
|
|
|
|
# and the next word.
|
|
|
|
punctuation = complete_string[max(start_index - 2, 0)]
|
|
|
|
is_after_split_boundary = punctuation in SPLIT_BOUNDARY
|
|
|
|
if is_string_start or is_after_split_boundary:
|
|
|
|
return safe_string.capitalize()
|
|
|
|
|
|
|
|
return safe_string
|
|
|
|
|
|
|
|
def get_safe_text(text):
|
|
|
|
# type: (str) -> str
|
|
|
|
"""
|
|
|
|
This returns text which is rendered by BeautifulSoup and is in the
|
|
|
|
form that can be split easily and has all IGNORED_PHRASES processed.
|
|
|
|
"""
|
|
|
|
soup = BeautifulSoup(text, 'lxml')
|
|
|
|
text = ' '.join(soup.text.split()) # Remove extra whitespaces.
|
2017-03-10 11:47:06 +01:00
|
|
|
for phrase_regex in COMPILED_IGNORED_PHRASES:
|
2017-03-03 12:42:07 +01:00
|
|
|
text = phrase_regex.sub(replace_with_safe_phrase, text)
|
|
|
|
|
|
|
|
return text
|
|
|
|
|
|
|
|
def is_capitalized(safe_text):
|
|
|
|
# type: (str) -> bool
|
|
|
|
sentences = SPLIT_BOUNDARY_REGEX.split(safe_text)
|
|
|
|
sentences = [sentence.strip()
|
|
|
|
for sentence in sentences if sentence.strip()]
|
|
|
|
|
|
|
|
if not sentences:
|
|
|
|
return False
|
|
|
|
|
|
|
|
for sentence in sentences:
|
|
|
|
for regex in DISALLOWED_REGEXES:
|
|
|
|
if regex.search(sentence):
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2018-03-11 06:06:54 +01:00
|
|
|
def check_banned_words(text: str) -> List[str]:
|
|
|
|
lower_cased_text = text.lower()
|
|
|
|
errors = []
|
|
|
|
for word, reason in BANNED_WORDS.items():
|
|
|
|
if word in lower_cased_text:
|
2018-03-17 00:41:21 +01:00
|
|
|
# Hack: Should move this into BANNED_WORDS framework; for
|
|
|
|
# now, just hand-code the skips:
|
|
|
|
if 'realm_name' in lower_cased_text:
|
|
|
|
continue
|
2018-03-11 06:06:54 +01:00
|
|
|
kwargs = dict(word=word, text=text, reason=reason)
|
|
|
|
msg = "{word} found in '{text}'. {reason}".format(**kwargs)
|
|
|
|
errors.append(msg)
|
|
|
|
|
|
|
|
return errors
|
|
|
|
|
2017-03-03 12:42:07 +01:00
|
|
|
def check_capitalization(strings):
|
2018-03-11 06:06:54 +01:00
|
|
|
# type: (List[str]) -> Tuple[List[str], List[str], List[str]]
|
2017-03-03 12:42:07 +01:00
|
|
|
errors = []
|
|
|
|
ignored = []
|
2018-03-11 06:06:54 +01:00
|
|
|
banned_word_errors = []
|
2017-03-03 12:42:07 +01:00
|
|
|
for text in strings:
|
2017-03-10 11:47:06 +01:00
|
|
|
text = ' '.join(text.split()) # Remove extra whitespaces.
|
2017-03-03 12:42:07 +01:00
|
|
|
safe_text = get_safe_text(text)
|
|
|
|
has_ignored_phrase = text != safe_text
|
|
|
|
capitalized = is_capitalized(safe_text)
|
|
|
|
if not capitalized:
|
|
|
|
errors.append(text)
|
|
|
|
elif capitalized and has_ignored_phrase:
|
|
|
|
ignored.append(text)
|
|
|
|
|
2018-03-11 06:06:54 +01:00
|
|
|
banned_word_errors.extend(check_banned_words(text))
|
|
|
|
|
|
|
|
return sorted(errors), sorted(ignored), sorted(banned_word_errors)
|