Skip to content

Commit

Permalink
merge same speaker overlapping turns
Browse files Browse the repository at this point in the history
  • Loading branch information
desh2608 committed Jan 5, 2022
1 parent 56f5b0e commit b87fda9
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 6 deletions.
110 changes: 110 additions & 0 deletions src/spyder/GroupBy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Copyright (C) 2014 Łukasz Czerwiński
*
* GitHub: https://github.com/wo3kie/groupBy
*
* Distributed under the BSD Software License (see file license)
*/

#ifndef SPYDER_GROUP_BY_H
#define SPYDER_GROUP_BY_H

#include <map>
#include <vector>

namespace spyder
{
/*
* return_type
*/

template< typename T >
struct return_type
{
typedef typename return_type< typename std::decay< decltype( &T::operator() )>::type >::type type;
};

template< typename R, typename C, typename... A >
struct return_type< R ( C::* )( A... ) >
{
typedef typename std::decay< R >::type type;
};

template< typename R, typename C, typename... A >
struct return_type< R ( C::* )( A... ) const >
{
typedef typename std::decay< R >::type type;
};

template< typename R, typename... A >
struct return_type< R ( * )( A... ) >
{
typedef typename std::decay< R >::type type;
};
}

namespace spyder
{
/*
* GroupBy
*/

template< typename Arg, typename... Ts >
struct GroupBy;

template< typename Arg, typename T, typename... Ts >
struct GroupBy< Arg, T, Ts... >
{
typedef std::map<
typename spyder::return_type< typename std::decay< T >::type >::type,
typename GroupBy< Arg, Ts... >::return_type
> return_type;
};

template< typename Arg, typename T >
struct GroupBy< Arg, T >
{
typedef std::map<
typename return_type< typename std::decay< T >::type >::type,
std::vector< Arg >
> return_type;
};
}

namespace spyder
{
/*
* groupByImpl
*/

template< typename Map, typename Iterator, typename F >
void groupByImpl( Map & map, Iterator && current, F && f )
{
map[ f( *current ) ].push_back( *current );
}

template< typename Map, typename Iterator, typename F, typename... Fs >
void groupByImpl( Map & map, Iterator && current, F && f, Fs &&... fs )
{
groupByImpl( map[ f( *current ) ], current, std::forward< Fs >( fs )... );
}
}

/*
* groupBy
*/

template< typename Iterator, typename F, typename... Fs >
typename spyder::GroupBy< typename std::iterator_traits< Iterator >::value_type, F, Fs... >::return_type
groupBy( Iterator begin, Iterator const end, F && f, Fs &&... fs )
{
typename spyder::GroupBy< typename std::iterator_traits< Iterator >::value_type, F, Fs... >::return_type result;
for( /* empty */ ; begin != end ; ++ begin )
{
spyder::groupByImpl( result, begin, std::forward< F >( f ), std::forward< Fs >( fs )... );
}

return result;
}

#endif
38 changes: 38 additions & 0 deletions src/spyder/containers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#define SPYDER_CONTAINERS_CC

#include "containers.h"
#include "GroupBy.h"

#include <algorithm>
#include <cmath>
Expand All @@ -32,11 +33,16 @@
#include <set>
#include <stdexcept>
#include <string>
#include <vector>

#include "float.h"

namespace spyder {

bool Turn::operator<(const Turn &other) const {
return start < other.start;
}

bool TurnList::check_input(std::vector<Turn> turns_list) {
for (auto &turn : turns_list) {
if (turn.start > turn.end) {
Expand All @@ -58,6 +64,38 @@ TurnList::~TurnList() {
std::set<std::string>().swap(speaker_set);
}

void TurnList::merge_same_speaker_turns() {
std::vector<Turn> new_turns;
// Group the list of turns by speaker
std::map< std::string, std::vector<Turn> > const turns_by_speaker = groupBy( turns.begin(), turns.end(), []( Turn const & t ){ return t.spk; } );
for (auto &it: turns_by_speaker) {
std::vector<Turn> spk_turns = it.second;
// Sort the turns by start time
std::sort(spk_turns.begin(), spk_turns.end());
// Merge overlapping intervals in interval tree
std::vector<Turn> merged_turns;
double prev_end = -1;
for (auto &turn : spk_turns) {
if (prev_end < 0) {
prev_end = turn.end;
merged_turns.push_back(turn);
} else {
if (turn.start <= prev_end) {
prev_end = std::max(prev_end, turn.end);
merged_turns.back().end = prev_end;
} else {
prev_end = turn.end;
merged_turns.push_back(turn);
}
}
}
// Add merged turns to new list
new_turns.insert(new_turns.end(), merged_turns.begin(), merged_turns.end());
}
// Replace the old list with the new list
turns.swap(new_turns);
}

void TurnList::build_speaker_index() {
// Get set of speakers
for (auto &turn : turns) {
Expand Down
18 changes: 12 additions & 6 deletions src/spyder/containers.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,15 @@ const std::string HYP = "hyp";

// Stores speaker turns as provided in the input reference and hypothesis.
class Turn {
public:
std::string spk;
double start;
double end;
Turn(std::string spk, double start, double end) : spk(spk), start(start), end(end) {}
~Turn() {}
public:
std::string spk;
double start;
double end;
Turn(std::string spk, double start, double end) : spk(spk), start(start), end(end) {}
~Turn() {}
// Overload less than operator to enable sorting on start time
bool operator<(const Turn &other) const;

};

class TurnList {
Expand All @@ -67,6 +70,9 @@ class TurnList {
TurnList(std::vector<Turn> turns);
~TurnList();

// Merge overlapping turns from the same speaker in the list of turns.
void merge_same_speaker_turns();

// Build index of speakers. Each speaker is mapped to a natural number, i.e.,
// 0,1,2,.. and so on. This is needed to build the cost matrix and apply
// the Hungarian algorithm.
Expand Down
2 changes: 2 additions & 0 deletions src/spyder/der.cc
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ void compute_der_mapped(TurnList& ref, TurnList& hyp, Metrics& metrics, std::str
}

Metrics compute_der(TurnList& ref, TurnList& hyp, std::string regions) {
ref.merge_same_speaker_turns();
hyp.merge_same_speaker_turns();
ref.build_speaker_index();
hyp.build_speaker_index();
std::vector<std::vector<double>> cost_matrix = build_cost_matrix(ref, hyp);
Expand Down

0 comments on commit b87fda9

Please sign in to comment.