File: tnt.R

package info (click to toggle)
r-cran-nlp 0.3-2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 456 kB
  • sloc: makefile: 2
file content (141 lines) | stat: -rw-r--r-- 3,161 bytes parent folder | download | duplicates (5)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
## A simple class for storing tokens and tags ("tagged tokens").

## Conceptually, a single tagged token is a token/tag pair and our
## Tagged_Token objects are sequences (to allow positional access) of
## tagged tokens, i.e., sequences of pairs.
## The implementation actually uses a "pair" (named list of length two)
## of "slots" giving the token and tag sequences.
## Subscripting via [ extracts subsets of tagged tokens.
## Subscripting via $ extracts one slot.

Tagged_Token_slot_names <- c("token", "tag")

Tagged_Token <-
function(token, tag)
{
    token <- as.character(token)
    tag <- as.character(tag)
    if(length(token) != length(tag))
        stop("arguments must have the same length")
    .Tagged_Token_from_args(token, tag)
}

.Tagged_Token_from_args <-
function(token, tag)
{
    x <- list(token, tag)
    names(x) <- Tagged_Token_slot_names
    .Tagged_Token_from_list(x)
}

.Tagged_Token_from_list <-
function(x)
{
    class(x) <- "Tagged_Token"
    x
}

as.Tagged_Token <-
function(x)
    UseMethod("as.Tagged_Token")

as.Tagged_Token.Tagged_Token <- identity

## <FIXME>
## Should this get a '...'?  (And hence the generic, too?)
as.Tagged_Token.TextDocument <-
function(x)
    tagged_words(x)
## </FIXME>

is.Tagged_Token <-
function(x)
    inherits(x, "Tagged_Token")

`[.Tagged_Token` <-
function(x, i)
    .Tagged_Token_from_list(lapply(unclass(x), `[`, i))

## <TODO>
## Implement eventually ...
`[<-.Tagged_Token` <-
function(x, i, value)
    .NotYetImplemented()
## </TODO>

`[[.Tagged_Token` <-
function(x, i)
    .Tagged_Token_from_list(lapply(unclass(x), `[[`, i))

## <TODO>
## Implement eventually ...
`[[<-.Tagged_Token` <-
function(x, i, value)
    .NotYetImplemented()
## </TODO>

## $.Tagged_Token is not really necessary.
`$<-.Tagged_Token` <-
function(x, name, value)
{
    n <- length(x)
    x <- unclass(x)
    if(is.na(pos <- pmatch(name, Tagged_Token_slot_names)))
        stop("invalid element name")
    value <- as.integer(value)
    if(length(value) != n)
        stop("replacement must have the same length as object")
    x[[pos]] <- value
    .Tagged_Token_from_list(x)
}

as.data.frame.Tagged_Token <-
function(x, row.names = NULL, optional = FALSE, ...)
{
    data.frame(token = x$token, tag = x$tag, row.names = row.names)
}

as.list.Tagged_Token <-
function(x,  ...) 
    lapply(seq_along(x), function(i) x[i])

c.Tagged_Token <-
function(..., recursive = FALSE)
{
    args <- lapply(list(...), function(e) unclass(as.Tagged_Token(e)))
    y <- lapply(Tagged_Token_slot_names,
                function(e) unlist(lapply(args, `[[`, e)))
    names(y) <- Tagged_Token_slot_names
    .Tagged_Token_from_list(y)
}

duplicated.Tagged_Token <-
function(x, incomparables = FALSE, ...)
{
    do.call(`&`, lapply(unclass(x), duplicated))
}

format.Tagged_Token <-
function(x, ...)
{
    sprintf("%s/%s", x$token, x$tag)
}

length.Tagged_Token <-
function(x)
    length(x$token)

names.Tagged_Token <-
function(x)
    NULL

## print.Tagged_Token <-
## function(x, ...)
## {
##     print(format(x, ...))
##     invisible(x)
## }

unique.Tagged_Token <-
function(x, incomparables = FALSE, ...)
    x[!duplicated(x)]