diff --git a/dict2Trie.py b/dict2Trie.py index 2af6460..c46883e 100644 --- a/dict2Trie.py +++ b/dict2Trie.py @@ -1,46 +1,117 @@ #conding:utf-8 import os -import click +import random +import json import pickle -from collections import defaultdict +import click + + -type_dict ={"地名":"LOC","人名":"PER","机构名称":"ORG"} -word_dict = {} -word_first_char = defaultdict(list) -Trie = {} +TYPE_DICT = {"地名":"LOC", "人名":"PER", "机构名称":"ORG"} +TYPE_NUM_LIMIT = {"LOC":1000000, "PER":1000000, "ORG":1000000} +WORD_DICT = {} + +''' +get ner from word vocabulary directories +''' @click.command() -@click.option('--dict_dir',default="词库") +@click.option('--dict_dir', default="词库") def get_dict(dict_dir): - Tries = Trie() + trie = Trie() for dir_name in os.listdir(dict_dir): print (dir_name) for file_name in os.listdir(os.path.join(dict_dir,dir_name)): - with open(os.path.join(dict_dir,dir_name,file_name),'r',encoding='utf-8') as fn: + with open(os.path.join(dict_dir, dir_name, file_name), 'r', encoding='utf-8') as fn: for line in fn: text = line.strip() if text == '' : continue - Tries.add(type_dict[dir_name],text) - Tries.add("rev_"+type_dict[dir_name],text[::-1]) - pickle.dump(Trie.root,open("Trie.data",'wb')) + trie.add(TYPE_DICT[dir_name], text) + trie.add("rev_"+TYPE_DICT[dir_name], text[::-1]) + pickle.dump(trie.root, open("Trie.data",'wb')) +''' +get ner from jsonl file +''' +@click.command() +@click.option('--dict_dir',default="../data") +def get_dict_from_json(dict_dir): + trie = Trie() + for file_name in os.listdir(dict_dir): + if '.jsonl' in file_name: + print (file_name) + with open(os.path.join(dict_dir,file_name), 'r', encoding='utf-8') as fn: + for line in fn: + content = json.loads(line.strip()) + word = content["word"].strip() + label = content["label"].strip() + if word == '': + continue + trie.add(TYPE_DICT[label], word) + trie.add('rev_'+TYPE_DICT[label], word[::1]) + + pickle.dump(trie.root, open("Trie.data",'wb')) + + +def into_trie(trie, word_list, Type, span): + i = span[0] + while i < span[1]: + trie.add(Type, word_list[i]) + trie.add("rev_"+Type, word_list[i][::1]) + i+=1 +''' +seperate vocabulary for train and test set +''' +@click.command() +@click.option('--dict_dir', default="../data") +def train_and_test(dict_dir): + trie1 = Trie() + trie2 = Trie() + type2word_list = {"PER":set(), "ORG":set(), "LOC":set()} + for file_name in os.listdir(dict_dir): + if '.jsonl' in file_name: + print(file_name) + with open(os.path.join(dict_dir,file_name), 'r', encoding='utf-8') as fn: + for line in fn: + content = json.loads(line.strip()) + word = content['word'].strip() + label = content["label"] + type2word_list[TYPE_DICT[label]].add(word) + for Type in ["PER", "LOC", "ORG"]: + print("processing "+Type) + word_list = list(type2word_list[Type]) + length = len(word_list) + length = length if length <= TYPE_NUM_LIMIT[Type] else TYPE_NUM_LIMIT[Type] + print(Type, length) + into_trie(trie1,word_list, Type,[0, int(length/2)]) + into_trie(trie2,word_list, Type,[0, length]) + type2word_list[Type] = [] + print("finally1") + pickle.dump(trie1.root,open("../data/trie4train.data",'wb')) + print("finally2") + pickle.dump(trie2.root,open("../data/trie4test.data",'wb')) + print("finally3") + return + + class Trie: - root = {"LOC":{},"PER":{},"ORG":{},"rev_LOC":{},"rev_PER":{},"rev_ORG":{}} - END = '/-/-/-' + def __init__(self): + self.root = {"LOC":{}, "PER":{}, "ORG":{}, "rev_LOC":{}, "rev_PER":{}, "rev_ORG":{}} + self.END = '/-/-' def add(self,ner_type,text): node =self.root[ner_type] - if( len(text) < 2): + if(len(text) < 2): text = "" for word in text: - node.setdefault(word,{}) + node.setdefault(word, {}) node = node[word] node[self.END] = None @@ -51,7 +122,7 @@ class Trie: if __name__ == '__main__': - get_dict() + train_and_test()