任务详情
给定一段英文句子,判断句中单词拼写是否满足以下规则。
除以下特殊情况外,句子中第一个单词首字母必须大写,其它所有单词小写:
任务要求
输入英文句子为 str 类型,输出为 bool 类型;
如果句子满足规则要求,程序返回结果为 True;否则返回 False。
测试用例
输入:'I love Python' 输出:True
输入:'python love me' 输出:False
输入: 'JUST DO IT' 输出:True
输入: 'I come from HK' 输出:True
输入: 'Machinelearning is so hot' 输出:False
输入: 'I love Python' 输出:True
输入: 'I love python' 输出:False
输入: 'The upper pattern has not been test yet, so USA' 输出:True
def check_sentence(sentence):
# 特殊情况1:全大写单词正确
for word in sentence.upper().split():
if word.isupper():
continue
# 特殊情况2:四个特定单词正确性判断
for key in ['Python', 'Java', 'MachineLearning', 'DataMining']:
if key in sentence:
if key != sentence.split()[sentence.split().index(key)]:
return False
# 特殊情况3:数字+字母单词全大写
for word in sentence.split():
if word.isalnum() and word[0].isdigit():
if word != word.upper():
return False
# 一般情况:第一个单词首字母大写,其余小写
first_word = True
for word in sentence.split():
if first_word:
if word[0].islower():
return False
first_word = False
else:
if word[0].isupper():
return False
return True
sentence1 = 'I love Python'
sentence2 = 'python love me'
sentence3 = 'JUST DO IT'
sentence4 = 'I come from HK'
sentence5 = 'Machinelearning is so hot'
sentence6 = 'I love Python'
sentence7 = 'I love python'
sentence8 = 'The upper pattern has not been test yet, so USA'
print(check_sentence(sentence1))
print(check_sentence(sentence2))
print(check_sentence(sentence3))
print(check_sentence(sentence4))
print(check_sentence(sentence5))
print(check_sentence(sentence6))
print(check_sentence(sentence7))
print(check_sentence(sentence8))
代码:
def check_spelling(sentence):
words = sentence.split()
if len(words) == 0: # 句子为空,返回 False
return False
if words[0].islower(): # 第一个单词首字母为小写,返回 False
return False
for word in words:
if word.isupper() and len(word) > 1: # 全部为大写字母且不是单个字母,拼写正确
continue
elif word in ['Python', 'Java', 'MachineLearning', 'DataMining']: # 四个单词必须为双引号中给出的形式
continue
elif len(word) > 1 and word[0].isdigit() and word[1:].islower(): # 数字+字母混合形式,字母全部大写
if word[1:].isupper():
continue
else:
return False
elif not word.islower(): # 其他情况,单词不是全部小写,返回 False
return False
return True
测试:
print(check_spelling('I love Python')) # True
print(check_spelling('python love me')) # False
print(check_spelling('JUST DO IT')) # True
print(check_spelling('I come from HK')) #True
print(check_spelling('Machinelearning is so hot')) # False
print(check_spelling('I love Python')) # True
print(check_spelling('I love python')) # False
print(check_spelling('The upper pattern has not been test yet, so USA')) # True
下面是Python实现该任务的代码。代码思路是先对句子进行分词,然后按照题目要求判断每个单词的拼写是否正确。
import nltk
nltk.download("punkt")
def is_spelling_correct(sentence):
words = nltk.word_tokenize(sentence) # 对句子进行分词
for i, word in enumerate(words):
if word == "Python" or word == "Java" or word == "MachineLearning" or word == "DataMining": # 四个单词必须为双引号中给出的形式
if i == 0 or words[i-1][-1] != '"' or (i < len(words) - 1 and words[i+1][0] != '"'):
return False
elif word.isupper() and len(word) > 1: # 如果单词全部为大写,则拼写正确
continue
elif word.isupper() and len(word) == 1: # 如果单词只有一个字母,则拼写错误
return False
else:
if i == 0 and not word[0].isupper(): # 第一个单词首字母必须大写
return False
if i > 0 and words[i-1][-1] == '"': # 如果前一个单词以"结束,则当前单词拼写正确
continue
if word.isalpha() and not word.islower(): # 如果单词首字母不是大写且单词全部由字母构成,则拼写错误
return False
if word.isalnum() and word[0].isdigit() and word[1:].isupper(): # 数字和字母混合的形式,全部大写
continue
if word != "I" and not word.islower(): # 其他情况,单词必须小写
return False
return True
使用上述代码,可以像下面这样来测试:
print(is_spelling_correct("I love Python")) # True
print(is_spelling_correct("python love me")) # False
print(is_spelling_correct("JUST DO IT")) # True
print(is_spelling_correct("I come from HK")) # True
print(is_spelling_correct("Machinelearning is so hot")) # False
print(is_spelling_correct("I love Python")) # True
print(is_spelling_correct("I love python")) # False
print(is_spelling_correct("The upper pattern has not been test yet, so USA")) # True
输出结果如下所示:
True
False
True
True
False
True
False
True