Lesson 2 of 6 · 2 min
Signals you can explain
import re
STOCK = ["delve", "tapestry", "in today's fast-paced world", "it's important to note", "unlock the power", "game-changer", "seamlessly", "in conclusion"]
def signals(text):
sentences = [s.strip() for s in re.split(r"(?<=[.!?])\s+", text) if s.strip()]
words = re.findall(r"[a-z']+", text.lower())
lengths = [len(s.split()) for s in sentences]
mean = sum(lengths) / max(len(lengths), 1)
spread = (sum((n - mean) ** 2 for n in lengths) / max(len(lengths), 1)) ** 0.5
trigrams = [tuple(words[i:i + 3]) for i in range(len(words) - 2)]
return {
"stock phrases": [p for p in STOCK if p in text.lower()],
"repeated 3-word runs": len(trigrams) - len(set(trigrams)),
"variety of words": round(len(set(words)) / max(len(words), 1), 2),
"variation in sentence length": round(spread / max(mean, 1), 2),
}
sample = "In today's fast-paced world, it's important to note that we delve into data. We delve into data seamlessly."
for name, value in signals(sample).items():
print(f"{name:30} {value}")
