示例#1
0
def test_perceptron(w, input_file, output_file):
    with open(input_file) as f_in, open(output_file, 'w') as f_out:
        for line in f_in:
            sentence = line.rstrip()
            phi = create_features(sentence)
            prediction = predict_one(w, phi)
            print(f'{prediction}\t{sentence}', file=f_out)
示例#2
0
def test_perceptron(w, input_file):
    with open(input_file, 'r', encoding='utf-8') as i_file:
        for line in i_file:
            snetence = line.rstrip()
            phi = create_features(snetence)
            prediction = predict_one(w, phi)
            print(f'{prediction}\t{snetence}')
def predict_all(model_file, input_fle, n):
    w = load_model(model_file)
    result = []
    for sentence in input_fle:
        phi = defaultdict(lambda: 0)
        phi = create_features(sentence.lower(), n, phi)
        result.append(predict_one(w, phi))
    return result
示例#4
0
def predict_all(model_file, input_file):
    with open(model_file, mode='rb') as model_f \
        , open(input_file) as in_f:
        weights = pickle.load(model_f)
        #入力ファイルを一行ずつsplit
        for line in in_f:
            phi = create_features(line)
            label_pred = predict_one(weights, phi)
            print(label_pred)
def predict_all(model_file, input_file):
    with open(model_file, 'r') as m_f, open(input_file, 'r') as i_f:
        w = defaultdict(lambda: 0)
        for line in m_f:
            word, value = line.split('\t')
            w[word] = int(value)
        for x in i_f:
            phi = defaultdict(lambda: 0)
            for k, v in create_features(x).items():
                phi[k] = int(v)
            y_ = predict_one(w, phi)
            yield ('{}\t{}'.format(y_, x))
示例#6
0
def predict_all():
    w = defaultdict(int)
    with open('../../data/titles-en-test.word') as i_f, open(
            'model_file.txt') as m_f:
        for line in m_f:
            words = line.strip().split()
            w[words[0]] = int(words[1])
        for x in i_f:
            phi = create_features(x)
            y_ = predict_one(w, phi)
            printline = (str(y_) + '\t' + str(x)).strip('\n')
            print(printline)
def pred_all(model_file, input_file):

    phi = defaultdict(lambda : 0)
    with open(model_file) as modelf:
        w = defaultdict(lambda: 0)
        for model_line in modelf:
            name, weight = model_line.split('\t')
            w[name] = int(weight)

    with open(input_file) as inputf, open(ansfile, 'w') as ansout:
        for in_line in inputf:
            l = in_line
            phi = create_features(l.lower())
            y_pred = int(predict_one(w,phi))
            print('{}\t{}'.format(y_pred, l), end='', file=ansout)
示例#8
0
    def test_create_features(self):

        ansdict = {
            'UNI:A': 1,
            'UNI:site': 1,
            'UNI:,': 2,
            'UNI:Site': 1,
            'UNI:in': 1,
            'UNI:Maizuru': 1,
            'UNI:Kyoto': 1
        }
        # Create features
        outdict = create_features('A site , Site in Maizuru , Kyoto')

        self.assertTrue(ansdict == outdict)
def test_perceptron(test_file, model_file, output_file):
    '''パーセプトロンを用いた分類器テスト'''

    # モデルの読み込み
    w = defaultdict(float)
    for line in open(model_file, encoding='utf8'):
        key, raw_value = line.strip().split('\t')
        w[key] = float(raw_value)

    # テスト
    with open(output_file, 'w', encoding='utf8') as f:
        for line in open(test_file, encoding='utf8'):
            raw_sentence = line.strip()
            phi = create_features(raw_sentence)
            prediction = predict_one(w, phi)
            f.write(f'{prediction}\t{raw_sentence}\n')
def test_perceptron(model_file, test_file, output_file):
    """Predict all on the test file with a trained model.
    (Described in Neubig's slides p.12.)
    """
    w = load_model(model_file)

    # Write the result into a buffer.
    out = io.StringIO()

    with open(test_file, 'r') as f:
        for x in f:
            phi = train_perceptron.create_features(x)
            y_prime = train_perceptron.predict_one(w, phi)
            out.write('{}\t{}\n'.format(y_prime, x.strip()))

    # Print on the screen or save in the file.
    if output_file == 'stdout':
        print(out.getvalue().strip())
    else:
        with open(output_file, 'w') as f:
            f.write(out.getvalue().strip())
示例#11
0
# 重みを読み込み、予測を1行ずつ出力
from train_perceptron import create_features
from train_perceptron import predict_one

w = {} # word : weight
with open('model','r') as model:
    for line in model:
        columm= line.strip().split('\t')
        w[columm[0]] =  int(columm[1])

test_path = '../../data/titles-en-test.word' # テスト用
# test_path = 'test.txt'

with open(test_path,'r') as test_file:
    for line in test_file:
        line = line.rstrip()
        phi = create_features(str(line)) 
        predicted_label = predict_one(w, phi) # sign(w * phi)を計算
        print(predicted_label)


# ../../script/grade-prediction.py data/titles-en-test.labeled result

        
示例#12
0
from train_perceptron import create_features, predict_one
from collections import defaultdict

W = defaultdict(int)
for line in open('train_result.txt', 'r'):
    key, value = line.split('\t')
    W[key] = int(value)

for x in open('../../data/titles-en-test.word', 'r'):
    phi = create_features(x)
    y_prime = predict_one(W, phi)
    print(y_prime)
示例#13
0
import sys
from collections import defaultdict
from train_perceptron import create_features, predict_one

with open(sys.argv[1],
          "r") as model_file, open(sys.argv[2], "r") as input_file, open(
              "test_result.txt", "w") as fout:
    w = defaultdict(int)
    for line in model_file:
        splited_line = line.strip().split("\t")
        w[splited_line[0]] = int(splited_line[1])
    for x in input_file:
        phi = create_features(x.strip())
        y_2 = predict_one(w, phi)
        fout.write(f"{y_2}\n")

# Accuracy = 80.517180%