Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
models
提交
e17bca2a
M
models
项目概览
PaddlePaddle
/
models
大约 1 年 前同步成功
通知
222
Star
6828
Fork
2962
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
602
列表
看板
标记
里程碑
合并请求
255
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
models
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
602
Issue
602
列表
看板
标记
里程碑
合并请求
255
合并请求
255
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
e17bca2a
编写于
6月 01, 2017
作者:
Z
zhaopu7
提交者:
GitHub
6月 01, 2017
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add files via upload
上级
485e88ba
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
108 addition
and
0 deletion
+108
-0
language_model/data_util.py
language_model/data_util.py
+108
-0
未找到文件。
language_model/data_util.py
0 → 100644
浏览文件 @
e17bca2a
# coding=utf-8
import
numpy
as
np
import
collections
# config
train_file
=
'data/ptb.train.txt'
test_file
=
'data/ptb.test.txt'
vocab_max_size
=
3000
min_sentence_length
=
3
max_sentence_length
=
60
def
build_vocab
():
"""
build vacab.
:return: dictionary with content of '{word, id}', 'word' is string type , 'id' is int type.
"""
words
=
[]
for
line
in
open
(
train_file
):
words
+=
line
.
decode
(
'utf-8'
,
'ignore'
).
strip
().
split
()
counter
=
collections
.
Counter
(
words
)
counter
=
sorted
(
counter
.
items
(),
key
=
lambda
x
:
-
x
[
1
])
if
len
(
counter
)
>
vocab_max_size
:
counter
=
counter
[:
vocab_max_size
]
words
,
counts
=
zip
(
*
counter
)
word_id_dict
=
dict
(
zip
(
words
,
range
(
2
,
len
(
words
)
+
2
)))
word_id_dict
[
'<UNK>'
]
=
0
word_id_dict
[
'<EOS>'
]
=
1
return
word_id_dict
def
_read_by_fixed_length
(
file_name
,
sentence_len
=
10
):
"""
create reader, each sample with fixed length.
:param file_name: file name.
:param sentence_len: each sample's length.
:return: data reader.
"""
def
reader
():
word_id_dict
=
build_vocab
()
words
=
[]
UNK
=
word_id_dict
[
'<UNK>'
]
for
line
in
open
(
file_name
):
words
+=
line
.
decode
(
'utf-8'
,
'ignore'
).
strip
().
split
()
ids
=
[
word_id_dict
.
get
(
w
,
UNK
)
for
w
in
words
]
words_len
=
len
(
words
)
sentence_num
=
(
words_len
-
1
)
//
sentence_len
count
=
0
while
count
<
sentence_num
:
start
=
count
*
sentence_len
count
+=
1
yield
ids
[
start
:
start
+
sentence_len
],
ids
[
start
+
1
:
start
+
sentence_len
+
1
]
return
reader
def
_read_by_line
(
file_name
):
"""
create reader, each line is a sample.
:param file_name: file name.
:return: data reader.
"""
def
reader
():
word_id_dict
=
build_vocab
()
UNK
=
word_id_dict
[
'<UNK>'
]
for
line
in
open
(
file_name
):
words
=
line
.
decode
(
'utf-8'
,
'ignore'
).
strip
().
split
()
if
len
(
words
)
<
min_sentence_length
or
len
(
words
)
>
max_sentence_length
:
continue
ids
=
[
word_id_dict
.
get
(
w
,
UNK
)
for
w
in
words
]
ids
.
append
(
word_id_dict
[
'<EOS>'
])
target
=
ids
[
1
:]
target
.
append
(
word_id_dict
[
'<EOS>'
])
yield
ids
[:],
target
[:]
return
reader
def
_reader_creator_for_NGram
(
file_name
,
N
):
"""
create reader for ngram.
:param file_name: file name.
:param N: ngram's n.
:return: data reader.
"""
assert
N
>=
2
def
reader
():
word_id_dict
=
build_vocab
()
words
=
[]
UNK
=
word_id_dict
[
'<UNK>'
]
for
line
in
open
(
file_name
):
words
+=
line
.
decode
(
'utf-8'
,
'ignore'
).
strip
().
split
()
ids
=
[
word_id_dict
.
get
(
w
,
UNK
)
for
w
in
words
]
words_len
=
len
(
words
)
for
i
in
range
(
words_len
-
N
-
1
):
yield
tuple
(
ids
[
i
:
i
+
N
])
return
reader
def
train_data
():
return
_read_by_line
(
train_file
)
def
test_data
():
return
_read_by_line
(
test_file
)
def
train_data_for_NGram
(
N
):
return
_reader_creator_for_NGram
(
train_file
,
N
)
def
test_data_for_NGram
(
N
):
return
_reader_creator_for_NGram
(
test_file
,
N
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录