-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebugger.R
53 lines (35 loc) · 1.09 KB
/
debugger.R
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
# Verify if an entity mention finishes a sentence
findName<-function(t, name)
{
x <- paste0(t, name)
y <- unlist(strsplit(x, name))
last <- length(y)
limit <- nchar(y[last])
if(limit==0)
TRUE
else
FALSE
}
# Check if names occur in the sentence
debugEntities<-function(pair, text, i=1, j=1)
{
#Verify i-th name to left entity
v1 <- unlist(strsplit(text, pair[1]))
left.exists <- length(v1)>i & nchar(text)>sum(nchar(v1))
if(!left.exists)
stop("Left entity not found in the sentence","\n")
#Know if the sentence end was shortened due to left entity
extreme <- findName(text, pair[1])
if(extreme)
remainder <- c(v1[(i+1):length(v1)],pair[1])
else
remainder <- v1[(i+1):length(v1)]
remainder <- paste0(remainder, collapse=pair[1])
#Verify j-th name to right entity
v2 <- unlist(strsplit(remainder, pair[2]))
right.exists <- length(v2)>=j & nchar(remainder)>sum(nchar(v2))
if (!right.exists)
stop("Right entity not found in the sentence","\n")
taken.context <- paste0(v2[1:j], collapse=pair[2])
taken.context
}