def lookup(self, transaction): #check originality for prev_block in self.chain: if transaction['genre'] == prev_block.transaction['genre']: try: if transaction['genre'] == 'Audio': score = ac.calc_accuracy('./tmp/' + transaction['media'], './uploads/' + prev_block.transaction['media']) print(score) if score > 0.9: return prev_block if transaction['genre'] == 'Text': score = tc.check_text_similarity('./tmp/' + transaction['media'], './uploads/'+prev_block.transaction['media']) print(score) if score < 100: return prev_block if transaction['genre'] == "Image": score = ic.calc_accuracy('./tmp/' + transaction['media'], './uploads/' + prev_block.transaction['media']) print(score) if score < 0.4: return prev_block except: print("exception") return prev_block return None
时间: 2024-02-10 08:34:12 浏览: 19
这段代码看起来像是一个区块链节点中的交易查重函数,可以判断交易是否重复,并返回已存在的交易(即已经被记录在区块链中的交易)。其中,交易的类型有音频、文本和图片,分别使用了不同的计算方法进行比较。对于音频和图片,使用了ac.calc_accuracy和ic.calc_accuracy计算相似度,而对于文本,则使用了tc.check_text_similarity计算相似度。如果相似度高于一定阈值,则认为交易重复,返回已存在的交易。如果没有找到已存在的交易,则返回None。
相关问题
优化sql:SELECT we.organization_id ,we.wip_entity_id ,case when wl.line_id is null then we.wip_entity_name else '' end wip_entity_name ,we.primary_item_id ,mtt.transaction_type_name ,mmt.transaction_date ,bd.department_code ,mmt.inventory_item_id ,mmt.subinventory_code ,mta.reference_account ,br.resource_code ,lu2.meaning as line_type_name ,mta.base_transaction_value ,mta.cost_element_id ,flv.meaning as cost_element ,wdj.class_code job_type_code ,ml.meaning job_type_name FROM (select * from gerp.mtl_material_transactions where substr(transaction_date,1,7) >= '2023-06' and transaction_source_type_id = 5) mmt inner join gerp.wip_entities we on mmt.organization_id = we.organization_id inner join gerp.mtl_transaction_accounts mta on mta.transaction_source_id = we.wip_entity_id and mta.transaction_id = mmt.transaction_id and mta.transaction_source_type_id = 5 inner join gerp.mtl_transaction_types mtt on mtt.transaction_type_id = mmt.transaction_type_id inner join mfg_lookups lu2 on lu2.lookup_code = mta.accounting_line_type and lu2.lookup_type = 'CST_ACCOUNTING_LINE_TYPE' inner join gerp.mtl_system_items_b msi on msi.inventory_item_id = mmt.inventory_item_id and msi.organization_id = mta.organization_id left join gerp.bom_departments bd on bd.department_id = mmt.department_id left join gerp.bom_resources br on br.resource_id = mta.resource_id left join gerp.wip_lines wl on wl.line_id = mmt.repetitive_line_id left join gerp.wip_discrete_jobs wdj on wdj.wip_entity_id = mta.transaction_source_id left join gerp.fnd_lookup_values_vl flv on cast(mta.cost_element_id as string) = flv.lookup_code and flv.lookup_type = 'CST_COST_CODE_TYPE' left join mfg_lookups ml on ml.lookup_code = wdj.job_type and ml.lookup_type = 'WIP_DISCRETE_JOB' 。其中mmt,we,mta,msi,wdj数据量很大
针对这个SQL语句,可以优化的地方有:
1. 使用JOIN语句时,可以根据数据量大小,将数据量大的表放在后面,这样可以避免在连接时出现性能问题。
2. 使用子查询时,可以在子查询中添加条件筛选,减少返回的数据量。
3. 可以对查询中的日期条件进行优化,例如使用日期范围查询时,可以使用BETWEEN关键字代替大于等于和小于等于的运算符。
4. 可以对大表进行分区或分片,以加快查询速度。
5. 可以对JOIN语句中的字段添加索引,以提高查询效率。
下面是对原SQL语句的优化:
```
SELECT
we.organization_id,
we.wip_entity_id,
CASE WHEN wl.line_id is null THEN we.wip_entity_name ELSE '' END wip_entity_name,
we.primary_item_id,
mtt.transaction_type_name,
mmt.transaction_date,
bd.department_code,
mmt.inventory_item_id,
mmt.subinventory_code,
mta.reference_account,
br.resource_code,
lu2.meaning as line_type_name,
mta.base_transaction_value,
mta.cost_element_id,
flv.meaning as cost_element,
wdj.class_code job_type_code,
ml.meaning job_type_name
FROM
gerp.wip_entities we
INNER JOIN (
SELECT
*
FROM
gerp.mtl_material_transactions
WHERE
transaction_date BETWEEN '2023-06-01' AND '2023-06-30'
AND transaction_source_type_id = 5
) mmt ON mmt.organization_id = we.organization_id
INNER JOIN gerp.mtl_transaction_accounts mta ON mta.transaction_source_id = we.wip_entity_id
AND mta.transaction_id = mmt.transaction_id
AND mta.transaction_source_type_id = 5
INNER JOIN gerp.mtl_transaction_types mtt ON mtt.transaction_type_id = mmt.transaction_type_id
INNER JOIN mfg_lookups lu2 ON lu2.lookup_code = mta.accounting_line_type AND lu2.lookup_type = 'CST_ACCOUNTING_LINE_TYPE'
INNER JOIN gerp.mtl_system_items_b msi ON msi.inventory_item_id = mmt.inventory_item_id
AND msi.organization_id = mta.organization_id
LEFT JOIN gerp.bom_departments bd ON bd.department_id = mmt.department_id
LEFT JOIN gerp.bom_resources br ON br.resource_id = mta.resource_id
LEFT JOIN gerp.wip_lines wl ON wl.line_id = mmt.repetitive_line_id
LEFT JOIN gerp.wip_discrete_jobs wdj ON wdj.wip_entity_id = mta.transaction_source_id
LEFT JOIN gerp.fnd_lookup_values_vl flv ON cast(mta.cost_element_id as string) = flv.lookup_code
AND flv.lookup_type = 'CST_COST_CODE_TYPE'
LEFT JOIN mfg_lookups ml ON ml.lookup_code = wdj.job_type AND ml.lookup_type = 'WIP_DISCRETE_JOB';
```
在优化后的SQL语句中,将子查询中的日期范围查询放在了WHERE语句中,将数据量较大的表放在了后面,左连接的表也放在了后面。同时,可以根据具体情况对需要添加索引的字段进行索引优化。
def model(self): # 词向量映射 with tf.name_scope("embedding"): input_x = tf.split(self.input_x, self.num_sentences, axis=1) # shape:[None,self.num_sentences,self.sequence_length/num_sentences] input_x = tf.stack(input_x, axis=1) embedding = tf.get_variable("embedding", [self.vocab_size, self.embedding_dim]) # [None,num_sentences,sentence_length,embed_size] embedding_inputs = tf.nn.embedding_lookup(embedding, input_x) # [batch_size*num_sentences,sentence_length,embed_size] sentence_len = int(self.seq_length / self.num_sentences) embedding_inputs_reshaped = tf.reshape(embedding_inputs,shape=[-1, sentence_len, self.embedding_dim])
这段代码是一个模型中的一部分,用于进行词向量映射。首先,将输入的句子进行分割,得到每个句子的词语序列。然后,通过embedding_lookup函数将词语序列转换为词向量。接着,将词向量进行reshape操作,将其变为三维的张量,形状为[batch_size*num_sentences, sentence_length, embed_size]。其中,batch_size代表批次大小,num_sentences代表句子数量,sentence_length代表每个句子的长度,embed_size代表词向量的维度。这样做的目的是为了方便后续的模型处理。