Knowee
Questions
Features
Study Tools

From the assignment brief, along with the spelling of the word, a word in your lexicon needs to store the following information:The frequency: How many times the word appears in the input files.The list of neighbours: A neighbour of a word w is a word that is of the same length and differs from w by only one letter.The best way this can be implemented is as a Word class, where you can populate your lexicon with Word objects.Your task in this section is to create a class Word that is used to represent a Word in your lexicon.When you do this, you will need to:Think about what instance variables should be defined (and how they should be initialized)Think about what methods you need to implement for this classIn a future task, you will need to sort your lexicon full of Word objects. In the labs you saw a similar example where you needed to sort a collection of Person objects. It may be useful to refer back to this to see what methods were required.You may find that once you attempt the following tasks, you need to come back to this class and add additional methods.

Question

From the assignment brief, along with the spelling of the word, a word in your lexicon needs to store the following information:The frequency: How many times the word appears in the input files.The list of neighbours: A neighbour of a word w is a word that is of the same length and differs from w by only one letter.The best way this can be implemented is as a Word class, where you can populate your lexicon with Word objects.Your task in this section is to create a class Word that is used to represent a Word in your lexicon.When you do this, you will need to:Think about what instance variables should be defined (and how they should be initialized)Think about what methods you need to implement for this classIn a future task, you will need to sort your lexicon full of Word objects. In the labs you saw a similar example where you needed to sort a collection of Person objects. It may be useful to refer back to this to see what methods were required.You may find that once you attempt the following tasks, you need to come back to this class and add additional methods.

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

To implement the Word class for your lexicon, you need to consider the following steps:

  1. Define the Instance Variables: These are the properties that each Word object will have. In this case, you need to store the word itself, its frequency, and its list of neighbours. These should be initialized in the constructor of the class.
class Word:
    def __init__(self, word):
        self.word = word
        self.frequency = 0
        self.neighbours = []
  1. Implement the Methods: These are the actions that each Word object can perform. You will need methods to update the frequency and add neighbours.
class Word:
    # ... previous code ...

    def update_frequency(self):
        self.frequency += 1

    def add_neighbour(self, neighbour):
        self.neighbours.append(neighbour)
  1. Implement Sorting: In a future task, you will need to sort your lexicon. Python's built-in sort function can be used if your class implements the __lt__ (less than) method.
class Word:
    # ... previous code ...

    def __lt__(self, other):
        return self.word < other.word

This is a basic implementation of the Word class. Depending on the requirements of your future tasks, you may need to come back and add additional methods or properties.

This problem has been solved

Similar Questions

InstructionsEach word in your lexicon should also store a list of neighbour words.From the assignment brief, a neighbour of a word w is a word that:Is of the same length as w and,Differs from w by only one letter.For example, if the word w refers to "cat", then:"cathy" is not w's neighbour, for they are not of the same length."man" is not w's neighbour, for they differ by more than one letter."can" is a neighbour of w"fat" is a neighbour of w"cot" is a neighbour of wIn this task, you should define a function add_neighbours() that takes one argument, your lexicon, and for every word in your lexicon, populates the list of neighbours that meet the criteria above.When adding neighbours, you should also ensure that the list of neighbours for each word is in sorted order.REMINDER: You are NOT allowed to use any of the builtin sorting functions/methods in Python (e.g. sorted() or .sort()).Once you have written this function, call it on your lexicon to populate the neighbours for each word in your lexicon.

In this step you should define a function read_data() that reads words from a text file into your lexicon.This function should take two arguments, the lexicon that words should be loaded into, and the name of the file to load.From the assignment brief, the criteria for a word is:A word is obtained as a sequence of characters separated by whitespaceWords should be stored in lowercaseAny numbers or punctuation characters should be removedUnique words should only be stored once in the lexicon (You can use the frequency of the word to show that a word appears twice in the files)Hint:Below is an example of how you can remove all punctiation and numeric characters from a string:my_str = 'H1-ell.0o'# Remove all punctuation and numeric charactersmy_str = ''.join([ch for ch in my_str if ch.isalpha()])print(my_str)>>> Hello

In this task, you should define two sorting algorithms that you can use to sort your lexicon of words. In the next task you will write a short program to use one of the sorting algorithms to sort your lexicon. Following sorting, words in your lexicon should be sorted in alphabetical order.It is entirely up to you which two sorting algorithms you include, as long as they are in the subject and different regarding time complexity.You should write one function per-sorting algorithm. Each of these functions should take a single parameter, the lexicon to be sorted. They should sort the lexicon in-place, so they do not need to return anything.

define 'word'and write an essay on 'word' as a syntactic unit

This task involves creating a lexicon that can store your collection of Word objects, and populating it with words loaded from two text files (in1.txt and in2.txt).More details on each of these steps is provided below, however in short, for this task you should:Create and initialize a variable that represents your lexicon.Define a function read_data() that can read words from a text file into your lexicon (Keeping in mind the constraints from the assignment brief).Call the read_data() function to load all words from the text files in1.txt and in2.txt into your lexicon.Step 1:It is up to you to decide on how you want to represent your lexicon. It may be useful to refer back to the labs to see how we represented collections of Person objects for the sorting algorithms that you implemented.Step 2:In this step you should define a function read_data() that reads words from a text file into your lexicon.This function should take two arguments, the lexicon that words should be loaded into, and the name of the file to load.From the assignment brief, the criteria for a word is:A word is obtained as a sequence of characters separated by whitespaceWords should be stored in lowercaseAny numbers or punctuation characters should be removedUnique words should only be stored once in the lexicon (You can use the frequency of the word to show that a word appears twice in the files)Hint:Below is an example of how you can remove all punctiation and numeric characters from a string:my_str = 'H1-ell.0o'# Remove all punctuation and numeric charactersmy_str = ''.join([ch for ch in my_str if ch.isalpha()])print(my_str)>>> HelloDo not be concerned with initializing the neighbours per-word. This will be done in Task 4.Step 3.In this step, you should make two calls to your read_data() function to load both in1.txt and in2.txt into your lexicon. When calling these functions, you can hardcode the names of these files.

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.