Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
senpy
senpy
Commits
00da7515
Commit
00da7515
authored
Jun 12, 2017
by
J. Fernando Sánchez
Browse files
Change conversion to Euclidean distance
* Added neutral point (if present) Closes !gsi-upm/senpy#37 (Ian's)
parent
6b843a43
Changes
1
Hide whitespace changes
Inline
Side-by-side
senpy/plugins/conversion/centroids.py
View file @
00da7515
...
...
@@ -32,8 +32,17 @@ class CentroidConversion(EmotionConversionPlugin):
nv1
[
aliases
.
get
(
k2
,
k2
)]
=
v2
ncentroids
[
aliases
.
get
(
k1
,
k1
)]
=
nv1
info
[
'centroids'
]
=
ncentroids
super
(
CentroidConversion
,
self
).
__init__
(
info
)
self
.
dimensions
=
set
()
for
c
in
self
.
centroids
.
values
():
self
.
dimensions
.
update
(
c
.
keys
())
self
.
neutralPoints
=
self
.
get
(
"neutralPoints"
,
dict
())
if
not
self
.
neutralPoints
:
for
i
in
self
.
dimensions
:
self
.
neutralPoints
[
i
]
=
self
.
get
(
"neutralValue"
,
0
)
def
_forward_conversion
(
self
,
original
):
"""Sum the VAD value of all categories found."""
res
=
Emotion
()
...
...
@@ -49,15 +58,19 @@ class CentroidConversion(EmotionConversionPlugin):
def
_backwards_conversion
(
self
,
original
):
"""Find the closest category"""
dimensions
=
set
(
c
.
keys
()
for
c
in
centroids
.
values
())
neutralPoint
=
self
.
get
(
"origin"
,
None
)
neutralPoint
=
{
k
:
neutralPoint
[
k
]
if
k
in
neturalPoint
else
0
}
centroids
=
self
.
centroids
neutralPoints
=
self
.
neutralPoints
dimensions
=
self
.
dimensions
def
distance_k
(
centroid
,
original
,
k
):
# k component of the distance between the value and a given centroid
return
(
centroid
.
get
(
k
,
neutralPoints
[
k
])
-
original
.
get
(
k
,
neutralPoints
[
k
]))
**
2
def
distance
(
centroid
):
return
sum
((
centroid
.
get
(
k
,
neutralPoint
[
k
])
-
original
.
get
(
k
,
neutralPoint
[
k
]))
**
2
for
k
in
dimensions
)
return
sum
(
distance_k
(
centroid
,
original
,
k
)
for
k
in
dimensions
)
emotion
=
min
(
centroids
,
key
=
lambda
x
:
distance
(
centroids
[
x
]))
emotion
=
min
(
centroids
,
key
=
lambda
x
:
distance
(
centroids
[
x
])
result
=
Emotion
(
onyx__hasEmotionCategory
=
emotion
)
return
result
...
...
Write
Preview
Supports
Markdown
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