Skip to content

Instantly share code, notes, and snippets.

@lethe2211
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lethe2211/642cde2a48a8ae9731f7 to your computer and use it in GitHub Desktop.
Save lethe2211/642cde2a48a8ae9731f7 to your computer and use it in GitHub Desktop.
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