Class: Lbp::FilePart

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filepath, transcription_type, confighash, partid) ⇒ FilePart

Returns a new instance of FilePart.



9
10
11
12
13
14
15
16
17
18
# File 'lib/lbp/file_part.rb', line 9

def initialize(filepath, transcription_type, confighash, partid)

@confighash = confighash

@partid = partid
@filepath = filepath
@transcription_type = transcription_type
@element = self.element_name

end

Instance Attribute Details

#elementObject (readonly)

Returns the value of attribute element.



8
9
10
# File 'lib/lbp/file_part.rb', line 8

def element
  @element
end

#partidObject (readonly)

Returns the value of attribute partid.



8
9
10
# File 'lib/lbp/file_part.rb', line 8

def partid
  @partid
end

Instance Method Details

#element_nameObject



20
21
22
23
24
# File 'lib/lbp/file_part.rb', line 20

def element_name
	transcr = File.new(@filepath, @transcription_type, @confighash)
	xmlobject = transcr.nokogiri
	element_name = xmlobject.xpath("name(//node()[@xml:id='#{@partid}'])", 'tei' => 'http://www.tei-c.org/ns/1.0')
end

#nextObject

return part_number end



36
37
38
39
40
41
42
43
44
# File 'lib/lbp/file_part.rb', line 36

def next
	xmlobject = File.new(@filepath, @transcription_type, @confighash).nokogiri
	nextpartid = xmlobject.xpath("//tei:#{@element}[@xml:id='#{@partid}']/following::tei:#{@element}[1]/@xml:id", 'tei' => 'http://www.tei-c.org/ns/1.0')
if nextpartid.text == nil
     return nil
   else
	return FilePart.new(@filepath, @transcription_type, @confighash, nextpartid.text)
   end	
end

#number_of_zonesObject



54
55
56
57
58
59
# File 'lib/lbp/file_part.rb', line 54

def number_of_zones
	xmlobject = File.new(@filepath, @transcription_type, @confighash).nokogiri
	partid_with_hash = "#" + @partid
	result = xmlobject.xpath("/tei:TEI/tei:facsimile//tei:surface/tei:zone[@start='#{partid_with_hash}']", 'tei' => 'http://www.tei-c.org/ns/1.0')
	return result.count
end

#previousObject



45
46
47
48
49
50
51
52
53
# File 'lib/lbp/file_part.rb', line 45

def previous
	xmlobject = File.new(@filepath, @transcription_type, @confighash).nokogiri
	previouspartid = xmlobject.xpath("//tei:#{@element}[@xml:id='#{@partid}']/preceding::tei:#{@element}[1]/@xml:id", 'tei' => 'http://www.tei-c.org/ns/1.0')
	if previouspartid.empty?
     return nil
   else
	return FilePart.new(@filepath, @transcription_type, @confighash, previouspartid.text)
   end
end

#transform(xsltfile, xslt_param_array = []) ⇒ Object



66
67
68
69
70
# File 'lib/lbp/file_part.rb', line 66

def transform(xsltfile, xslt_param_array=[])
 	result = File.new(@filepath, @transcription_type, @confighash).transform(xsltfile, xslt_param_array)
	p = result.xpath("//#{@element}[@id='#{@partid}']")
	return p
end

#transform_plain_text(xslt_param_array = []) ⇒ Object

TODO not working because result of transformation is no longer valid xml document might be easier to pass the part if to the xslt_param_array and then return the result def transform_main_view(xslt_param_array=[]) result = File.new(@filepath, @transcription_type, @confighash).transform_main_view(xslt_param_array) #p = result.xpath(“//#@element”) #hard coding for the moment so it will only really work for paragraphs p = result.xpath(“//div”) return p end



81
82
83
84
85
86
87
88
# File 'lib/lbp/file_part.rb', line 81

def transform_plain_text(xslt_param_array=[])
	# not that it could be slightly confusing that paragraph plain text uses the transform clean,
	# because we still the basic paragraph elements in order to select the desired paragraph
	result = File.new(@filepath, @transcription_type, @confighash).transform_clean_nokogiri(xslt_param_array)
	
	p = result.xpath("//#{@element}[@id='#{@partid}']")
	return p
end

#word_arrayObject



94
95
96
97
98
# File 'lib/lbp/file_part.rb', line 94

def word_array
	plaintext = self.transform_plain_text
	word_array = plaintext.text.split
	word_array.map!{ |word| word.downcase}
end

#word_countObject



90
91
92
93
# File 'lib/lbp/file_part.rb', line 90

def word_count
	plaintext = self.transform_plain_text
	size = plaintext.text.split.size
end

#word_frequency(sort = 'frequency', order = 'descending') ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/lbp/file_part.rb', line 99

def word_frequency(sort='frequency', order='descending')
  	word_array = self.word_array
  	wf = Hash.new(0)
	word_array.each { |word| wf[word] += 1 }
	
	if sort == "frequency" 
		if order == "descending" # high to low
			wf = wf.sort_by{|k,v| v}.reverse
		elsif order == "ascending" # low to high
			wf = wf.sort_by{|k,v| v}
		end
	elsif sort == "word"
		if order == "descending" # z - a
				wf = wf.sort_by{|k,v| k}.reverse
		elsif order == "ascending" #a - z
				wf = wf.sort_by{|k,v| k}
		end
	end
	return wf.to_h
end

#xmlObject



61
62
63
64
# File 'lib/lbp/file_part.rb', line 61

def xml
	result = File.new(@filepath, @transcription_type, @confighash).nokogiri
	p = result.xpath("//tei:#{@element}[@xml:id='#{@partid}']", 'tei' => 'http://www.tei-c.org/ns/1.0')
end