Hive 区块链文章句子记录(一)

Words
358
Reading
2 min
Listen
Play
4y

对用户张贴的文章,先将其分解成一条条句子,比如说下面一篇短文章:

We split an article into sentences. For each sentence, we calculate its sha256 hash value. Then we broadcast the sentences to a blockchain along with its sha256 hash and some meta info. By doing this, we can record the contributions of each author to the article in sentence level.

将其分解成下面若干条句子:

[
'We split an article into sentences.',
'For each sentence, we calculate its sha256 hash value.',
'Then we broadcast the sentences to a blockchain along with its sha256 hash and some meta info.',
'By doing this, we can record the contributions of each author to the article in sentence level.'
]

计算所分解的每条句子的 Hash256 值:

[
('14629c1c5aa59aa1725ef9fbfe18ed3eb1e0cc989a1520a2fc6d350dae08e781', 'We split an article into sentences.'),
('298d8755b5655bb419fe25aaf86dbd1b27ca2d25b6cc12be0d6a1a9c017371af', 'For each sentence, we calculate its sha256 hash value.'),
('8e9d1c9b459234f6ff2c5d34a3811fae96446be76256288f8d5120e918151025', 'Then we broadcast the sentences to a blockchain along with its sha256 hash and some meta info.'),
('2ea1b066f4f51bea05aea3d0db6a3a9ab0e4e06160d65951e214b8741bf9f382', 'By doing this, we can record the contributions of each author to the article in sentence level.')
]

然后将这些句子及其 Hash256 值记录到区块链上(同时会记录句子的所有者和句子的提交时间),如下:

{'type': 'custom_json_operation', 'value': {'required_auths': [], 'required_posting_auths': ['zikele'], 'id': 'sentence_post', 'json': '{"time":"2022-10-21T12:34:03.394347","author":"zikele","hash":"14629c1c5aa59aa1725ef9fbfe18ed3eb1e0cc989a1520a2fc6d350dae08e781","sentence":"We split an article into sentences."}'}}
{'type': 'custom_json_operation', 'value': {'required_auths': [], 'required_posting_auths': ['zikele'], 'id': 'sentence_post', 'json': '{"time":"2022-10-21T12:34:26.767014","author":"zikele","hash":"dbafd6a74295ce7a2bdceb16a8ce4d9c8c90273212e7a6d41b84ca6c212e8ea7","sentence":"For each sentence, we calculte its sha256 hash value."}'}}
{'type': 'custom_json_operation', 'value': {'required_auths': [], 'required_posting_auths': ['zikele'], 'id': 'sentence_post', 'json': '{"time":"2022-10-21T12:34:45.532803","author":"zikele","hash":"8e9d1c9b459234f6ff2c5d34a3811fae96446be76256288f8d5120e918151025","sentence":"Then we broadcast the sentences to a blockchain along with its sha256 hash and some meta info."}'}}
{'type': 'custom_json_operation', 'value': {'required_auths': [], 'required_posting_auths': ['zikele'], 'id': 'sentence_post', 'json': '{"time":"2022-10-21T12:35:06.198640","author":"zikele","hash":"16cbcb572fdf0d73edea310bf51bb1077cb0e58f3093ad1de26f90ad53ef5ea9","sentence":"By Doing this, we can record the contributions of each author to the article in sentence level."}'}}

下面给出用 Python beem 实现的代码:

import hashlib
from datetime import datetime
from nltk.tokenize import sent_tokenize
from beem.transactionbuilder import TransactionBuilder
from beembase.operations import Custom_json
from beem import Hive
from beem.blockchain import Blockchain
from helper import wif, name
from beem.nodelist import NodeList


nodelist = NodeList()
nodelist.update_nodes()
nodes = nodelist.get_hive_nodes()

nobroadcast = False

hive = Hive(node=nodes, nobroadcast=nobroadcast)
blockchain = Blockchain(hive)
current_block_num = blockchain.get_current_block_num()

text = 'We split an article into sentences. For each sentence, we calculate its sha256 hash value. Then we broadcast the sentences to a blockchain along with its sha256 hash and some meta info. By doing this, we can record the contributions of each author to the article in sentence level.'
sentences = sent_tokenize(text)

for st in sentences:
    st_hash = hashlib.sha256(st.encode()).hexdigest()

    data = {
        'time': datetime.utcnow().isoformat(),
        'author': name,
        'hash': st_hash,
        'sentence': st
    }

    cj = {
        "required_auths": [],
        "required_posting_auths": [name],
        "id": "sentence_post",
        "json": data
    }

    tx = TransactionBuilder(blockchain_instance=hive)
    tx.appendOps(Custom_json(cj))
    tx.appendWif(wif)
    signed_tx = tx.sign()
    broadcast_tx = tx.broadcast()

Hive 区块链文章句子记录(一) | Ecency