[NLP]夏のコースワーク3 - POSタギング(簡単な単語頻度確率と統計)

ジョブのタスク:

トレーニングとテストをタグ付けスピーチを使用して98人民日報コーパス。

ジョブエントリ:

データ検証セットの20%トレーニングセットとしてデータの80%を毎日コーパス98(1998-01-105-有声の.txt)、、。

動作環境:

Jupyterノートブック、のpython3

操作方法:

予測を行うために音声の単語の一部のために、単語頻度統計の簡単な方法を使用。しかし、N-gram言語規則を使用して。

ジョブステップ:

1.取扱いコーパス:フロントラベルセグメントを削除します。

# 读取原始语料文件
in_path = '1998-01-105-带音.txt'
file = open(in_path, encoding='gbk')
in_data = file.readlines()
# 预处理后的语料库
curpus_path = 'curpus.txt'
curpusfile = open(curpus_path, 'w', encoding='utf-8')
#删除段前标号,[],{}
for sentence in in_data:
    words = sentence.strip().split(' ')
    words.pop(0)
    
    for word in words:
        if word.strip() != '':
            if word.startswith('['):
                word = word[1:]
            elif ']' in word:
                word = word[0:word.index(']')]

            w_c = word.split('/')
            # 生成语料库
            if(len(w_c) > 1):
                curpusfile.write(w_c[0] + ' ' + w_c[1] + '\n')

20%の訓練セットと検証セットのランダム分割の2 80%。

from sklearn.model_selection import train_test_split

# 随机划分
curpus = open(curpus_path, encoding='utf-8').readlines()
train_data, test_data = train_test_split(
    curpus, test_size=0.2, random_state=10)
# 查看划分后的数据大小
print(len(curpus))
print(len(train_data) / len(curpus))
print(len(test_data) / len(curpus))
1114419
0.7999998205342874
0.20000017946571264

3.トレーニングセットの単語頻度統計が。

# 生成词频记录文件
from tqdm import tqdm_notebook

doc = []

for sentence in tqdm_notebook(train_data):
    words = sentence.strip().split(' ')
    if len(words) > 1:
        temp = []
        temp.append(words[0])
        temp.append(words[1])
        flag = False
        for line in doc:
            if line[0] == temp[0] and line[1] == temp[1]:
                line[2] += 1
                flag = True
                break
        if not flag:
            temp.append(1)
            doc.append(temp)

4.音声の最大確率を選択します。

# 保存验证集
test_path = 'test.txt'
testfile = open(test_path, 'w', encoding='utf-8')
for sentence in test_data:
    words = sentence.strip().split(' ')
    if len(words) > 1:
        testfile.write(sentence)
# 保存标注结果
result_path = 'result.txt'
resultfile = open(result_path, 'w', encoding='utf-8')
# 选择概率最大的词性进行标注
for sentence in tqdm_notebook(test_data):
    words = sentence.strip().split(' ')
    if len(words) > 1:
        words[1] = 'n'
        max = 0
        for line in doc:
            if line[0] == words[0] and line[2] > max:
                max = line[2]
                words[1] = line[1]
        resultfile.write(words[0] + ' ' + word[1] + '\n')

性能評価:精度

def get_word(path):
    f = open(path, 'r', encoding='utf-8')
    lines = f.readlines()
    return lines

result_lines = get_word(result_path)
test_lines = get_word(test_path)

list_num = len(test_lines)
right_num = 0

for i in range(0, list_num):
    if result_lines[i][1] == test_lines[i][1]:
        right_num += 1

print("准确率为:", right_num / list_num)
准确率为: 0.23189316857201872

おすすめ

転載: www.cnblogs.com/yanqiang/p/11259468.html