B
basi
Hello,
I have a word frequency table, thus:
tgl_tokens = db.create_table(
:tgltokens,
:tgltoken, :String,
:count, :Integer)
With data from a hash, I'm trying to insert a record into the table,
but only if the key does not exist in the table. If it does, then add
the new frequency count into the stored frequency count.
tgl_texts.select.each do |t|
freq = Hash.new(0)
for word in t.text.to_s.downcase.tr_s('^A-Za-z-',' ').split(' ')
freq[word] += 1
end
freq.delete("")
freq.each do |w,c|
-- if w exists in tgl_tokens, then
-- add c to the stored count value of w
-- else
tgl_tokens.insert do |token|
token.tgltoken = w
token.count = c
end
-- end
end
end
Basically the KirbyBase / Ruby syntax that eludes me corresponds to
this SQL snippet:
begin
select 1 into v_dummy
from tgltokens
where tgltoken = w;
-- if found then
update tgltokens
set count = count + c
where tgltoken = w;
exception
when no_data_found then
insert into tgltokens values (w, c);
end
Thanks!
basi
I have a word frequency table, thus:
tgl_tokens = db.create_table(
:tgltokens,
:tgltoken, :String,
:count, :Integer)
With data from a hash, I'm trying to insert a record into the table,
but only if the key does not exist in the table. If it does, then add
the new frequency count into the stored frequency count.
tgl_texts.select.each do |t|
freq = Hash.new(0)
for word in t.text.to_s.downcase.tr_s('^A-Za-z-',' ').split(' ')
freq[word] += 1
end
freq.delete("")
freq.each do |w,c|
-- if w exists in tgl_tokens, then
-- add c to the stored count value of w
-- else
tgl_tokens.insert do |token|
token.tgltoken = w
token.count = c
end
-- end
end
end
Basically the KirbyBase / Ruby syntax that eludes me corresponds to
this SQL snippet:
begin
select 1 into v_dummy
from tgltokens
where tgltoken = w;
-- if found then
update tgltokens
set count = count + c
where tgltoken = w;
exception
when no_data_found then
insert into tgltokens values (w, c);
end
Thanks!
basi