Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a155d62
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
require 'mongo_mapper' | ||
|
||
MongoMapper.database = 'testing' | ||
|
||
class Lemma | ||
include MongoMapper::Document | ||
key :lemma, String | ||
key :translation_ids, Array | ||
many :translations, :in => :translation_ids | ||
timestamps! | ||
end | ||
|
||
class Translation | ||
include MongoMapper::Document | ||
key :source, String | ||
key :target, String | ||
timestamps! | ||
end | ||
|
||
Lemma.collection.remove | ||
Translation.collection.remove | ||
Lemma.ensure_index(:lemma) | ||
|
||
error = {} | ||
Dir.glob('../farhang/*.txt').each do |ff| | ||
File.open(ff, 'r') do |f| | ||
lemma = nil | ||
f.each_line do |l| | ||
source, target = l.split(';') | ||
begin | ||
source.strip! | ||
target.strip! | ||
rescue | ||
error["#{ff.basename}: #{f.lineno}"] = l | ||
end | ||
unless source.start_with?('- ') | ||
lemma = Lemma.create( :lemma => source ) | ||
trans = Translation.create( :source => source, :target => target ) | ||
lemma.translations << trans | ||
lemma.save | ||
else | ||
source.sub!('- ', '') | ||
trans = Translation.create( :source => source, :target => target ) | ||
lemma.translations << trans | ||
lemma.save | ||
end | ||
end | ||
end | ||
end | ||
|
||
p error.to_s |