我如何在 python/nltk 中使用完整的 penn treebank 数据集
how could I use complete penn treebank dataset inside python/nltk
我正在尝试学习使用 NLTK package in python. In particular, I need to use penn tree bank dataset in NLTK. As far as I know, If I call nltk.download('treebank')
I can get the 5% of the dataset. However, I have a complete dataset in tar.gz file and I want to use it. In here 据说:
If you have access to a full installation of the Penn Treebank, NLTK
can be configured to load it as well. Download the ptb package, and in
the directory nltk_data/corpora/ptb place the BROWN and WSJ
directories of the Treebank installation (symlinks work as well). Then
use the ptb module instead of treebank:
所以,我从终端打开 python,导入 nltk 并输入 nltk.download('ptb')
。使用此命令,"ptb" 目录已在我的 ~/nltk_data
目录下创建。最后,现在我有了 ~/nltk_data/ptb
目录。在那里,正如我在上面给出的 link 中所建议的那样,我已经放置了我的数据集文件夹。所以这是我的最终目录层次结构。
$: pwd
$: ~/nltk_data/corpora/ptb/WSJ
$: ls
$:00 02 04 06 08 10 12 14 16 18 20 22 24
01 03 05 07 09 11 13 15 17 19 21 23 merge.log
在00到24的所有文件夹里面,有很多.mrg
个文件,比如wsj_0001.mrg , wsj_0002.mrg
等等
现在,让 return 我的问题。同样,根据 here :
如果我写以下内容,我应该能够获取文件ID:
>>> from nltk.corpus import ptb
>>> print(ptb.fileids()) # doctest: +SKIP
['BROWN/CF/CF01.MRG', 'BROWN/CF/CF02.MRG', 'BROWN/CF/CF03.MRG', 'BROWN/CF/CF04.MRG', ...]
不幸的是,当我输入 print(ptb.fileids())
时,我得到的是空数组。
>>> print(ptb.fileids())
[]
有没有人可以帮助我?
编辑
这是我的 ptb 目录和一些 allcats.txt 文件的内容:
$: pwd
$: ~/nltk_data/corpora/ptb
$: ls
$: allcats.txt WSJ
$: cat allcats.txt
$: WSJ/00/WSJ_0001.MRG news
WSJ/00/WSJ_0002.MRG news
WSJ/00/WSJ_0003.MRG news
WSJ/00/WSJ_0004.MRG news
WSJ/00/WSJ_0005.MRG news
and so on ..
PTB 语料库 reader 需要大写目录和文件名(正如您在问题中包含的 allcats.txt
的内容所暗示的)。这与使用小写字母的 Penn Treebank 的许多发行版冲突。
一个快速解决方法是将文件夹 wsj
和 brown
及其内容重命名为大写。您可以为此使用的 UNIX 命令是:
find . -depth | \
while read LONG
do
SHORT=$( basename "$LONG" | tr '[:lower:]' '[:upper:]' )
DIR=$( dirname "$LONG" )
if [ "${LONG}" != "${DIR}/${SHORT}" ]
then
mv "${LONG}" "${DIR}/${SHORT}"
fi
done
(从 this question 获得)。它会递归地将目录和文件名更改为大写。
我正在尝试学习使用 NLTK package in python. In particular, I need to use penn tree bank dataset in NLTK. As far as I know, If I call nltk.download('treebank')
I can get the 5% of the dataset. However, I have a complete dataset in tar.gz file and I want to use it. In here 据说:
If you have access to a full installation of the Penn Treebank, NLTK can be configured to load it as well. Download the ptb package, and in the directory nltk_data/corpora/ptb place the BROWN and WSJ directories of the Treebank installation (symlinks work as well). Then use the ptb module instead of treebank:
所以,我从终端打开 python,导入 nltk 并输入 nltk.download('ptb')
。使用此命令,"ptb" 目录已在我的 ~/nltk_data
目录下创建。最后,现在我有了 ~/nltk_data/ptb
目录。在那里,正如我在上面给出的 link 中所建议的那样,我已经放置了我的数据集文件夹。所以这是我的最终目录层次结构。
$: pwd
$: ~/nltk_data/corpora/ptb/WSJ
$: ls
$:00 02 04 06 08 10 12 14 16 18 20 22 24
01 03 05 07 09 11 13 15 17 19 21 23 merge.log
在00到24的所有文件夹里面,有很多.mrg
个文件,比如wsj_0001.mrg , wsj_0002.mrg
等等
现在,让 return 我的问题。同样,根据 here :
如果我写以下内容,我应该能够获取文件ID:
>>> from nltk.corpus import ptb
>>> print(ptb.fileids()) # doctest: +SKIP
['BROWN/CF/CF01.MRG', 'BROWN/CF/CF02.MRG', 'BROWN/CF/CF03.MRG', 'BROWN/CF/CF04.MRG', ...]
不幸的是,当我输入 print(ptb.fileids())
时,我得到的是空数组。
>>> print(ptb.fileids())
[]
有没有人可以帮助我?
编辑 这是我的 ptb 目录和一些 allcats.txt 文件的内容:
$: pwd
$: ~/nltk_data/corpora/ptb
$: ls
$: allcats.txt WSJ
$: cat allcats.txt
$: WSJ/00/WSJ_0001.MRG news
WSJ/00/WSJ_0002.MRG news
WSJ/00/WSJ_0003.MRG news
WSJ/00/WSJ_0004.MRG news
WSJ/00/WSJ_0005.MRG news
and so on ..
PTB 语料库 reader 需要大写目录和文件名(正如您在问题中包含的 allcats.txt
的内容所暗示的)。这与使用小写字母的 Penn Treebank 的许多发行版冲突。
一个快速解决方法是将文件夹 wsj
和 brown
及其内容重命名为大写。您可以为此使用的 UNIX 命令是:
find . -depth | \
while read LONG
do
SHORT=$( basename "$LONG" | tr '[:lower:]' '[:upper:]' )
DIR=$( dirname "$LONG" )
if [ "${LONG}" != "${DIR}/${SHORT}" ]
then
mv "${LONG}" "${DIR}/${SHORT}"
fi
done
(从 this question 获得)。它会递归地将目录和文件名更改为大写。