Created
March 2, 2018 23:41
-
-
Save nobitagit/9023ef6bfb4ee7ba3971a863ef2f4884 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
maxScore = 7 | |
scores = { | |
'q': 5, | |
'w': 4, | |
'e': 3, | |
'r': 2, | |
't': 3, | |
'y': 3, | |
'u': 2, | |
'i': 3, | |
'o': 4, | |
'p': 5, | |
'a': 4, | |
's': 3, | |
'd': 2, | |
'f': 1, | |
'g': 2, | |
'h': 2, | |
'j': 1, | |
'k': 2, | |
'l': 3, | |
';': 4, | |
'z': 5, | |
'x': 4, | |
'c': 3, | |
'v': 2, | |
'b': 3, | |
'n': 3, | |
'm': 2, | |
',': 3, | |
'.': 4, | |
'/': 5, | |
'1': 6, | |
'2': 5, | |
'3': 4, | |
'4': 3, | |
'5': 4, | |
'6': 4, | |
'7': 3, | |
'8': 4, | |
'9': 5, | |
'0': 6, | |
' ': 1 | |
} | |
def to_score(ch): | |
if ch in scores: | |
if ch.isupper(): | |
return scores[ch] + 2 | |
else: | |
return scores[ch] | |
return maxScore | |
# for a given sentence calcultate the characters score | |
def score_sentence(sentence): | |
scored = list(map(to_score, sentence)) | |
aggregate = reduce(lambda x, y: x + y, scored, 0) | |
return aggregate | |
def process_text(coll): | |
sentences = coll.split(". ") | |
ret = [] | |
for sentence in sentences: | |
ret.append({ | |
'text': sentence, | |
'score': score_sentence(sentence) | |
}) | |
return ret | |
# As an example we load text from a file | |
text_coll = open("testfile.txt", "r").read() | |
print(process_text(text_coll)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment