-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtract Questions from XML.Rmd
86 lines (61 loc) · 1.38 KB
/
Extract Questions from XML.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
---
title: "Text Analysis 10"
author: "Junyan Yao"
date: "12/17/2017"
output: html_document
---
```{r}
library(XML)
library(xml2)
```
```{r}
setwd("/Users/YaoJunyan/Documents/Text-Analysis/problem")
temp <- list.files()
myfiles<- lapply (temp, xmlParse)
myfiles[[1]]
readfiles= lapply (temp, read_xml)
readfiles[1]
```
extract the pattern
Some problems have images inserted. Some are not;
When they insert the image, the problem questions is the second line;
If they don't have image inserted, the first line is the question
```{r}
img<-rep(NA,122)
for( i in 1:length(myfiles)){
img[i]<- grepl("img", readfiles[[i]])
}
scriptype<-rep(NA,122)
for( i in 1:length(myfiles)){
scriptype[i]<- grepl("system_path", readfiles[[i]])
}
```
Get the questions content for these questions without images
```{r}
xml_children((readfiles[[1]]))[2]
xml_children((readfiles[[3]]))[2]
xml_children((readfiles[[7]]))[2]
```
get the content for all questions
```{r}
for (i in 1:length(readfiles)){
if (img[i]==TRUE){
c[i]<- xml_children((readfiles[[i]]))[2]
}
else{
c[i]<-xml_children((readfiles[[i]]))[1]
}
}
#second try
d<-rep(NA,122)
for (i in 1:length(readfiles)){
if (img[i]==TRUE){
d[i]<- xml_children((readfiles[[i]]))[3]
}
else{
d[i]<-xml_children((readfiles[[i]]))[2]
}
}
df<-data.frame(question1=(as.character(c)))
df2<-data.frame(question2=(as.character(d)))
```