forked from zzw922cn/Automatic_Speech_Recognition
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Former-commit-id: 50cdeba
- Loading branch information
Showing
6 changed files
with
206 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
|
||
import numpy as np | ||
import pickle | ||
|
||
def load_obj(name): | ||
with open(name + '.pkl', 'rb') as f: | ||
return pickle.load(f) | ||
|
||
def frequence(gram, type=2): | ||
if type == 2: | ||
for key, value in gram.items(): | ||
total = 0.0 | ||
for subkey, subvalue in value.items(): | ||
total += subvalue | ||
|
||
for subkey, subvalue in value.items(): | ||
gram[key][subkey] = subvalue/total | ||
else: | ||
raise NotImplementedError('%s-gram is being developed'%type) | ||
return gram | ||
|
||
|
||
def generate_sentence(corpus_dir, seed='what are', length=10): | ||
bigram = load_obj(corpus_dir+'bigram') | ||
freq_bigram = frequence(bigram) | ||
sent = '' | ||
if not ' ' in seed: | ||
sent += seed | ||
prev = seed | ||
for i in range(length): | ||
probs = [] | ||
for _, value in freq_bigram[prev].items(): | ||
probs.append(value) | ||
sample = np.random.choice(range(len(freq_bigram[prev])),p=probs) | ||
prev = freq_bigram[prev].keys()[sample] | ||
sent += ' '+prev | ||
print sent | ||
|
||
generate_sentence('/home/pony/github/data/libri/ngram/', seed='love', length=10) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# -*- coding:utf-8 -*- | ||
#!/usr/bin/python | ||
|
||
''' Language modelling for automatic speech recognition based on n-gram | ||
author: | ||
iiiiiiiiiiii iiiiiiiiiiii !!!!!!! !!!!!! | ||
# ### # ### ### I# #: | ||
# ### # I##; ##; ## ## | ||
### ### !## #### # | ||
### ### ### ## ### #' | ||
!##; `##% ##; ## ### ## | ||
### ### $## `# ## # | ||
### # ### # #### ####; | ||
`### -# ### `# ### ### | ||
############## ############## `# # | ||
date:2017-04-17 | ||
''' | ||
import numpy as np | ||
import os | ||
import operator | ||
import pickle | ||
|
||
def save_obj(name, obj): | ||
with open(name + '.pkl', 'wb') as f: | ||
pickle.dump(obj, f, pickle.HIGHEST_PROTOCOL) | ||
|
||
class NGram: | ||
def __init__(self, rootdir): | ||
self.rootdir = rootdir | ||
|
||
def get_corpus(self): | ||
corpus = [] | ||
word_count = {} | ||
biword_count = {} | ||
bigram = {} | ||
bigram['SOS'] = {} | ||
trigram = {} | ||
for subdir, dirs, files in os.walk(self.rootdir): | ||
for f in files: | ||
fullFilename = os.path.join(subdir, f) | ||
filenameNoSuffix = os.path.splitext(fullFilename)[0] | ||
if f.endswith('.label'): | ||
with open(fullFilename, 'r') as f: | ||
line = f.readline() | ||
corpus.append(line) | ||
line = line.strip().split(' ') | ||
len_sent = range(len(line)) | ||
for idx in len_sent: | ||
word = line[idx] | ||
word_count = inc_dict(word_count, word) | ||
|
||
if not bigram.has_key(word): | ||
bigram[word] = {} | ||
|
||
if idx == 0: | ||
bigram['SOS'] = inc_dict(bigram['SOS'], word) | ||
|
||
elif idx != len(line)-1: | ||
bigram[word] = inc_dict(bigram[word], line[idx+1]) | ||
|
||
else: | ||
bigram[word] = inc_dict(bigram[word], 'EOS') | ||
|
||
if idx == 0: | ||
tri_key = 'SOS ' + word | ||
else: | ||
tri_key = line[idx-1]+' '+word | ||
if not trigram.has_key(tri_key): | ||
trigram[tri_key] = {} | ||
if idx == len(line)-1: | ||
trigram[tri_key] = inc_dict(trigram[tri_key], 'EOS') | ||
else: | ||
trigram[tri_key] = inc_dict(trigram[tri_key], line[idx+1]) | ||
|
||
return corpus, word_count, bigram, trigram | ||
|
||
|
||
def inc_dict(dic, key): | ||
if not dic.has_key(key): | ||
dic[key] = 0 | ||
dic[key] += 1 | ||
return dic | ||
|
||
|
||
if __name__ == '__main__': | ||
ngram = NGram('/media/pony/Seagate Expansion Drive/学习/语音识别/ASR数据库/LibriSpeech/') | ||
corpus, word_count, bigram, trigram = ngram.get_corpus() | ||
savedir = '/home/pony/github/data/libri/ngram/' | ||
save_obj(savedir+'corpus', corpus) | ||
save_obj(savedir+'word_count', word_count) | ||
save_obj(savedir+'bigram', bigram) | ||
save_obj(savedir+'trigram', trigram) | ||
#sorted_word_count = sorted(word_count.items(), key=operator.itemgetter(1), reverse=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
#!/bin/bash | ||
|
||
for loop in {2..30} | ||
for loop in {3..30} | ||
do | ||
echo "loop is $loop" | ||
b=$(( $loop % 3 )) | ||
echo "dataset is index $b" | ||
if [ $loop -eq 1 ] | ||
then | ||
/usr/bin/python train.py --mode=train | ||
/usr/bin/python train.py --lb=$b | ||
else | ||
/usr/bin/python train.py --mode=train --keep=True | ||
fi | ||
/usr/bin/python train.py --lb=$b --keep=True | ||
/usr/bin/python train.py --mode=test | ||
fi | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters