Class: Mizuho::IdMap

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/mizuho/id_map.rb

Defined Under Namespace

Classes: AlreadyAssociatedError, Entry

Constant Summary collapse

URANDOM =
File.open("/dev/urandom", "rb")
MATCHER =
JaroWinklerPure.new
"###### Autogenerated by Mizuho, DO NOT EDIT ######\n" <<
"# This file maps section names to IDs so that the commenting system knows which\n" <<
"# comments belong to which section. Section names may be changed at will but\n" <<
"# IDs always stay the same, allowing one to retain old comments even if you\n" <<
"# rename a section.\n" <<
"#\n" <<
"# This file is autogenerated but is not a cache; you MUST NOT DELETE this\n" <<
"# file and you must check it into your version control system. If you lose\n" <<
"# this file you may lose the ability to identity old comments.\n" <<
"#\n" <<
"# Entries marked with \"fuzzy\" indicate that the section title has changed\n" <<
"# and that Mizuho has found an ID which appears to be associated with that\n" <<
"# section. You should check whether it is correct, and if not, fix it.\n\n"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#chapter_to_int_array, #extract_chapter, included, #title_to_docid

Constructor Details

#initializeIdMap

Returns a new instance of IdMap.



52
53
54
55
56
# File 'lib/mizuho/id_map.rb', line 52

def initialize
	@entries = {}
	@associations = {}
	#@namespace = slug(File.basename(filename, File.extname(filename)))
end

Instance Attribute Details

#associationsObject (readonly)

Returns the value of attribute associations.



50
51
52
# File 'lib/mizuho/id_map.rb', line 50

def associations
  @associations
end

#entriesObject (readonly)

Returns the value of attribute entries.



50
51
52
# File 'lib/mizuho/id_map.rb', line 50

def entries
  @entries
end

Instance Method Details

#add(title, id, *options) ⇒ Object



186
187
188
# File 'lib/mizuho/id_map.rb', line 186

def add(title, id, *options)
	return @entries[title] = Entry.new(title, id || create_unique_id(title), *options)
end

#generate_associations(titles) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/mizuho/id_map.rb', line 106

def generate_associations(titles)
	@associations = {}

	# Associate exact matches.
	titles = titles.reject do |title|
		if (entry = @entries[title]) && !entry.associated?
			entry.associated = true
			@associations[title] = entry.id
			true
		else
			false
		end
	end

	# For the remaining titles, associate with moved or similar-looking entry.
	titles.reject! do |title|
		if entry = find_moved(title)
			@entries.delete(entry.title)
			@entries[title] = entry
			entry.title = title
			entry.associated = true
			entry.fuzzy = false
			@associations[title] = entry.id
			true
		else
			false
		end
	end

	# For the remaining titles, associate with similar-looking entry.
	titles.reject! do |title|
		if entry = find_similar(title)
			@entries.delete(entry.title)
			@entries[title] = entry
			entry.title = title
			entry.associated = true
			entry.fuzzy = true
			@associations[title] = entry.id
			true
		else
			false
		end
	end

	# For the remaining titles, create new entries.
	titles.each do |title|
		id = create_unique_id(title)
		add(title, id, false, true)
		@associations[title] = id
	end
end

#load(filename_or_io) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/mizuho/id_map.rb', line 58

def load(filename_or_io)
	@entries.clear
	open_io(filename_or_io, :read) do |io|
		fuzzy = false
		while true
			begin
				line = io.readline.strip
				if line.empty?
					fuzzy = false
				elsif line == "# fuzzy"
					fuzzy = true
				elsif line !~ /\A#/
					title, id = line.split("\t=>\t", 2)
					add(title, id, fuzzy, false)
					fuzzy = false
				end
			rescue EOFError
				break
			end
		end
	end
	return self
end

#save(filename_or_io) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/mizuho/id_map.rb', line 82

def save(filename_or_io)
	normal, orphaned = group_and_sort_entries
	output = ""
	output << BANNER
	normal.each do |entry|
		output << "# fuzzy\n" if entry.fuzzy?
		output << "#{entry.title}	=>	#{entry.id}\n"
		output << "\n"
	end
	if !orphaned.empty?
		output << "\n"
		output << "### These sections appear to have been removed. Please check.\n"
		output << "\n"
		orphaned.each do |entry|
			output << "# fuzzy\n" if entry.fuzzy?
			output << "#{entry.title}	=>	#{entry.id}\n"
			output << "\n"
		end
	end
	open_io(filename_or_io, :write) do |f|
		f.write(output)
	end
end

#statsObject



190
191
192
193
194
195
196
197
198
# File 'lib/mizuho/id_map.rb', line 190

def stats
	fuzzy = 0
	orphaned = 0
	@entries.each_value do |entry|
		fuzzy += 1 if entry.fuzzy?
		orphaned += 1 if !entry.associated?
	end
	return { :fuzzy => fuzzy, :orphaned => orphaned }
end

#xassociate(title) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/mizuho/id_map.rb', line 158

def xassociate(title)
	if entry = @entries[title]
		if entry.associated?
			raise AlreadyAssociatedError, "Cannot associate an already associated title (#{title.inspect})"
		else
			entry.associated = true
			id = entry.id
		end
	elsif (moved_entry = find_moved(title)) || (similar_entry = find_similar(title))
		if moved_entry
			puts "moved entry: #{title.inspect} -> #{moved_entry.title.inspect}"
		elsif similar_entry
			puts "similar entry: #{title.inspect} -> #{similar_entry.title.inspect}"
		end
		entry = (moved_entry || similar_entry)
		@entries.delete(entry.title)
		@entries[title] = entry
		entry.title = title
		entry.associated = true
		entry.fuzzy = true if similar_entry
		id = entry.id
	else
		id = create_unique_id(title)
		add(title, id, false, true)
	end
	return id
end