Skip to content

Instantly share code, notes, and snippets.

import tensorflow as tf
import os
#path that contains all 3 ckpt files of your fine-tuned model
path = './bert_output'
#path to output the new optimized model
output_path = os.path.join(path, 'optimized_model')
sess = tf.Session()
from . import run_classifier
import os
import tensorflow as tf
import json
def getListPrediction(in_sentences):
#1
input_examples = [run_classifier.InputExample(guid="", text_a = x, text_b = None, label = "0") for x in in_sentences] # here, "" is just a dummy label
#2
tokenization = run_classifier.tokenization
base_path = "C:/Users/wfng/path/to/optimized_model" #modify accordingly
init_checkpoint = os.path.join(base_path, 'model.ckpt')
bert_config_file = os.path.join(base_path, 'bert_config.json')
vocab_file = os.path.join(base_path, 'vocab.txt')
processor = run_classifier.ColaProcessor()
label_list = processor.get_labels()
#since the original bert source code combines train, eval and predict in one single configuration,
#we need to feed such data during initialization, can be anything as it is needed for run configuration
from snownlp import SnowNLP
s = SnowNLP(u'今天是周六。')
from snownlp import SnowNLP
s = SnowNLP(u'我喜欢看电影。')
s.words
from snownlp import SnowNLP
s = SnowNLP(u'我喜欢看电影。')
list(s.tags)
from snownlp import SnowNLP
s = SnowNLP(u'我喜欢看电影。')
s.pinyin
from snownlp import SnowNLP
s = SnowNLP(u'這傢伙是壞人。')
s.han
from snownlp import SnowNLP
s = SnowNLP(u'在茂密的大森林里,一只饥饿的老虎逮住了一只狐狸。老虎张开大嘴就要把狐狸吃掉。"慢着"!狐狸虽然很害怕但还是装出一副很神气的样子说,"你知道我是谁吗?我可是玉皇大帝派来管理百兽的兽王,你要是吃了我,玉皇大帝是决不会放过你的"。')
s.sentences
from snownlp import SnowNLP
s = SnowNLP(u'在茂密的大森林里,一只饥饿的老虎逮住了一只狐狸。老虎张开大嘴就要把狐狸吃掉。"慢着"!狐狸虽然很害怕但还是装出一副很神气的样子说,"你知道我是谁吗?我可是玉皇大帝派来管理百兽的兽王,你要是吃了我,玉皇大帝是决不会放过你的"。')
s.keywords(5)