forked from ifmeorg/ifme
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Resource recommendation #1
Open
AlineRibeiro
wants to merge
70
commits into
master
Choose a base branch
from
resource_recommendation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
70 commits
Select commit
Hold shift + click to select a range
7ffef32
Render resources in view to test show action
AlineRibeiro 02540f2
Link each resource to their webpage and render a list
AlineRibeiro ade6417
Parse resources from json file
AlineRibeiro 4dd3c88
Create link to resources index
AlineRibeiro 0b51d51
Limit resourse each iteration to display maximmun of 3 items
AlineRibeiro c837408
Create ResourceRecommendation class
AlineRibeiro 082e87a
Create method to parse resources from json file
AlineRibeiro 2fb6310
Rename method parse_json to resources
AlineRibeiro 295e44f
Match a moment name with resources according to tags
AlineRibeiro a65d353
Match tag with why and fix
AlineRibeiro 19d2a2f
Code cleanup
AlineRibeiro 831d748
Downcase every word in the moment_keyword array
AlineRibeiro 811591d
Code clean up
AlineRibeiro 35eb7cb
Extract category names and compare to tags
AlineRibeiro 52004c6
Extract strategy names and compare to tags
AlineRibeiro 0195ada
Fix iteration that loops through moods
AlineRibeiro 7553127
Fix category name loops so it works for multi word names
AlineRibeiro 2ea93b8
Split words in category and strategy descriptions before pushing to t…
AlineRibeiro 959172c
Refactor strategy, mood and category description, so it removec html …
AlineRibeiro 901ee37
Remove html tags from moment why and fix
AlineRibeiro f578aa8
Remove all special charachters from moment keywords
AlineRibeiro b4914b0
Split multi-word tags with '_' into two separated words
AlineRibeiro dc7b0d7
Refactor ActionController::Base.helpers.strip_tags usages into method…
AlineRibeiro 5db0a22
Refacotor moment_name into a method
AlineRibeiro 389ee81
Refacotor moment_why into a method
AlineRibeiro 4c730d1
Refacotor moment_fix into a method
AlineRibeiro 153a098
Code clean up
AlineRibeiro 64388b7
Refactor all resources into a method
AlineRibeiro fee8f81
Remove unused variables from iteration loops
AlineRibeiro 576e08a
Move description and name loops into the same iteration
AlineRibeiro 129dd71
Refactor category keywords loop into a method
AlineRibeiro 785d0ce
Refactor mood keywords loop into a method
AlineRibeiro aff8e5e
Refactor strategy keywords loop into a method
AlineRibeiro f7f39a1
Make extracted methods private
AlineRibeiro 0de3cfc
Extract downcase_keyword method
AlineRibeiro e656b88
Rename category, mood and strategy related methods
AlineRibeiro 86d57d4
Refactor and rename moment_name, moment_fix, moment_why
AlineRibeiro 70519ad
Create method extract_moment_keywords
AlineRibeiro 703e9e6
Create remove_special_chars method to clean strings with gsub
AlineRibeiro a6a6dad
Substitute map for flat_map
AlineRibeiro 5183f90
Match any character that is not a letter
AlineRibeiro e5c4843
Select matched resources
AlineRibeiro dcb965e
Remove yarn.lock file from PR
AlineRibeiro a424065
Create MomentKeyword class and move keyword extraction methods inside it
AlineRibeiro 9f1f143
Rename class and file
AlineRibeiro be5f3fe
Get moment keywords from a MomentKeywords instance
AlineRibeiro 1c7f3ac
Refactor array extraction methods
AlineRibeiro fdb9de4
Match international characters
AlineRibeiro a98570e
Move remove_special_chars method to the moment_keywords class
AlineRibeiro 3f5862d
Remove reassignment from remove_special_chars method
AlineRibeiro 0a16b3b
Move downcase_keywords method to moment_keywords class
AlineRibeiro 0b407e7
Code clean up
AlineRibeiro 7e95790
Code clean up
AlineRibeiro 5584b17
Build a moment factory with customized content
AlineRibeiro ada08a6
Test if extract_moment_keywords downcases words properly
AlineRibeiro d917431
Refactor test to check if array contains a term
AlineRibeiro fde3041
Test is special chars are eliminated and hyphenated words are separated
AlineRibeiro 2d93d59
Turn keywords into a let symbol to avoi repetition
AlineRibeiro 950a472
Test if correct array of words is returned
AlineRibeiro b9b6448
Change gsub and splits to match self-care input with self_care tag
AlineRibeiro bb75e8a
Associate moment factory with categories factory
AlineRibeiro 8bbf9ac
Associate moment factory with moods and strategy factory and code cle…
AlineRibeiro 4be1a55
Create stub and test if resources method return the right resources
AlineRibeiro 70d44ed
Test if resources method wont return any resources if none of the tag…
AlineRibeiro 3c1a8a7
Code clean up
AlineRibeiro 33d2108
Refacot test to account for special chars and hyphen cases
AlineRibeiro 93eee5e
Test clean up
AlineRibeiro 85e4a00
Revert client yarn.lock
AlineRibeiro eaf70b4
Change subject to let
AlineRibeiro babf5f6
Change let to subject and code clean up
AlineRibeiro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# frozen_string_literal: true | ||
|
||
class MomentKeywords | ||
def initialize(moment) | ||
@moment = moment | ||
@moment_keywords = [] | ||
end | ||
|
||
def extract_moment_keywords | ||
extract_keywords(@moment.categories) | ||
extract_keywords(@moment.moods) | ||
extract_keywords(@moment.strategies) | ||
extract_moment_name | ||
extract_moment_why | ||
extract_moment_fix | ||
remove_special_chars | ||
downcase_keywords | ||
end | ||
|
||
private | ||
|
||
def strip_tags(str) | ||
ActionController::Base.helpers.strip_tags(str) | ||
end | ||
|
||
def extract_keywords(array) | ||
array.each do |item| | ||
@moment_keywords.push(item['name'].split, | ||
strip_tags(item['description']).split) | ||
end | ||
end | ||
|
||
def extract_moment_name | ||
@moment_keywords.push(@moment.name.split) | ||
end | ||
|
||
def extract_moment_why | ||
@moment_keywords.push(strip_tags(@moment.why).split) | ||
end | ||
|
||
def extract_moment_fix | ||
@moment_keywords.push(strip_tags(@moment.fix).split) | ||
end | ||
|
||
def remove_special_chars | ||
@moment_keywords = @moment_keywords.flatten.each do |keyword| | ||
keyword.tr!('-', ' ') | ||
keyword.gsub!(/[^\p{Alpha} -]/, '') | ||
end | ||
end | ||
|
||
def downcase_keywords | ||
@moment_keywords = @moment_keywords.map(&:downcase) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# frozen_string_literal: true | ||
|
||
class ResourceRecommendation | ||
def initialize(moment) | ||
@moment = moment | ||
@moment_keywords = [] | ||
end | ||
|
||
def resources | ||
@moment_keywords = MomentKeywords.new(@moment).extract_moment_keywords | ||
all_resources.select do |resource| | ||
tags = resource['tags'].flat_map do |tag| | ||
tag.tr('_', ' ') | ||
end | ||
(tags & @moment_keywords).any? | ||
end | ||
end | ||
|
||
private | ||
|
||
def all_resources | ||
JSON.parse(File.read(Rails.root.join('doc', 'pages', 'resources.json'))) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
describe MomentKeywords do | ||
let(:moment) do | ||
FactoryBot.build( | ||
:moment, | ||
categories: [build(:category, name: 'free', description: 'Description')], | ||
moods: [build(:mood, name: 'Name', description: 'Blog^^')], | ||
strategies: [build(:strategy, name: 'Name', description: 'books@!##.')], | ||
name: "ADDICTION", | ||
why: "self-care.", | ||
fix: "@Teachers!!" | ||
) | ||
end | ||
|
||
subject(:keywords) { MomentKeywords.new(moment).extract_moment_keywords } | ||
|
||
it 'downcases words' do | ||
expect(keywords).to include("addiction") | ||
end | ||
|
||
it 'removes hyphen from compound words' do | ||
expect(keywords).to include("self care") | ||
end | ||
|
||
it 'removes special characters' do | ||
expect(keywords).to include("teachers") | ||
end | ||
|
||
it 'returns an array of all the key words' do | ||
expect(keywords).to eq( | ||
["free", "description","name", "blog", "name", "books", | ||
"addiction", "self care", "teachers"] | ||
) | ||
end | ||
end | ||
|
||
|
||
|
||
|
||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,22 @@ | ||||||
describe ResourceRecommendation do | ||||||
subject(:moment) {FactoryBot.build(:moment, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
name: 'SELF-INJURY@', | ||||||
why: 'text', | ||||||
fix: 'text' )} | ||||||
|
||||||
let(:resources) { ResourceRecommendation.new(moment).resources} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
describe 'A test for resources method from ResourceRecommendation class' do | ||||||
it 'returns recommended resources based on moment keywords' do | ||||||
available_resource = [{'tags'=> ["self_injury"]}] | ||||||
allow(JSON).to receive(:parse) { available_resource } | ||||||
expect(resources).to eq(available_resource) | ||||||
end | ||||||
|
||||||
it 'does not return any matched resources' do | ||||||
available_resource2 = [{'tags'=> ['self_care', 'anonymous', 'ios', 'communities']}] | ||||||
allow(JSON).to receive(:parse) { available_resource2 } | ||||||
expect(resources).not_to eq(available_resource2) | ||||||
end | ||||||
end | ||||||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These extra empty lines must be removed