本文整理匯總了Python中fasttext.load_model方法的典型用法代碼示例。如果您正苦於以下問題:Python fasttext.load_model方法的具體用法?Python fasttext.load_model怎麽用?Python fasttext.load_model使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類fasttext
的用法示例。
在下文中一共展示了fasttext.load_model方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: import fasttext [as 別名]
# 或者: from fasttext import load_model [as 別名]
def __init__(self):
resource_package = __name__
yelp_acc_path = 'acc_yelp.bin'
yelp_ppl_path = 'ppl_yelp.binary'
yelp_ref0_path = 'yelp.refs.0'
yelp_ref1_path = 'yelp.refs.1'
yelp_acc_file = pkg_resources.resource_stream(resource_package, yelp_acc_path)
yelp_ppl_file = pkg_resources.resource_stream(resource_package, yelp_ppl_path)
yelp_ref0_file = pkg_resources.resource_stream(resource_package, yelp_ref0_path)
yelp_ref1_file = pkg_resources.resource_stream(resource_package, yelp_ref1_path)
self.yelp_ref = []
with open(yelp_ref0_file.name, 'r') as fin:
self.yelp_ref.append(fin.readlines())
with open(yelp_ref1_file.name, 'r') as fin:
self.yelp_ref.append(fin.readlines())
self.classifier_yelp = fasttext.load_model(yelp_acc_file.name)
self.yelp_ppl_model = kenlm.Model(yelp_ppl_file.name)
示例2: cache
# 需要導入模塊: import fasttext [as 別名]
# 或者: from fasttext import load_model [as 別名]
def cache(self, name, cache, url=None):
path = os.path.join(cache, name)
if not os.path.isfile(path) and url:
logger.info('Downloading vectors from {}'.format(url))
if not os.path.exists(cache):
os.makedirs(cache)
if not os.path.isfile(self.destination):
if 'drive.google.com' in url:
download_from_url(url, self.destination)
else:
urlretrieve(url, self.destination)
logger.info('Extracting vectors into {}'.format(cache))
ext = os.path.splitext(self.destination)[1][1:]
if ext == 'zip':
with zipfile.ZipFile(self.destination, "r") as zf:
zf.extractall(cache)
elif ext == 'gz':
with tarfile.open(self.destination, 'r:gz') as tar:
tar.extractall(path=cache)
if not os.path.isfile(path):
raise RuntimeError('no vectors found at {}'.format(path))
self.model = fasttext.load_model(path)
self.dim = len(self['a'])
示例3: load
# 需要導入模塊: import fasttext [as 別名]
# 或者: from fasttext import load_model [as 別名]
def load(cls, load_dir, batch_size=4, gpu=False):
import fasttext
if os.path.isfile(load_dir):
return cls(model=fasttext.load_model(load_dir))
else:
logger.error(f"Fasttext model file does not exist at: {load_dir}")
示例4: get_compiled_model
# 需要導入模塊: import fasttext [as 別名]
# 或者: from fasttext import load_model [as 別名]
def get_compiled_model(self):
return load_fasttext_model(self.MODEL_PATH)
示例5: load
# 需要導入模塊: import fasttext [as 別名]
# 或者: from fasttext import load_model [as 別名]
def load(self):
if os.path.exists(self.model_path):
return fasttext.load_model(self.model_path)
else:
return None
示例6: load
# 需要導入模塊: import fasttext [as 別名]
# 或者: from fasttext import load_model [as 別名]
def load(self, model_path):
"""
加載訓練好的模型
:param model_path: 訓練好的模型路徑
:return:
"""
if os.path.exists(self.model_path + '.bin'):
return fasttext.load_model(model_path + '.bin')
else:
return None
示例7: __init__
# 需要導入模塊: import fasttext [as 別名]
# 或者: from fasttext import load_model [as 別名]
def __init__(self, vecs_txt_fname, vecs_bin_fname=None, method="word2vec", dim=100, tokenizer_name="mecab"):
self.tokenizer = get_tokenizer(tokenizer_name)
self.tokenizer_name = tokenizer_name
self.dim = dim
self.method = method
self.dictionary, self.words, self.vecs = self.load_vectors(vecs_txt_fname, method)
if "fasttext" in method:
self.model = load_ft_model(vecs_bin_fname)
示例8: fasttext
# 需要導入模塊: import fasttext [as 別名]
# 或者: from fasttext import load_model [as 別名]
def fasttext(self):
if self._fasttext is None and self.fasttext_model_file:
LOG.info("Loading fasttext embeddings from %s", self.fasttext_model_file)
self._fasttext = fasttext.load_model(self.fasttext_model_file)
return self._fasttext
示例9: __init__
# 需要導入模塊: import fasttext [as 別名]
# 或者: from fasttext import load_model [as 別名]
def __init__(self, method='fasttext', filename=''):
self.method = method
self.filename = filename
self.model = None
# If a filename is given, try to load the model
if os.path.exists(self.filename):
self.load_model()
示例10: load_model
# 需要導入模塊: import fasttext [as 別名]
# 或者: from fasttext import load_model [as 別名]
def load_model(self, filename=''):
# Define path to the feature extractor model filename
if (len(filename) > 0) and os.path.exists(filename):
self.filename = filename
if not os.path.exists(self.filename):
print('Feature file %s does not exist' % self.filename)
return -1
print('Loading model %s' % self.filename)
if self.method == 'fasttext':
self.model = fasttext.load_model(self.filename)
elif self.method == 'bow':
self.model = joblib.load(self.filename)
示例11: load
# 需要導入模塊: import fasttext [as 別名]
# 或者: from fasttext import load_model [as 別名]
def load(self) -> None:
"""
Load fastText binary model from self.load_path
"""
log.info(f"[loading fastText embeddings from `{self.load_path}`]")
self.model = fasttext.load_model(str(self.load_path))
self.dim = self.model.get_dimension()
示例12: __init__
# 需要導入模塊: import fasttext [as 別名]
# 或者: from fasttext import load_model [as 別名]
def __init__(self, model_file: str=None) -> None:
super().__init__()
# pip install fasttext
import fasttext
try:
self.model = fasttext.load_model(model_file)
except ValueError:
raise Exception("Please specify a valid trained FastText model file (.bin or .ftz extension)'{}'."
.format(model_file))
示例13: __init__
# 需要導入模塊: import fasttext [as 別名]
# 或者: from fasttext import load_model [as 別名]
def __init__(self, path_to_model: str) -> None:
"Input fastText trained sentiment model"
import fasttext
self.classifier = fasttext.load_model(path_to_model)
示例14: read_embedding_df_fasttext_format
# 需要導入模塊: import fasttext [as 別名]
# 或者: from fasttext import load_model [as 別名]
def read_embedding_df_fasttext_format(filepath):
"""Read embedding from fasttext format."""
model = load_fasttext(filepath)
return pd.DataFrame({
word: model.get_word_vector(word)
for word in model.get_words()
}).T
示例15: load
# 需要導入模塊: import fasttext [as 別名]
# 或者: from fasttext import load_model [as 別名]
def load(self, embedding_fname, embedding_url=None, *args, **kwargs):
"""
Method initializes dict of embeddings from file
Args:
fname: file name
Returns:
Nothing
"""
if not embedding_fname:
raise RuntimeError('Please, provide path to model')
fasttext_model_file = embedding_fname
if not Path(fasttext_model_file).is_file():
emb_path = embedding_url
if not emb_path:
raise RuntimeError('Fasttext model file does not exist locally. URL does not contain fasttext model file')
embedding_fname = Path(fasttext_model_file).name
try:
download(dest_file_path=fasttext_model_file, source_url=embedding_url)
except Exception as e:
raise RuntimeError('Looks like the `EMBEDDINGS_URL` variable is set incorrectly', e)
if self.module == "fastText":
import fastText
self.fasttext_model = fastText.load_model(fasttext_model_file)
if self.module == "fasttext":
import fasttext
self.fasttext_model = fasttext.load_model(fasttext_model_file)
return