top of page

Identify patterns in a string of text In Ruby

Assignment


Using the provided `./sentence.rb` file, please create a `Sentence` class that implements the following behavior:

- `#initialize(sentence)` Given a string, initializes the class with this string as its sentence

- `#longest_word` Returns the longest word in the sentence

- `#word_count` Returns the number of unique words in the sentence

- `#count(word)` Given a word, returns the number of occurrences in the sentence (case insensitive)


You may assume the sentence is immutable (i.e. it will not change after creation).


This exercise requires you to identify patterns in a string of text. Problems like this are commonly solved with a

tool called Regular Expressions (RegEx). We are not assessing your familiarity with RegEx syntax for this position; for

this reason, we've supplied a RegEx pattern that you can use to match words within the sample sentence (the backtick

symbols provide markdown formatting; don't copy them):


`/[\w']+/i`


It's up to you to decide if and how you'll use this RegEx pattern for the exercise.


---


Testing


The included `./test_sentence.rb` file defines the expected behavior of your `Sentence` class. To run the tests, open a

terminal and navigate to the project's root directory:


`$ cd <INSERT PATH TO PROJECT>/homework/`


Then install the `minitest` gem:


`$ gem install minitest`


And run the test file:


`$ ruby -Ilib:test test/test_sentence.rb`


See the test results printed to your terminal output:


```

Run options: --seed 34256


# Running:


.......


Finished in 0.001057s, 6622.5172 runs/s, 6622.5172 assertions/s.


7 runs, 7 assertions, 0 failures, 0 errors, 0 skips

```


See the `minitest` [documentation](https://github.com/seattlerb/minitest) for more information.


---


Extra Challenges

_(optional)_


1. Read this blog post on ["memoization"](https://www.honeybadger.io/blog/ruby-rails-memoization/) in Ruby, and

implement this behavior for your sentence class.


2. Read this blog post on [Regular Expressions](https://www.rubyguides.com/2015/06/ruby-regex/) in Ruby, and include a

`readme.md` file in your submission to deconstruct and explain the RegEx pattern above.


3. Rename your `Sentence` class to `Speech` and extend it to implement the following additional behavior:

- `#initialize(path)` Given a path to a text file, initializes the class with the contents of the file as its speech

- `#most_used_words(count)` Given an integer, returns a list containing the most used words in the speech (sorted by

frequency)

- **Note:** The included `./test_speech.rb` file defines the expected behavior of your `Speech` class.



Solution screenshots:


In this assignment there are two test file to check your assignment solutions

test_sentence.rb and test_speech.rb


For test_sentence.rb solution output: we have made solution which pass all the test cases


For test_speech.rb solution output: we have made solution which pass all the test cases except one test case



We provide programming assignment help, programming assignment solution on demands at affordable price rate. If you need help/solution simply reply to chat, we are online to help you or send your assignment request at contact@codersarts.com

Comments


bottom of page