Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
app/controllers/static_pages_controller.rb
# -*- coding: utf-8 -*-
require 'open3'
require 'json'
class StaticPagesController < ApplicationController
# 検索画面に相当するアクション
def search
end
# 検索結果画面に相当するアクション
def result
command = Rails.root.to_s + "/lib/crawler/google_scholar_crawler.py"
query = params[:search_string]
if query.strip! == ""
return
end
query = query.gsub(/(\s| )+/, "+")
@text_field_val = params[:search_string] if params[:search_string]
command += " " + query
out, err, status = Open3.capture3(command)
json = JSON.parser.new(out)
@articles = json.parse() # JSONをパースして得られたオブジェクト
@@graph = {nodes: {}, edges: {}} # 今回JavaScript側に送りたいJSON
@articles.each do |article|
cid = article["cluster_id"][0].to_s
@@graph[:nodes][cid] = {}
@@graph[:edges][cid] = {}
citation = article["citation"][0]
citation.each do |cit|
@@graph[:edges][cid][cid + "_" + cit["num"].to_s] = {directed: true, weight: 3}
end
end
end
# JSONをJavaScript側に送るためのアクション
def get_citation
render :json => @@graph # JSONを返す
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment