Skip to content

Instantly share code, notes, and snippets.

@nobitagit
Created March 2, 2018 23:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nobitagit/9023ef6bfb4ee7ba3971a863ef2f4884 to your computer and use it in GitHub Desktop.
Save nobitagit/9023ef6bfb4ee7ba3971a863ef2f4884 to your computer and use it in GitHub Desktop.
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