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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
//! Core types for copyright detection.
//!
//! This module defines:
//! - Detection result types ([`CopyrightDetection`], [`HolderDetection`], [`AuthorDetection`])
//! - The POS tag enum ([`PosTag`]) with 55 variants for token classification
//! - Parse tree types ([`ParseNode`], [`TreeLabel`]) for grammar-based extraction
//! - The [`Token`] struct linking text values to POS tags and source locations
use serde::Serialize;
/// A detected copyright statement with source location.
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct CopyrightDetection {
/// The full copyright text (e.g., "Copyright 2024 Acme Inc.").
pub copyright: String,
/// 1-based line number where this detection starts.
pub start_line: usize,
/// 1-based line number where this detection ends.
pub end_line: usize,
}
/// A detected copyright holder name with source location.
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct HolderDetection {
/// The holder name (e.g., "Acme Inc.").
pub holder: String,
/// 1-based line number where this detection starts.
pub start_line: usize,
/// 1-based line number where this detection ends.
pub end_line: usize,
}
/// A detected author name with source location.
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct AuthorDetection {
/// The author name (e.g., "John Doe").
pub author: String,
/// 1-based line number where this detection starts.
pub start_line: usize,
/// 1-based line number where this detection ends.
pub end_line: usize,
}
/// Part-of-Speech tag for a token (type-safe, not stringly-typed)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum PosTag {
// Copyright keywords
Copy, // "Copyright", "(c)", "Copr.", etc.
SpdxContrib, // "SPDX-FileContributor"
// Year-related
Yr, // A year like "2024"
YrPlus, // Year with plus: "2024+"
BareYr, // Short year: "99"
// Names and entities
Nnp, // Proper noun: "John", "Smith"
Nn, // Common noun (catch-all)
Caps, // All-caps word: "MIT", "IBM"
Pn, // Dotted name: "P.", "DMTF."
MixedCap, // Mixed case: "LeGrande"
// Organization suffixes
Comp, // Company suffix: "Inc.", "Ltd.", "GmbH"
Uni, // University: "University", "College"
// Author keywords
Auth, // "Author", "@author"
Auth2, // "Written", "Developed", "Created"
Auths, // "Authors", "author's"
AuthDot, // "Author.", "Authors."
Maint, // "Maintainer", "Developer"
Contributors, // "Contributors"
Commit, // "Committers"
// Rights reserved
Right, // "Rights", "Rechte", "Droits"
Reserved, // "Reserved", "Vorbehalten", "Réservés"
// Conjunctions and prepositions
Cc, // "and", "&", ","
Of, // "of", "De", "Di"
By, // "by"
In, // "in", "en"
Van, // "van", "von", "de", "du"
To, // "to"
Dash, // "-", "--", "/"
// Special
Email, // Email address
EmailStart, // Email opening bracket like "<foo"
EmailEnd, // Email closing bracket like "bar>"
Url, // URL with scheme
Url2, // URL without scheme (domain.com)
Holder, // "Holder", "Holders"
Is, // "is", "are"
Held, // "held"
Notice, // "NOTICE"
Portions, // "Portions", "Parts"
Oth, // "Others", "et al."
Following, // "following"
Mit, // "MIT" (special handling)
Linux, // "Linux"
Parens, // "(" or ")"
At, // "AT" (obfuscated email)
Dot, // "DOT" (obfuscated email)
Ou, // "OU" (org unit in certs)
// Structural
EmptyLine, // Empty line marker
Junk, // Junk to ignore
// Cardinals
Cd, // Cardinal number
Cds, // Small cardinal (0-39)
Month, // Month abbreviation
Day, // Day of week
}
/// A token with its POS tag and source location.
#[derive(Debug, Clone)]
pub struct Token {
/// The token text (e.g., "Copyright", "2024", "Acme").
pub value: String,
/// The assigned POS tag.
pub tag: PosTag,
/// 1-based source line number.
pub start_line: usize,
}
/// A node in the parse tree
#[derive(Debug, Clone)]
pub enum ParseNode {
Leaf(Token),
Tree {
label: TreeLabel,
children: Vec<ParseNode>,
},
}
/// Labels for parse tree nodes (grammar non-terminals)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TreeLabel {
YrRange,
YrAnd,
AllRightReserved,
Name,
NameEmail,
NameYear,
NameCopy,
NameCaps,
Company,
AndCo,
Copyright,
Copyright2,
Author,
AndAuth,
InitialDev,
DashCaps,
}
impl ParseNode {
/// Get the tag of this node (for leaf tokens) or None (for trees)
pub fn tag(&self) -> Option<PosTag> {
match self {
ParseNode::Leaf(token) => Some(token.tag),
ParseNode::Tree { .. } => None,
}
}
/// Get the label of this node (for trees) or None (for leaf tokens)
pub fn label(&self) -> Option<TreeLabel> {
match self {
ParseNode::Tree { label, .. } => Some(*label),
ParseNode::Leaf(_) => None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_copyright_detection_creation() {
let d = CopyrightDetection {
copyright: "Copyright 2024 Acme Inc.".to_string(),
start_line: 1,
end_line: 1,
};
assert_eq!(d.copyright, "Copyright 2024 Acme Inc.");
}
#[test]
fn test_token_creation() {
let t = Token {
value: "Copyright".to_string(),
tag: PosTag::Copy,
start_line: 1,
};
assert_eq!(t.tag, PosTag::Copy);
}
#[test]
fn test_parse_node_leaf() {
let node = ParseNode::Leaf(Token {
value: "2024".to_string(),
tag: PosTag::Yr,
start_line: 5,
});
assert_eq!(node.tag(), Some(PosTag::Yr));
assert_eq!(node.label(), None);
}
#[test]
fn test_parse_node_tree() {
let child = ParseNode::Leaf(Token {
value: "2024".to_string(),
tag: PosTag::Yr,
start_line: 3,
});
let tree = ParseNode::Tree {
label: TreeLabel::YrRange,
children: vec![child],
};
assert_eq!(tree.label(), Some(TreeLabel::YrRange));
assert_eq!(tree.tag(), None);
}
}