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
use std::borrow::Borrow;
use std::collections::HashSet;
use petgraph::prelude::{EdgeIndex, NodeIndex};
use crate::Tree;
pub struct Climber {
cur: Option<(NodeIndex, EdgeIndex)>,
}
impl Climber {
pub fn new(node: NodeIndex, tree: &Tree) -> Self {
let cur = tree.parent(node);
Climber { cur }
}
pub fn next(&mut self, tree: &Tree) -> Option<NodeIndex> {
if let Some((node, _)) = self.cur {
self.cur = tree.parent(node);
Some(node)
} else {
None
}
}
pub fn next_with_edge(&mut self, tree: &Tree) -> Option<(NodeIndex, EdgeIndex)> {
if let Some((node, edge)) = self.cur {
self.cur = tree.parent(node);
Some((node, edge))
} else {
None
}
}
}
#[derive(Clone)]
pub enum LabelSet {
Positive(HashSet<String>),
Negative(HashSet<String>),
}
impl LabelSet {
pub fn matches(&self, q: impl Borrow<str>) -> bool {
match self {
LabelSet::Positive(ref set) => set.contains(q.borrow()),
LabelSet::Negative(ref set) => !set.contains(q.borrow()),
}
}
}
#[cfg(test)]
mod tests {
use std::collections::HashSet;
use crate::io::PTBFormat;
use crate::util::{Climber, LabelSet};
#[test]
fn label_set_test() {
let set = vec!["a".to_string(), "b".to_string(), "c".to_string()]
.into_iter()
.collect::<HashSet<_>>();
let positive_label_set = LabelSet::Positive(set.clone());
assert!(positive_label_set.matches("a"));
assert!(positive_label_set.matches("b"));
assert!(positive_label_set.matches("c"));
assert!(!positive_label_set.matches("d"));
assert!(!positive_label_set.matches("e"));
let positive_label_set = LabelSet::Negative(set);
assert!(!positive_label_set.matches("a"));
assert!(!positive_label_set.matches("b"));
assert!(!positive_label_set.matches("c"));
assert!(positive_label_set.matches("d"));
assert!(positive_label_set.matches("e"));
}
#[test]
fn climber_test() {
let input = "(NX (NN Nounphrase) (PX (PP on) (NX (DET a) (ADJ single) (NX line))))";
let tree = PTBFormat::TueBa.string_to_tree(input).unwrap();
let mut climber = Climber::new(tree.root(), &tree);
assert!(climber.next(&tree).is_none());
let mut terminals = tree.terminals();
terminals.next();
let on_idx = terminals.next().unwrap();
let mut climber = Climber::new(on_idx, &tree);
let px_idx = climber.next(&tree).unwrap();
assert!(tree[px_idx].nonterminal().is_some());
assert_eq!(tree[px_idx].nonterminal().unwrap().label(), "PX");
let nx_idx = climber.next(&tree).unwrap();
assert!(tree[nx_idx].nonterminal().is_some());
assert_eq!(tree[nx_idx].nonterminal().unwrap().label(), "NX");
assert!(climber.next(&tree).is_none());
}
}