From 2cb4b0353249c076c637942700220c84a5f7fd52 Mon Sep 17 00:00:00 2001 From: Abigail Gold <5366828+classabbyamp@users.noreply.github.com> Date: Mon, 30 Mar 2020 22:56:29 +0000 Subject: [PATCH] add phonetic weight command (#215) Fixes #170 Co-authored-by: 0x5c --- CHANGELOG.md | 1 + exts/ham.py | 20 ++++++++++++++++++++ resources/phonetics.py | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c44dce7..b339ac4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ## [Unreleased] ### Added +- `?phoneticweight` command, which calculates a message's length in syllables. - `?standards` command to display [xkcd 927](https://xkcd.com/927/). ### Changed - Python>=3.7 now required. diff --git a/exts/ham.py b/exts/ham.py index d6e0bd5..4b40583 100644 --- a/exts/ham.py +++ b/exts/ham.py @@ -92,6 +92,26 @@ class HamCog(commands.Cog): embed.colour = cmn.colours.good await ctx.send(embed=embed) + @commands.command(name="phoneticweight", aliases=["pw"], category=cmn.cat.ref) + async def _weight(self, ctx: commands.Context, *, msg: str): + """Calculates the phonetic weight of a callsign or message.""" + embed = cmn.embed_factory(ctx) + msg = msg.upper() + weight = 0 + for char in msg: + try: + weight += phonetics.pweights[char] + except KeyError: + embed.title = "Error in calculation of phonetic weight" + embed.description = f"Unknown character `{char}` in message" + embed.colour = cmn.colours.bad + await ctx.send(embed=embed) + return + embed.title = f"Phonetic Weight of {msg}" + embed.description = f"The phonetic weight is **{weight}**" + embed.colour = cmn.colours.good + await ctx.send(embed=embed) + def setup(bot: commands.Bot): bot.add_cog(HamCog(bot)) diff --git a/resources/phonetics.py b/resources/phonetics.py index 2dc637f..b2f9194 100644 --- a/resources/phonetics.py +++ b/resources/phonetics.py @@ -36,3 +36,43 @@ phonetics = { "y": "yankee", "z": "zulu" } + +pweights = { + "A": 2, + "B": 2, + "C": 2, + "D": 2, + "E": 2, + "F": 2, + "G": 1, + "H": 2, + "I": 3, + "J": 3, + "K": 2, + "L": 2, + "M": 1, + "N": 3, + "O": 2, + "P": 2, + "Q": 2, + "R": 3, + "S": 3, + "T": 2, + "U": 3, + "V": 2, + "W": 2, + "X": 2, + "Y": 2, + "Z": 2, + "0": 2, + "1": 1, + "2": 1, + "3": 1, + "4": 1, + "5": 1, + "6": 1, + "7": 2, + "8": 1, + "9": 2, + "/": 1, +}