Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
D
dlib
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
钟尚武
dlib
Commits
c4dcd48e
Commit
c4dcd48e
authored
Nov 12, 2012
by
Davis King
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added unit tests for find_max_parse_cky().
parent
2337fae8
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
224 additions
and
0 deletions
+224
-0
CMakeLists.txt
dlib/test/CMakeLists.txt
+1
-0
makefile
dlib/test/makefile
+1
-0
parse.cpp
dlib/test/parse.cpp
+222
-0
No files found.
dlib/test/CMakeLists.txt
View file @
c4dcd48e
...
...
@@ -84,6 +84,7 @@ set (tests
optimization.cpp
optimization_test_functions.cpp
opt_qp_solver.cpp
parse.cpp
pipe.cpp
pixel.cpp
probabilistic.cpp
...
...
dlib/test/makefile
View file @
c4dcd48e
...
...
@@ -95,6 +95,7 @@ SRC += multithreaded_object.cpp
SRC
+=
object_detector.cpp
SRC
+=
oca.cpp
SRC
+=
one_vs_all_trainer.cpp
SRC
+=
parse.cpp
SRC
+=
one_vs_one_trainer.cpp
SRC
+=
optimization.cpp
SRC
+=
optimization_test_functions.cpp
...
...
dlib/test/parse.cpp
0 → 100644
View file @
c4dcd48e
// Copyright (C) 2012 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#include <dlib/optimization.h>
#include <sstream>
#include <string>
#include <cstdlib>
#include <ctime>
#include "tester.h"
namespace
{
using
namespace
test
;
using
namespace
dlib
;
using
namespace
std
;
logger
dlog
(
"test.parse"
);
// ----------------------------------------------------------------------------------------
const
unsigned
long
DET
=
0
;
const
unsigned
long
N
=
1
;
const
unsigned
long
V
=
2
;
const
unsigned
long
NP
=
3
;
const
unsigned
long
VP
=
4
;
const
unsigned
long
S
=
5
;
const
unsigned
long
B
=
6
;
const
unsigned
long
G
=
7
;
typedef
unsigned
long
tags
;
template
<
bool
has_glue_term
>
void
user_defined_ruleset
(
const
std
::
vector
<
tags
>&
words
,
const
constituent
<
tags
>&
c
,
std
::
vector
<
std
::
pair
<
tags
,
double
>
>&
possible_ids
)
{
DLIB_TEST
(
c
.
begin
<
c
.
k
&&
c
.
k
<
c
.
end
&&
c
.
end
<=
words
.
size
());
DLIB_TEST
(
possible_ids
.
size
()
==
0
);
if
(
c
.
left_tag
==
NP
&&
c
.
right_tag
==
VP
)
possible_ids
.
push_back
(
make_pair
(
S
,
log
(
0.80
)));
else
if
(
c
.
left_tag
==
DET
&&
c
.
right_tag
==
N
)
possible_ids
.
push_back
(
make_pair
(
NP
,
log
(
0.30
)));
else
if
(
c
.
left_tag
==
V
&&
c
.
right_tag
==
NP
)
{
possible_ids
.
push_back
(
make_pair
(
VP
,
log
(
0.20
)));
possible_ids
.
push_back
(
make_pair
(
B
,
0.10
));
}
else
if
(
has_glue_term
)
{
possible_ids
.
push_back
(
make_pair
(
G
,
log
(
0.01
)));
}
}
// ----------------------------------------------------------------------------------------
void
dotest1
()
{
print_spinner
();
dlog
<<
LINFO
<<
"in dotest1()"
;
std
::
vector
<
std
::
string
>
words
;
std
::
vector
<
tags
>
sequence
;
for
(
int
i
=
0
;
i
<
8
;
++
i
)
{
sequence
.
push_back
(
DET
);
sequence
.
push_back
(
N
);
sequence
.
push_back
(
V
);
sequence
.
push_back
(
DET
);
sequence
.
push_back
(
N
);
words
.
push_back
(
"The"
);
words
.
push_back
(
"flight"
);
words
.
push_back
(
"includes"
);
words
.
push_back
(
"a"
);
words
.
push_back
(
"meal"
);
}
std
::
vector
<
parse_tree_element
<
tags
>
>
parse_tree
;
find_max_parse_cky
(
sequence
,
user_defined_ruleset
<
true
>
,
parse_tree
);
DLIB_TEST
(
parse_tree
.
size
()
!=
0
);
std
::
vector
<
unsigned
long
>
roots
;
find_trees_not_rooted_with_tag
(
parse_tree
,
G
,
roots
);
DLIB_TEST
(
roots
.
size
()
==
8
);
for
(
unsigned
long
i
=
0
;
i
<
roots
.
size
();
++
i
)
{
dlog
<<
LINFO
<<
parse_tree_to_string
(
parse_tree
,
words
,
roots
[
i
]);
DLIB_TEST
(
parse_tree_to_string
(
parse_tree
,
words
,
roots
[
i
])
==
"[[The flight] [includes [a meal]]]"
);
dlog
<<
LINFO
<<
parse_tree_to_string_tagged
(
parse_tree
,
words
,
roots
[
i
]);
DLIB_TEST
(
parse_tree_to_string_tagged
(
parse_tree
,
words
,
roots
[
i
])
==
"[5 [3 The flight] [4 includes [3 a meal]]]"
);
}
words
.
clear
();
sequence
.
clear
();
for
(
int
i
=
0
;
i
<
2
;
++
i
)
{
sequence
.
push_back
(
DET
);
sequence
.
push_back
(
N
);
sequence
.
push_back
(
V
);
sequence
.
push_back
(
DET
);
sequence
.
push_back
(
N
);
words
.
push_back
(
"The"
);
words
.
push_back
(
"flight"
);
words
.
push_back
(
"includes"
);
words
.
push_back
(
"a"
);
words
.
push_back
(
"meal"
);
}
find_max_parse_cky
(
sequence
,
user_defined_ruleset
<
true
>
,
parse_tree
);
DLIB_TEST
(
parse_tree
.
size
()
!=
0
);
const
std
::
string
str1
=
"[[[The flight] [includes [a meal]]] [[The flight] [includes [a meal]]]]"
;
const
std
::
string
str2
=
"[7 [5 [3 The flight] [4 includes [3 a meal]]] [5 [3 The flight] [4 includes [3 a meal]]]]"
;
dlog
<<
LINFO
<<
parse_tree_to_string
(
parse_tree
,
words
);
DLIB_TEST
(
parse_tree_to_string
(
parse_tree
,
words
)
==
str1
);
dlog
<<
LINFO
<<
parse_tree_to_string_tagged
(
parse_tree
,
words
);
DLIB_TEST
(
parse_tree_to_string_tagged
(
parse_tree
,
words
)
==
str2
);
sequence
.
clear
();
find_max_parse_cky
(
sequence
,
user_defined_ruleset
<
true
>
,
parse_tree
);
DLIB_TEST
(
parse_tree
.
size
()
==
0
);
}
// ----------------------------------------------------------------------------------------
void
dotest2
()
{
print_spinner
();
dlog
<<
LINFO
<<
"in dotest2()"
;
std
::
vector
<
std
::
string
>
words
;
std
::
vector
<
tags
>
sequence
;
for
(
int
i
=
0
;
i
<
8
;
++
i
)
{
sequence
.
push_back
(
DET
);
sequence
.
push_back
(
N
);
sequence
.
push_back
(
V
);
sequence
.
push_back
(
DET
);
sequence
.
push_back
(
N
);
words
.
push_back
(
"The"
);
words
.
push_back
(
"flight"
);
words
.
push_back
(
"includes"
);
words
.
push_back
(
"a"
);
words
.
push_back
(
"meal"
);
}
std
::
vector
<
parse_tree_element
<
tags
>
>
parse_tree
;
find_max_parse_cky
(
sequence
,
user_defined_ruleset
<
false
>
,
parse_tree
);
DLIB_TEST
(
parse_tree
.
size
()
==
0
);
std
::
vector
<
unsigned
long
>
roots
;
find_trees_not_rooted_with_tag
(
parse_tree
,
G
,
roots
);
DLIB_TEST
(
roots
.
size
()
==
0
);
words
.
clear
();
sequence
.
clear
();
for
(
int
i
=
0
;
i
<
2
;
++
i
)
{
sequence
.
push_back
(
DET
);
sequence
.
push_back
(
N
);
sequence
.
push_back
(
V
);
sequence
.
push_back
(
DET
);
sequence
.
push_back
(
N
);
words
.
push_back
(
"The"
);
words
.
push_back
(
"flight"
);
words
.
push_back
(
"includes"
);
words
.
push_back
(
"a"
);
words
.
push_back
(
"meal"
);
}
find_max_parse_cky
(
sequence
,
user_defined_ruleset
<
false
>
,
parse_tree
);
DLIB_TEST
(
parse_tree
.
size
()
==
0
);
sequence
.
clear
();
find_max_parse_cky
(
sequence
,
user_defined_ruleset
<
false
>
,
parse_tree
);
DLIB_TEST
(
parse_tree
.
size
()
==
0
);
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
class
parse_tester
:
public
tester
{
public
:
parse_tester
(
)
:
tester
(
"test_parse"
,
"Runs tests on the parsing tools."
)
{}
void
perform_test
(
)
{
dotest1
();
dotest2
();
}
}
a
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment