Comments (3)
供参考
#!/usr/bin/env/ python
# -*- coding:utf-8 -*-
import numpy as np
import os
class GlobeTransformer:
def __init__(self, globe_filename, corpus_filename):
self.globe_array = {}
self.generate_globe_array(globe_filename)
self.word2id = {}
self.generate_corpus_word2vec(corpus_filename)
@staticmethod
def read_file(filename):
data = []
with open(filename, 'r', encoding='utf-8') as reader:
lines = reader.readlines()
for l in lines:
data.append(l.strip('\n'))
return data
def generate_globe_array(self, globe_filename):
for word_info in GlobeTransformer.read_file(globe_filename)[1:]:
array_info = word_info.split(' ')
word = array_info[0]
vec = array_info[1:]
if 'name' in vec[1]:
print(word_info.split(' ')[:4])
self.globe_array[word] = vec
def generate_corpus_word2vec(self, corpus_filename):
for word_info in GlobeTransformer.read_file(corpus_filename):
word = ' '.join(word_info.split()[:-1])
word_id = int(word_info.split()[-1])
self.word2id[word] = word_id
def save_corpus_emb(self, emb_filename):
def rand_emb():
return np.random.random_sample(300).tolist()
emb_array = ['']*len(self.word2id)
emb_size = 300
for word, word_id in self.word2id.items():
if word == '<pad>':
emb_array[0] = ' '.join(['0']*emb_size)
continue
if word in self.globe_array:
emb_array[word_id] = ' '.join(self.globe_array[word])
else:
emb_array[word_id] = ' '.join(list(map(str, rand_emb())))
print(len(emb_array))
GlobeTransformer.write_emb_file(emb_array, emb_filename)
@staticmethod
def write_emb_file(emb_array, filename):
with open(filename, 'w', encoding='utf-8') as writter:
for line in emb_array:
writter.write(line+'\n')
if __name__ == '__main__':
globe = 'glove.840B.300d.txt'
source = 'ACE'
corpus_word2id = os.path.join(source, 'word_class_id.txt')
save_filename = os.path.join(source, 'word_embedding.txt')
GlobeTransformer(globe, corpus_word2id).save_corpus_emb(save_filename)
from ssjdn.
这个跟论文里面里面 使用模型训练获取enbedding 的不太一样吧
from ssjdn.
代码里面也没包含其他几个文件的生成方式
from ssjdn.
Related Issues (7)
- F1的计算 HOT 1
- About KBP2015 Issue HOT 2
- ACE复现结果 HOT 2
- 关于数据集划分的问题 HOT 1
- 中文数据集 HOT 2
- KBP2015数据处理
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ssjdn.