我正在尝试处理一个名为的“分段文件”.TextGrid
(由 Praat 程序生成)。 )
原始格式如下所示:
File type = "ooTextFile"
Object class = "TextGrid"
xmin = 0
xmax = 243.761375
tiers? <exists>
size = 17
item []:
item [1]:
class = "IntervalTier"
name = "phones"
xmin = 0
xmax = 243.761
intervals: size = 2505
intervals [1]:
xmin = 0
xmax = 0.4274939687384032
text = "_"
intervals [2]:
xmin = 0.4274939687384032
xmax = 0.472
text = "v"
intervals [3]:
[...]
(然后重复到 EOF,文件中的 n 个项目(注释层)的间隔为 [3 到 n]。
有人提出了一个解决方案 http://nltk.googlecode.com/svn/trunk/nltk_contrib/nltk_contrib/textgrid.py using rPython R 包 http://rpython.r-forge.r-project.org/.
很遗憾 :
- 我对 Python 不太了解
-
rPython 版本不适用于 R.3.0.2(我正在使用)。
- 我的目标是开发这个解析器,专门在 R 环境下进行分析。
现在我的目标是将该文件分割成多个数据帧。每个数据框应包含一项(注释层)。
# Load the Data
txtgrid <- read.delim("./xxx_01_xx.textgrid", sep=c("=","\n"), dec=".", header=FALSE)
# Erase White spaces (use stringr package)
txtgrid[,1] <- str_trim(txtgrid[,1])
# Convert row.names to numeric
num.row<- as.numeric(row.names(txtgrid))
# Redefine the original textgrid and add those rows (I want to "keep them in case for later process)
txtgrid <- data.frame(num.row,txtgrid)
colnames(txtgrid) <- c("num.row","object", "value")
head(txtgrid)
的输出head(txtgrid)
非常原始,所以这是文本网格的前 20 行txtgrid[1:20,]
:
num.row object value
1 1 File type ooTextFile
2 2 Object class TextGrid
3 3 xmin 0
4 4 xmax 243.761375
5 5 tiers? <exists>
6 6 size 17
7 7 item []:
8 8 item [1]:
9 9 class IntervalTier
10 10 name phones
11 11 xmin 0
12 12 xmax 243.761
13 13 intervals: size 2505
14 14 intervals [1]:
15 15 xmin 0
16 16 xmax 0.4274939687384032
17 17 text _
18 18 intervals [2]:
19 19 xmin 0.4274939687384032
20 20 xmax 0.472
现在我已经对其进行了预处理,我可以:
# Find the number of the rows where I want to split (i.e. Item)
tier.begining <- txtgrid[grep("item", txtgrid$object, perl=TRUE), ]
# And save those numbers in a variable
x <- as.numeric(row.names(tier.begining))
这个变量x
给我数字-1,其中我的数据应该分成几个数据帧。
我有 18 个项目 -1 (第一个项目是 item[] 并包括所有其他项目。所以向量x
is :
x
[1] 7 8 10034 14624 19214 22444 25674 28904 31910 35140 38146 38156 38566 39040 39778 40222 44800
[18] 45018
我怎样才能告诉R:将此数据帧分割为多个数据帧textgrids$nameoftheItem
以这样的方式,我可以获得尽可能多的项目数据框?,例如:
textgrid$phones
item [1]:
class = "IntervalTier"
name = "phones"
xmin = 0
xmax = 243.761
intervals: size = 2505
intervals [1]:
xmin = 0
xmax = 0.4274939687384032
text = "_"
intervals [2]:
xmin = 0.4274939687384032
xmax = 0.472
text = "v"
[...]
intervals [n]:
textgrid$syllable
item [2]:
class = "IntervalTier"
name = "syllable"
xmin = 0
xmax = 243.761
intervals: size = 1200
intervals [1]:
xmin = 0
xmax = 0.500
text = "ve"
intervals [2]:
[...]
intervals [n]:
textgrid$item[n]
我想用
txtgrid.new <- split(txtgrid, f=x)
但这个消息是正确的:
Warning message: In split.default(x = seq_len(nrow(x)), f = f, drop = drop, ...) : data length is not a multiple of split variable
我没有得到所需的输出,似乎行号不相互跟随,并且文件全部混合在一起。
我也尝试过一些which
, daply
(from plyr
) & subset
功能,但从未让它们正常工作!
我欢迎任何正确有效地构建这些数据的想法。理想情况下,我应该能够链接它们之间的项目(注释层)(不同层的 xmin 和 xmax),以及多个文本网格文件,这只是开始。