-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from cre-ne-jp/keywords-rails6
キーワード抽出
- Loading branch information
Showing
42 changed files
with
863 additions
and
17 deletions.
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
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
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,37 @@ | ||
class KeywordsController < ApplicationController | ||
def index | ||
relationships = PrivmsgKeywordRelationship.arel_table | ||
keywords = Keyword.arel_table | ||
|
||
privmsg_counts = relationships | ||
.group(:keyword_id) | ||
.project(:keyword_id, Arel.sql('COUNT(*) AS privmsg_count')) | ||
.as('privmsg_counts') | ||
join_cond = keywords | ||
.join(privmsg_counts, Arel::Nodes::InnerJoin) | ||
.on(keywords[:id].eq(privmsg_counts[:keyword_id])) | ||
.join_sources | ||
|
||
page_i = params[:page].to_i | ||
page = page_i >= 1 ? page_i : 1 | ||
@keyword_privmsg_counts = Keyword | ||
.joins(join_cond) | ||
.select('keywords.*', 'privmsg_counts.privmsg_count') | ||
.order('privmsg_count DESC', 'id DESC') | ||
.page(page) | ||
end | ||
|
||
def show | ||
@keyword = Keyword.friendly.find(params[:id]) | ||
|
||
page_i = params[:page].to_i | ||
page = page_i >= 1 ? page_i : 1 | ||
@logs = @keyword.privmsgs | ||
.page(page) | ||
.joins(:channel) | ||
.select(:channel_id, 'DATE(timestamp) AS date', 'channels.row_order') | ||
.distinct | ||
.order('date DESC', 'channels.row_order ASC') | ||
.preload(:channel) | ||
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
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,66 @@ | ||
require 'nkf' | ||
require 'strscan' | ||
|
||
class Keyword < ApplicationRecord | ||
extend FriendlyId | ||
friendly_id :title | ||
|
||
has_many :privmsg_keyword_relationships, dependent: :destroy | ||
has_many :privmsgs, through: :privmsg_keyword_relationships | ||
|
||
validates(:title, | ||
presence: true) | ||
validates(:display_title, | ||
presence: true) | ||
|
||
# 全文検索 | ||
scope :full_text_search, ->query { | ||
where('MATCH(display_title) AGAINST(? IN BOOLEAN MODE)', "*D+ #{query}") | ||
} | ||
|
||
# キーワードの表記を正規化する | ||
# @param [String] text 正規化するテキスト | ||
# @return [String] | ||
# | ||
# * 英数字といくつかの記号を半角にする | ||
# * 半角片仮名を全角にする | ||
# * 全角空白を半角にする | ||
# * パスに使われる記号をアンダーバーに置換する | ||
# * 空白をアンダーバーに置換する | ||
# * アンダーバーの出現回数を最小化する | ||
def self.normalize(text) | ||
nkfed = NKF.nkf('-Ww -Z0 -Z1 -X -m0', text) | ||
|
||
# URL生成時に失敗しないよう、パスに使われる記号を置換する | ||
symbols_for_path_replaced = nkfed.gsub(%r![./]!, '_') | ||
|
||
spaces_replaced = symbols_for_path_replaced.gsub(/\s/, '_') | ||
underbars_trimmed = spaces_replaced. | ||
sub(/\A_+/, ''). | ||
sub(/_+\z/, '') | ||
|
||
underbars_shortened = String.new(underbars_trimmed) | ||
ss = StringScanner.new(underbars_shortened) | ||
|
||
# 以下の3通りのいずれか | ||
# | ||
# 1. 英数字以外 アンダーバー 任意の文字 | ||
# 2. 英数字 アンダーバー 英数字以外 | ||
# 3. 英数字 アンダーバー2個以上 英数字 | ||
re = /([^_A-Z0-9])(_+)[^_]|([A-Z0-9])(_+)[^_A-Z0-9]|([A-Z0-9]_)(_+)[A-Z0-9]/i | ||
while ss.scan_until(re) | ||
pre_underbars_length = (ss[1] || ss[3] || ss[5]).length | ||
underbars_length = (ss[2] || ss[4] || ss[6]).length | ||
|
||
# 余分なアンダーバーを除去する | ||
underbars_shortened.slice!(ss.pre_match.length + pre_underbars_length, | ||
underbars_length) | ||
|
||
# 除去したアンダーバーの文字数も考慮しながら | ||
# マッチした部分の最後の文字まで戻す | ||
ss.pos -= (underbars_length + 1) | ||
end | ||
|
||
underbars_shortened.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
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,12 @@ | ||
class PrivmsgKeywordRelationship < ApplicationRecord | ||
belongs_to :privmsg | ||
belongs_to :keyword | ||
|
||
validates :privmsg_id, presence: true | ||
validates :keyword_id, presence: true | ||
|
||
scope :from_privmsgs, ->privmsgs { | ||
includes(:keyword) | ||
.where(privmsg: privmsgs) | ||
} | ||
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
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,37 @@ | ||
<% title('キーワード') %> | ||
<div class="container main-container"> | ||
<div class="row"> | ||
<div class="col-xs-12"> | ||
<div class="main-panel"> | ||
<div class="panel-body"> | ||
<%= render('shared/flash') %> | ||
|
||
<div class="page-header"> | ||
<h1>キーワード</h1> | ||
</div> | ||
|
||
<p><%= page_entries_info(@keyword_privmsg_counts) %></p> | ||
|
||
<table class="table table-striped keyword-list"> | ||
<thead> | ||
<tr> | ||
<th class="keyword-keyword">キーワード</th> | ||
<th class="keyword-num-of-privmsgs">発言数</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<% @keyword_privmsg_counts.each do |keyword| %> | ||
<tr> | ||
<td class="keyword-keyword"><%= link_to(h(keyword.display_title), keyword) %></td> | ||
<td class="keyword-num-of-privmsgs"><%= keyword.privmsg_count %></td> | ||
</tr> | ||
<% end %> | ||
</tbody> | ||
</table> | ||
|
||
<%= paginate(@keyword_privmsg_counts, outer_window: 2) %> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> |
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,48 @@ | ||
<% title(@keyword.display_title) %> | ||
<div class="container main-container"> | ||
<div class="row"> | ||
<div class="col-xs-12"> | ||
<div class="main-panel"> | ||
<div class="panel-body"> | ||
<%= render('shared/flash') %> | ||
|
||
<ol class="breadcrumb"> | ||
<li><%= link_to('キーワード', keywords_path) %></li> | ||
<li class="active"><%= @keyword.display_title %></li> | ||
</ol> | ||
|
||
<article> | ||
<div class="page-header"> | ||
<h1>キーワード「<%= @keyword.display_title %>」を含むログ</h1> | ||
</div> | ||
|
||
<p><%= page_entries_info(@logs, entry_name: 'ログ') %></p> | ||
|
||
<table class="table table-striped channel-date-list"> | ||
<thead> | ||
<tr> | ||
<th class="channel-date-channel">チャンネル</th> | ||
<th class="channel-date-date">日付</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<% style = message_list_style(cookies) %> | ||
<% @logs.each do |log| %> | ||
<% channel = log.channel %> | ||
<% date = log.date %> | ||
<% browse_day = ChannelBrowse::Day.new(channel: channel, date: date, style: style) %> | ||
<tr> | ||
<td class="channel-date-channel"><%= link_to(channel.name_with_prefix, channel) %></td> | ||
<td class="channel-date-date"><%= link_to(date.strftime('%F'), browse_day.path) %></td> | ||
</tr> | ||
<% end %> | ||
</tbody> | ||
</table> | ||
|
||
<%= paginate(@logs, outer_window: 2) %> | ||
</article> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> |
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
Oops, something went wrong.