{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# CIS-024C Fall XXXXXXXXXXScrabble Project\n", "\n", "Source: MITEdx Introduction to Computation/Python\n", "\n", "The rules of the...

1 answer below »
Scrabble and Spell-Checker assignment


{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# CIS-024C Fall 2020 - Scrabble Project\n", "\n", "Source: MITEdx Introduction to Computation/Python\n", "\n", "The rules of the game are as follows:\n", "\n", "## Dealing\n", "A player is dealt a hand of n letters chosen at random (assume n=7 for now).\n", "\n", "The player arranges the hand into as many words as they want out of the letters, using each letter at most once.\n", "\n", "Some letters may remain unused (these won't be scored).\n", "\n", "## Scoring\n", "The score for the hand is the sum of the scores for each word formed.\n", "\n", "The score for a word is the sum of the points for letters in the word, multiplied by the length of the word, plus 50 points if all n letters are used on the first word created.\n", "\n", "Letters are scored as in Scrabble; A is worth 1, B is worth 3, C is worth 3, D is worth 2, E is worth 1, and so on. We have defined the dictionary SCRABBLE_LETTER_VALUES that maps each lowercase letter to its Scrabble letter value.\n", "\n", "For example, 'weed' would be worth 32 points ((4+1+1+2) for the four letters, then multiply by len('weed') to get (4+1+1+2)*4 = 32). Be sure to check that the hand actually has 1 'w', 2 'e's, and 1 'd' before scoring the word!\n", "\n", "As another example, if n=7 and you make the word 'waybill' on the first try, it would be worth 155 points (the base score for 'waybill' is (4+1+4+3+1+1+1)*7=105, plus an additional 50 point bonus for using all n letters)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Helper Code" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "import random\n", "import string\n", "\n", "VOWELS = 'aeiou'\n", "CONSONANTS = 'bcdfghjklmnpqrstvwxyz'\n", "HAND_SIZE = 7\n", "\n", "SCRABBLE_LETTER_VALUES = {\n", " 'a': 1, 'b': 3, 'c': 3, 'd': 2, 'e': 1, 'f': 4, 'g': 2, 'h': 4, 'i': 1, 'j': 8, 'k': 5, 'l': 1, 'm': 3, 'n': 1, 'o': 1, 'p': 3, 'q': 10, 'r': 1, 's': 1, 't': 1, 'u': 1, 'v': 4, 'w': 4, 'x': 8, 'y': 4, 'z': 10\n", "}\n", "\n", "# -----------------------------------\n", "# Helper code\n", "# (you don't need to understand this helper code)\n", "\n", "WORDLIST_FILENAME = \"words.txt\"" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "def loadWords():\n", " \"\"\"\n", " Returns a list of valid words. Words are strings of lowercase letters.\n", " \n", " Depending on the size of the word list, this function may\n", " take a while to finish.\n", " \"\"\"\n", " print(\"Loading word list from file...\")\n", " # inFile: file\n", " inFile = open(WORDLIST_FILENAME, 'r')\n", " # wordList: list of strings\n", " wordList = []\n", " for line in inFile:\n", " wordList.append(line.strip().lower())\n", " print(\" \", len(wordList), \"words loaded.\")\n", " return wordList" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "def getFrequencyDict(sequence):\n", " \"\"\"\n", " Returns a dictionary where the keys are elements of the sequence\n", " and the values are integer counts, for the number of times that\n", " an element is repeated in the sequence.\n", "\n", " sequence: string or list\n", " return: dictionary\n", " \"\"\"\n", " # freqs: dictionary (element_type -> int)\n", " freq = {}\n", " for x in sequence:\n", " freq[x] = freq.get(x,0) + 1\n", " return freq\n", "\t" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Scoring a word" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "# 10 points\n", "\n", "def getWordScore(word, n):\n", " \"\"\"\n", " Returns the score for a word. Assumes the word is a valid word.\n", "\n", " The score for a word is the sum of the points for letters in the\n", " word, multiplied by the length of the word, PLUS 50 points if all n\n", " letters are used on the first turn.\n", "\n", " Letters are scored as in Scrabble; A is worth 1, B is worth 3, C is\n", " worth 3, D is worth 2, E is worth 1, and so on (see SCRABBLE_LETTER_VALUES)\n", "\n", " word: string (lowercase letters)\n", " n: integer (HAND_SIZE; i.e., hand size required for additional points)\n", " returns: int >= 0\n", " \"\"\"\n", " # TO DO ... <-- remove="" this="" comment="" when="" you="" code="" this="" function\n"="" ]="" },="" {="" "cell_type":="" "markdown",="" "metadata":="" {},="" "source":="" [="" "###="" displaying="" a="" hand"="" ]="" },="" {="" "cell_type":="" "code",="" "execution_count":="" 16,="" "metadata":="" {},="" "outputs":="" [],="" "source":="" [="" "def="" displayhand(hand):\n",="" "="" \"\"\"\n",="" "="" displays="" the="" letters="" currently="" in="" the="" hand.\n",="" "\n",="" "="" for="" example:\n",="" "="">>> displayHand({'a':1, 'x':2, 'l':3, 'e':1})\n", " Should print out something like:\n", " a x x l l l e\n", " The order of the letters is unimportant.\n", "\n", " hand: dictionary (string -> int)\n", " \"\"\"\n", " for letter in hand.keys():\n", " for j in range(hand[letter]):\n", " print(letter,end='') # print all on the same line\n", " print() # print an empty line\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Deal hand" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "def dealHand(n):\n", " \"\"\"\n", " Returns a random hand containing n lowercase letters.\n", " At least n/3 the letters in the hand should
Answered Same DayDec 07, 2021

Answer To: { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# CIS-024C Fall...

Sudipta answered on Dec 17 2021
152 Votes
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"# Spell-Checker - Fall 2020 CIS-24C Spell Checker"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"## Project Description (100 points)\n",
"\n",
"This project involves creating a spell-checker problem that accepts a word from the user, look up that word in an available corpus and perform spell-correction on the word if the word is not present in the word corpus.\n",
"\n",
"the word corpus has been loaded and is available in a string named word corpus.\n",
"\n",
"You will need to do the following\n",
"\n",
"* Download both this program file and the associated google-10000-english.txt file to your computer.\n",
"\n",
"* Write a program using the WHILE loop that continuously asks the user to enter a word. If the user enters QUIT, then quit from the while loop and terminate the program. (20 points)\n",
"\n",
"* Once the user has entered the word, you will\n",
"** Compare the word with the word corpus, if there is a match, then you will let the user know that the word is valid. Note that the comparison must be case insensitive. (20 points)\n",
"\n",
"** If there is no match, then you will need to look up the corpus for the word that best matches the word that the user entered and display that word to the user. (40 points)\n",
"\n",
"*Extra credit* (20 points)\n",
"\n",
"* Allow the user to enter a paragraph and perform an automated spell correction of the paragraph. \n",
"For example, if the user enters \"Jack and Jill wen up the hills\", your program would return something like \"Jack and Jill went up the hill\"\n",
"\n",
"Other Points\n",
"\n",
"* 10 points will be awarded for the overall quality of the user interaction. \n",
"* 10 points will be awarded for the proper use of Python including making sure that he code is optimal.\n",
"\n",
"#### Hints\n",
"\n",
"Typically, this is implemented by looking at each word in the list and determining the number of adds, updates, deletes that are needed in order to get from the candidate word to the input word. Each operation has a score associated with it, for example\n",
"\n",
"Update - 2 point\n",
"Add - 1 point\n",
"Delete - 2 point\n",
"\n",
"For example, \n",
"\n",
"##### input word: wen\n",
"\n",
"##### candidate word: win\n",
"* To get from wen to win requires 1 update\n",
"* Total score for win is 2 points\n",
"\n",
"##### candidate word: went\n",
"* To get from wen to win requires 1 add\n",
"* Total score for win...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here