Class: Researchmap2bib::ResearchmapReader

Inherits:
Object
  • Object
show all
Defined in:
lib/researchmap2bib.rb

Instance Method Summary collapse

Instance Method Details

#concatenate_authors(authors) ⇒ Object



133
134
135
# File 'lib/researchmap2bib.rb', line 133

def concatenate_authors(authors)
  authors.gsub(/ *, */, ' and ')
end

#family_name(author) ⇒ Object



142
143
144
145
146
147
148
149
150
# File 'lib/researchmap2bib.rb', line 142

def family_name(author)
  if /(^\w*) *(\w*)$/ =~ author
    $2
  elsif /(^\S*) *(\S*)$/ =~ author
    $1
  else
    author
  end
end

#first_author(authors) ⇒ Object



137
138
139
140
# File 'lib/researchmap2bib.rb', line 137

def first_author(authors)
  r = /([^,]*)/.match(authors)
  r == nil ? authors.strip : r.captures[0].strip
end

#generate_key(authors, date) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
# File 'lib/researchmap2bib.rb', line 152

def generate_key(authors, date)
  first_author = first_author(authors)
  name = family_name(first_author)
  year, month = year_month(date)
  year = /[0-9][0-9]([0-9][0-9])/.match(year).captures[0]
  if month.to_i >0
    name + year + month
  else
    name + year
  end
end

#read_entry(entry) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/researchmap2bib.rb', line 33

def read_entry(entry)
  title           = entry.elements["title"].text
  author          = entry.elements["author"].elements["name"].text
  author          = to_hankaku(author)
  summary         = entry.elements["summary"].text
  journal         = entry.elements["rm:journal"].text
  publisher       = entry.elements["rm:publisher"].text
  publicationName = entry.elements["rm:publicationName"].text
  volume          = entry.elements["rm:volume"].text
  number          = entry.elements["rm:number"].text
  startingPage    = entry.elements["rm:startingPage"].text
  endingPage      = entry.elements["rm:endingPage"].text
  publicationDate = entry.elements["rm:publicationDate"].text
  referee         = entry.elements["rm:referee"] ? true : false
  language        = entry.elements["rm:language"].text
  paperType       = entry.elements["rm:paperType"].elements["id"].text
    
  Entry.new(
    title, author, summary, journal, publisher, publicationName,
    volume, number, startingPage, endingPage, publicationDate,
    referee, language, paperType,
  )
end

#read_paper(xml) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/researchmap2bib.rb', line 24

def read_paper(xml)
  doc = REXML::Document.new(xml)
  entries = Array.new
  REXML::XPath.each(doc, '/feed/entry') do |entry|
    entries.push(read_entry(entry))
  end
  entries
end

#read_researchmap(file_name) ⇒ Object



17
18
19
20
21
22
# File 'lib/researchmap2bib.rb', line 17

def read_researchmap(file_name)
  entries = Zip::File.open(file_name) do |zip_file|
    entry = zip_file.glob('*\/paper.xml').first
    read_paper(entry.get_input_stream.read)
  end
end

#to_hankaku(str) ⇒ Object



125
126
127
# File 'lib/researchmap2bib.rb', line 125

def to_hankaku(str)
  str.tr('0-9a-zA-Z,、 ', '0-9a-zA-Z,, ')
end

#write_bibliography_entry(entry) ⇒ Object

0:未設定、1:研究論文(学術雑誌)、2:研究論文(国際会議プロシーディングス)、3:研究論文(大学,研究機関紀要) 、4:研究論文(研究会,シンポジウム資料等)、5:研究論文(その他学術会議資料等)



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/researchmap2bib.rb', line 60

def write_bibliography_entry(entry)
  key = generate_key(entry.author, entry.publicationDate)
  year, month = year_month(entry.publicationDate)
  month = month.to_i
  case entry.paperType.to_i
  when 1, 3 then
    record = <<EOS
@Article{<%= key %>,
  author = 	 {<%= entry.author %>},
  title = 	 {<%= entry.title %>},
  journal = 	 {<%= entry.journal %>},
EOS
    record += '  year      = <%= year %>'
    if month > 0
      record += ",\n  month     = <%= month %>"
    end
    if entry.volume
      record += ",\n  volume    = <%= entry.volume %>"
    end
    if entry.number
      record += ",\n  number    = <%= entry.number %>"
    end
    if entry.startingPage && entry.endingPage
      record += ",\n  pages     = <%= entry.startingPage %>-<%= entry.endingPage %>"
    elsif entry.startingPage
      record += ",\n  pages     = <%= entry.startingPage %> %>"
    end
    record += "}\n"
    if entry.summary
      record += "% <%= entry.summary %>\n"
    end
  else
    record = <<EOS
@InProceedings{<%= key %>,
  author    = {<%= entry.author %>},
  title     = {<%= entry.title %>},
  booktitle = {<%= entry.journal %>},
EOS
    record += '  year      = <%= year %>'
    if month > 0
      record += ",\n  month     = <%= month %>"
    end
    if entry.volume
      record += ",\n  volume    = <%= entry.volume %>"
    end
    if entry.number
      record += ",\n  number    = <%= entry.number %>"
    end
    if entry.startingPage && entry.endingPage
      record += ",\n  pages     = <%= entry.startingPage %>-<%= entry.endingPage %>"
    elsif entry.startingPage
      record += ",\n  pages     = <%= entry.startingPage %> %>"
    end
    if entry.publisher
      record += ",\n  publisher = {<%= entry.publisher %>}"
    end
    record += "}\n"
    if entry.summary
      record += "% <%= entry.summary %>\n"
    end
  end
  erb = ERB.new(record)
  erb.result(binding)
end

#year_month(date) ⇒ Object



129
130
131
# File 'lib/researchmap2bib.rb', line 129

def year_month(date)
  /([0-9]{4})([0-9]{2})/.match(date).captures
end