added append method which reads and adds to an existing model file
This commit is contained in:
parent
d16ee04d5a
commit
43491cb668
1 changed files with 29 additions and 0 deletions
|
@ -69,6 +69,35 @@ module Ebooks
|
||||||
self
|
self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Append a generated model to existing model file instead of overwriting it
|
||||||
|
# @param path [String]
|
||||||
|
def append(path)
|
||||||
|
existing = File.file?(path)
|
||||||
|
if !existing
|
||||||
|
log "No existing model found at #{path}"
|
||||||
|
return
|
||||||
|
else
|
||||||
|
#read-in and deserialize existing model
|
||||||
|
props = Marshal.load(File.open(path,'rb') { |old| old.read })
|
||||||
|
old_tokens = props[:tokens]
|
||||||
|
old_sentences = props[:sentences]
|
||||||
|
old_mentions = props[:mentions]
|
||||||
|
old_keywords = props[:keywords]
|
||||||
|
|
||||||
|
#append existing properties to new ones and overwrite with new model
|
||||||
|
File.open(path, 'wb') do |f|
|
||||||
|
f.write(Marshal.dump({
|
||||||
|
tokens: @tokens.concat(old_tokens),
|
||||||
|
sentences: @sentences.concat(old_sentences),
|
||||||
|
mentions: @mentions.concat(old_mentions),
|
||||||
|
keywords: @keywords.concat(old_keywords)
|
||||||
|
}))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
self
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
@tokens = []
|
@tokens = []
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue