-
Notifications
You must be signed in to change notification settings - Fork 43
2312-3 #289
base: master
Are you sure you want to change the base?
2312-3 #289
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
source 'https://rubygems.org' | ||
|
||
gem 'haml' | ||
gem 'mechanize' | ||
gem 'ohm' | ||
gem 'redis' | ||
gem 'scss' | ||
gem 'sinatra' | ||
gem 'sinatra-asset-pipeline' | ||
gem 'sinatra-static-assets' | ||
|
||
group :development do | ||
gem 'rake' | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
GEM | ||
remote: https://rubygems.org/ | ||
specs: | ||
coffee-script (2.4.1) | ||
coffee-script-source | ||
execjs | ||
coffee-script-source (1.12.2) | ||
concurrent-ruby (1.0.5) | ||
connection_pool (2.2.2) | ||
domain_name (0.5.20180417) | ||
unf (>= 0.0.5, < 1.0.0) | ||
execjs (2.7.0) | ||
ffi (1.9.25) | ||
haml (5.0.4) | ||
temple (>= 0.8.0) | ||
tilt | ||
hiredis (0.6.1) | ||
http-cookie (1.0.3) | ||
domain_name (~> 0.5) | ||
mechanize (2.7.6) | ||
domain_name (~> 0.5, >= 0.5.1) | ||
http-cookie (~> 1.0) | ||
mime-types (>= 1.17.2) | ||
net-http-digest_auth (~> 1.1, >= 1.1.1) | ||
net-http-persistent (>= 2.5.2) | ||
nokogiri (~> 1.6) | ||
ntlm-http (~> 0.1, >= 0.1.1) | ||
webrobots (>= 0.0.9, < 0.2) | ||
mime-types (3.1) | ||
mime-types-data (~> 3.2015) | ||
mime-types-data (3.2016.0521) | ||
mini_portile2 (2.3.0) | ||
mustermann (1.0.2) | ||
nest (3.1.1) | ||
redic | ||
net-http-digest_auth (1.4.1) | ||
net-http-persistent (3.0.0) | ||
connection_pool (~> 2.2) | ||
nokogiri (1.8.4) | ||
mini_portile2 (~> 2.3.0) | ||
ntlm-http (0.1.1) | ||
ohm (3.1.1) | ||
nest (~> 3) | ||
redic (~> 1.5.0) | ||
stal | ||
rack (2.0.5) | ||
rack-protection (2.0.3) | ||
rack | ||
rake (12.3.1) | ||
rb-fsevent (0.10.3) | ||
rb-inotify (0.9.10) | ||
ffi (>= 0.5.0, < 2) | ||
redic (1.5.0) | ||
hiredis | ||
redis (4.0.1) | ||
sass (3.5.7) | ||
sass-listen (~> 4.0.0) | ||
sass-listen (4.0.0) | ||
rb-fsevent (~> 0.9, >= 0.9.4) | ||
rb-inotify (~> 0.9, >= 0.9.7) | ||
scss (1.0.2) | ||
sinatra (2.0.3) | ||
mustermann (~> 1.0) | ||
rack (~> 2.0) | ||
rack-protection (= 2.0.3) | ||
tilt (~> 2.0) | ||
sinatra-asset-pipeline (2.1.0) | ||
coffee-script (~> 2.4) | ||
rake (~> 12.3) | ||
sass (~> 3.5) | ||
sinatra (~> 2.0) | ||
sprockets (~> 3.7) | ||
sprockets-helpers (~> 1.2) | ||
sinatra-static-assets (1.0.4) | ||
sinatra (>= 1.1.0) | ||
sprockets (3.7.2) | ||
concurrent-ruby (~> 1.0) | ||
rack (> 1, < 3) | ||
sprockets-helpers (1.2.1) | ||
sprockets (>= 2.2) | ||
stal (0.3.0) | ||
redic (~> 1.5) | ||
temple (0.8.0) | ||
tilt (2.0.8) | ||
unf (0.1.4) | ||
unf_ext | ||
unf_ext (0.0.7.5) | ||
webrobots (0.1.2) | ||
|
||
PLATFORMS | ||
ruby | ||
|
||
DEPENDENCIES | ||
haml | ||
mechanize | ||
ohm | ||
rake | ||
redis | ||
scss | ||
sinatra | ||
sinatra-asset-pipeline | ||
sinatra-static-assets | ||
|
||
BUNDLED WITH | ||
1.16.2 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
require 'sinatra/asset_pipeline/task' | ||
require './commen_analyzer' | ||
|
||
Sinatra::AssetPipeline::Task.define! CommentAnalyzer |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
require_relative 'comment_analyzer' | ||
CommentAnalyzer.run! |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
require 'bundler' | ||
Bundler.require | ||
require 'json' | ||
require 'haml' | ||
require 'mechanize' | ||
require 'ohm' | ||
require 'redis' | ||
require 'sass' | ||
require 'sinatra' | ||
require 'sinatra/asset_pipeline' | ||
require 'sinatra/static_assets' | ||
# Prepare assets to application | ||
class CommentAnalyzer < Sinatra::Base | ||
set :root, File.dirname(__FILE__) | ||
set :assets_precompile, %w(*.css *.png *.jpg *.svg) | ||
set :assets_paths, ['assets/stylesheets', 'assets/images'] | ||
set :assets_css_compressor, :sass | ||
register Sinatra::AssetPipeline | ||
def image_tag(path) | ||
"<img src=\"#{image_path(path)}\"/>" | ||
end | ||
get '/' do | ||
haml :index | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
require './onliner_parser' | ||
require './comments_reader' | ||
require './api/text_analytics' | ||
require './comments_rating' | ||
# Class analyze comments and create json file | ||
class CommentsJSON | ||
attr_reader :json | ||
def initialize(link) | ||
comments_array = CommentsReader.new(link).comments_list | ||
response_from_azure = TextAnalytic.new(comments_array).json | ||
@json = CommentsRating.new(response_from_azure, comments_array).rating | ||
end | ||
end | ||
puts CommentsJSON.new('https://tech.onliner.by/2018/07/24/dji-4').json |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
require './api/text_analytics.rb' | ||
require './comments_reader.rb' | ||
# This class add rating to each comment | ||
class CommentsRating | ||
attr_reader :response_from_azure, :api_rating, :comments_rating, :rating, :comments_array | ||
def initialize(response_from_azure, comments_array) | ||
@api_rating = response_from_azure['documents'] | ||
@comments_array = comments_array | ||
@comments_rating = {} | ||
calculate_comments_ratings | ||
@rating = { post_rating: post_rating, | ||
comments_ratings: comments_rating, | ||
comments_id: comments_ids } | ||
end | ||
private | ||
def post_rating | ||
rating = 0 | ||
comments_rating.each { |_comment, coments_rating| rating += coments_rating } | ||
(rating / comments_rating.size).to_i | ||
end | ||
def calculate_comments_ratings | ||
api_rating.each do |comment| | ||
id = comment['id'].to_i | ||
comments_rating[id.to_i] = (comment['score'] * 200 - 100).to_i | ||
end | ||
end | ||
def comments_ids | ||
(0...api_rating.size).each_with_object({}) { |id, hash| hash[id] = comments_array[id] } | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
require './onliner_parser' | ||
# This class extracts comments from json | ||
class CommentsReader | ||
attr_reader :link, :comments_list | ||
def initialize(link) | ||
@link = link | ||
@comments_list = search_comments | ||
end | ||
private | ||
def search_comments | ||
OnlinerParser.new(link).json['comments'].map { |comment| comment['text'] } | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
require_relative 'comment_analyzer' | ||
run CommentAnalyzer |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class Comment < Ohm::Model | ||
attribute :text | ||
attribute :rating | ||
collection :post, :Post | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class Post < Ohm::Model | ||
attribute :link | ||
attribute :post_rating | ||
collection :comment, :Comment | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
require 'mechanize' | ||
require 'json' | ||
# This class search post id from link to after send this id and get json with comments | ||
class OnlinerParser | ||
attr_reader :link, :json | ||
COMMENTS_LIMIT = 50 | ||
def initialize(link) | ||
@link = link | ||
@json = parse_json | ||
end | ||
private | ||
def search_post_id | ||
Mechanize.new.get(link).search('app[entity-id]').to_s.match(/\d+/).to_s | ||
end | ||
def import_json_from_api | ||
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. Layout/EmptyLineBetweenDefs: Use empty lines between method definitions. |
||
"https://comments.api.onliner.by/news/tech.post/#{search_post_id}/comments?limit=#{COMMENTS_LIMIT}" | ||
end | ||
def parse_json | ||
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. Layout/EmptyLineBetweenDefs: Use empty lines between method definitions. |
||
JSON.parse Mechanize.new.get(import_json_from_api).body | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
require 'net/https' | ||
require 'uri' | ||
require 'json' | ||
# Class send request to Azure API | ||
class RequestApi | ||
attr_reader :uri, :request | ||
ACCESS_KEY = 'paste_your_key'.freeze | ||
def initialize(data) | ||
@uri = URI('https://westeurope.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment') | ||
@request = create_request(data) | ||
end | ||
def create_request(data) | ||
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. Layout/EmptyLineBetweenDefs: Use empty lines between method definitions. |
||
request = Net::HTTP::Post.new(uri) | ||
request['Content-Type'] = 'application/json' | ||
request['Ocp-Apim-Subscription-Key'] = ACCESS_KEY | ||
request.body = data.to_json | ||
request | ||
end | ||
def request_respond | ||
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. Layout/EmptyLineBetweenDefs: Use empty lines between method definitions. |
||
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http| | ||
http.request(request) | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
$background_color: #e7e5e4; | ||
$black_color: #131313; | ||
$white_color: #ffffff; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
@mixin flex-container($direction, $wrap, $justify, $align) { | ||
display: flex; flex-flow: $direction $wrap; justify-content: $justify; align-items: $align; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
@import 'colors'; | ||
@import 'mixins'; | ||
@import 'pages/index'; | ||
|
||
html, body { | ||
width: 100%; height: 100%; margin: 0; padding: 0; background: $background-color; | ||
font-family: 'Lato', sans-serif; font-weight: 400; font-size: 16px; | ||
} | ||
body { min-height: 100%; } | ||
main, main .page { width: 100%; height: 100%; } | ||
|
||
h1, h2, h3 { margin: 0 0 1rem 0; } | ||
|
||
h1 { font-size: 3em; } | ||
h2 { font-size: 2em; } | ||
h3 { font-size: 1em; } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
main .page.indexPage { | ||
@include flex-container(column, nowrap, center, center); | ||
text-align: center; | ||
.title { | ||
margin-bottom: 5em; | ||
h1 { | ||
color: $black_color; font-size: 5.75em; letter-spacing: .15em; | ||
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. Name of variable |
||
font-weight: 400; text-shadow: | ||
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. Property 'text-shadow' should be placed on own line |
||
1px -1px 0 #767676, -1px 2px 1px #737272, | ||
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. Color literals like |
||
-2px 4px 1px #767474, -3px 6px 1px #787777, | ||
-4px 8px 1px #7b7a7a, -5px 10px 1px #7f7d7d, | ||
-6px 12px 1px #828181, -7px 14px 1px #868585, | ||
-8px 16px 1px #8b8a89, -9px 18px 1px #8f8e8d, | ||
-10px 20px 1px #949392, -11px 22px 1px #999897, | ||
-12px 24px 1px #9e9c9c, -13px 26px 1px #a3a1a1, | ||
-14px 28px 1px #a8a6a6, -15px 30px 1px #adabab, | ||
-16px 32px 1px #b2b1b0, -17px 34px 1px #b7b6b5, | ||
-18px 36px 1px #bcbbba, -19px 38px 1px #c1bfbf, | ||
-20px 40px 1px #c6c4c4, -21px 42px 1px #cbc9c8, | ||
-22px 44px 1px #cfcdcd, -23px 46px 1px #d4d2d1, | ||
-24px 48px 1px #d8d6d5, -25px 50px 1px #dbdad9, | ||
-26px 52px 1px #dfdddc, -27px 54px 1px #e2e0df, | ||
-28px 56px 1px #e4e3e2; } | ||
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. Rule declaration should be followed by an empty line |
||
text-rendering: optimizeLegibility; | ||
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. Expected item on line 24 to appear before line 6. Rule sets should be ordered as follows: |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
require './api/request_api.rb' | ||
require 'json' | ||
# This class works with Microsoft Azure Text Analytic API | ||
class TextAnalytic | ||
attr_reader :comments, :request_api, :request, :request_respond | ||
def initialize(comments_list) | ||
@comments = comments_list | ||
@request_api = RequestApi.new(request_data) | ||
@request = request_api.request | ||
@request_respond = request_api.request_respond | ||
end | ||
def json | ||
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. Layout/EmptyLineBetweenDefs: Use empty lines between method definitions. |
||
JSON.parse request_respond.body | ||
end | ||
private | ||
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. Layout/EmptyLinesAroundAccessModifier: Keep a blank line before and after private. |
||
def request_data | ||
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. TooManyStatements: TextAnalytic#request_data has approx 8 statements. More info. |
||
data = {} | ||
comment_id = -1 | ||
hash_array = [] | ||
comments.each do |comment| | ||
comment_id += 1 | ||
hash_array << { 'id' => comment_id, 'language' => 'ru', 'text' => comment } | ||
end | ||
data['documents'] = hash_array | ||
data | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.page.indexPage | ||
.title | ||
%h1 Comment Analyzer | ||
%form | ||
%input | ||
%button Анализировать |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
!!! | ||
%html | ||
%head | ||
%title Comment Analyzer | ||
= stylesheet_tag 'app' | ||
%link(href='https://fonts.googleapis.com/css?family=Lato:400,400italic,700,700italic' rel='stylesheet' type='text/css') | ||
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. Line is too long. [123/80] |
||
%link{ :type => "image/png", :href => image_path("favicon.ico"), :rel => "shortcut icon" } | ||
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. Line is too long. [94/80] |
||
%meta{:name => 'viewport', :content => 'width=device-width; initial-scale=1; user-scalable=0;'} | ||
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. Line is too long. [99/80] |
||
%body | ||
%main= yield |
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.
Layout/EmptyLinesAroundAccessModifier: Keep a blank line before and after private.