Static Analyzer for Solidity
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
slither/utils/similarity/train.py

55 lines
1.8 KiB

import argparse
import logging
import sys
import traceback
import operator
6 years ago
import os
from fastText import train_unsupervised
6 years ago
from .encode import encode_contract, load_contracts
from .cache import save_cache
6 years ago
logger = logging.getLogger("Slither-simil")
def train(args):
try:
last_data_train_filename = "last_data_train.txt"
model_filename = args.model
dirname = args.input
nsamples = args.nsamples
if dirname is None:
6 years ago
logger.error('The train mode requires the input parameter.')
sys.exit(-1)
contracts = load_contracts(dirname, **vars(args))
6 years ago
logger.info('Saving extracted data into %s', last_data_train_filename)
6 years ago
cache = []
with open(last_data_train_filename, 'w') as f:
6 years ago
for filename in contracts:
#cache[filename] = dict()
for (filename, contract, function), ir in encode_contract(filename, **vars(args)).items():
if ir != []:
6 years ago
x = " ".join(ir)
f.write(x+"\n")
cache.append((os.path.split(filename)[-1], contract, function, x))
logger.info('Starting training')
model = train_unsupervised(input=last_data_train_filename, model='skipgram')
6 years ago
logger.info('Training complete')
6 years ago
logger.info('Saving model')
model.save_model(model_filename)
6 years ago
for i,(filename, contract, function, irs) in enumerate(cache):
cache[i] = ((filename, contract, function), model.get_sentence_vector(irs))
6 years ago
logger.info('Saving cache in cache.npz')
6 years ago
save_cache(cache, "cache.npz")
6 years ago
logger.info('Done!')
except Exception:
logger.error('Error in %s' % args.filename)
logger.error(traceback.format_exc())
sys.exit(-1)