-
Notifications
You must be signed in to change notification settings - Fork 289
Expand file tree
/
Copy pathnominal_phrase.rs
More file actions
178 lines (150 loc) · 4.67 KB
/
nominal_phrase.rs
File metadata and controls
178 lines (150 loc) · 4.67 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use crate::Token;
use super::Pattern;
#[derive(Default)]
pub struct NominalPhrase;
impl Pattern for NominalPhrase {
fn matches(&self, tokens: &[Token], _source: &[char]) -> Option<usize> {
let mut cursor = 0;
loop {
let tok = tokens.get(cursor)?;
if (tok.kind.is_adjective()
|| tok.kind.is_determiner()
|| tok.kind.is_verb_progressive_form())
&& let Some(next) = tokens.get(cursor + 1)
&& next.kind.is_whitespace()
{
cursor += 2;
continue;
}
if tok.kind.is_nominal() {
return Some(cursor + 1);
}
return None;
}
}
}
#[cfg(test)]
mod tests {
use super::super::DocPattern;
use super::NominalPhrase;
use crate::{Document, Span, Token, patterns::Pattern};
trait SpanVecExt {
fn to_strings(&self, doc: &Document) -> Vec<String>;
}
impl SpanVecExt for Vec<Span<Token>> {
fn to_strings(&self, doc: &Document) -> Vec<String> {
self.iter()
.map(|sp| {
doc.get_tokens()[sp.start..sp.end]
.iter()
.map(|tok| doc.get_span_content_str(&tok.span))
.collect::<String>()
})
.collect()
}
}
#[test]
fn simple_apple() {
let doc = Document::new_markdown_default_curated("A red apple");
let matches = NominalPhrase.find_all_matches_in_doc(&doc);
assert_eq!(matches.to_strings(&doc), vec!["A red apple"])
}
#[test]
fn complex_apple() {
let doc = Document::new_markdown_default_curated("A red apple with a long stem");
let matches = NominalPhrase.find_all_matches_in_doc(&doc);
assert_eq!(matches.to_strings(&doc), vec!["A red apple", "a long stem"])
}
#[test]
fn list_fruit() {
let doc = Document::new_markdown_default_curated("An apple, a banana and a pear");
let matches = NominalPhrase.find_all_matches_in_doc(&doc);
assert_eq!(
matches.to_strings(&doc),
vec!["An apple", "a banana", "a pear"]
)
}
#[test]
fn simplest_banana() {
let doc = Document::new_markdown_default_curated("a banana");
assert!(
NominalPhrase
.matches(doc.get_tokens(), doc.get_source())
.is_some()
);
}
#[test]
fn food() {
let doc = Document::new_markdown_default_curated(
"My favorite foods are pizza, sushi, tacos and burgers.",
);
let matches = NominalPhrase.find_all_matches_in_doc(&doc);
dbg!(&matches);
dbg!(matches.to_strings(&doc));
for span in &matches {
let gc = span
.to_char_span(doc.get_tokens())
.get_content(doc.get_source());
dbg!(gc);
}
assert_eq!(
matches.to_strings(&doc),
vec!["My favorite foods", "pizza", "sushi", "tacos", "burgers"]
)
}
#[test]
fn simplest_way() {
let doc = Document::new_markdown_default_curated("a way");
assert!(
NominalPhrase
.matches(doc.get_tokens(), doc.get_source())
.is_some()
);
}
#[test]
fn present_participle_way() {
let doc = Document::new_markdown_default_curated("a winning way");
assert!(
NominalPhrase
.matches(doc.get_tokens(), doc.get_source())
.is_some()
);
}
#[test]
fn perfect_participle_way() {
let doc = Document::new_markdown_default_curated("a failed way");
assert!(
NominalPhrase
.matches(doc.get_tokens(), doc.get_source())
.is_some()
);
}
// Tests that should not pass but do
#[test]
fn the_the_car() {
let doc = Document::new_markdown_default_curated("the the car");
assert!(
NominalPhrase
.matches(doc.get_tokens(), doc.get_source())
.is_some()
);
}
#[test]
fn red_the_car() {
let doc = Document::new_markdown_default_curated("red the car");
assert!(
NominalPhrase
.matches(doc.get_tokens(), doc.get_source())
.is_some()
);
}
#[test]
fn speeding_the_a_car() {
let doc = Document::new_markdown_default_curated("speeding the a car");
assert!(
NominalPhrase
.matches(doc.get_tokens(), doc.get_source())
.is_some()
);
}
}