Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New experimental LSPL parser #10

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
7d765a8
Removed unnecessary explicit attributes in AttributeValue
apathism Jan 24, 2018
46446f5
Fixed template depth for GCC 7.2 in external dependency
apathism Jan 24, 2018
8bda79d
Fixed building on Linux with system-wide installed Boost
apathism Jan 31, 2018
07914b3
Another Boost build fix
apathism Jan 31, 2018
eef53a1
New parser version. Still needs a lot of testing, but works almost fine
apathism Apr 30, 2018
ccc8b05
Fixup commit no. 1
apathism May 7, 2018
9f90346
Memory leak fixed (with unique_ptr enforced)
apathism May 8, 2018
333d91e
Utilities build fixed
apathism May 29, 2018
9bff42b
Patterns bindings at the end of definition are fixed
apathism May 29, 2018
49a47af
Parser integer exceptions fixed
apathism May 30, 2018
94fb55d
Friendlier exception messages in lspl-console
apathism Jun 3, 2018
8dcdf57
Transforms are hopefully working. Enabled special characters (_ and -…
apathism Jun 3, 2018
92e605e
Merge branch 'master' into feature/new-parser
apathism Jan 9, 2019
3516cfd
Revert some changes in buildscripts made before upstream fixed everyt…
apathism Jan 9, 2019
bd35ae6
Fixed restrictions applying in permutations
apathism Sep 4, 2019
7ebe351
Fixed invalid pattern arguments detection
apathism Oct 3, 2019
79998e0
Ditched ptr_vector for STL smart pointers.
apathism Oct 7, 2019
f538be9
Fixed build warnings
apathism Oct 7, 2019
9562b0e
Removed some unused parser functions
apathism Oct 7, 2019
446bed6
Using speech part name as a pattern name is prohibited from now on
apathism Oct 7, 2019
44df69b
Memory leak?
apathism Oct 7, 2019
476b45a
Fix OrRestrictions
apathism Sep 26, 2020
0aa5ae5
Fix bindings in pattern arguments
apathism Oct 1, 2020
83bae04
Fix double freeing in agreement restrictions
apathism Oct 1, 2020
d361c66
Fix recursive patterns
apathism Oct 1, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion common/CMakeCommon.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ else(WIN32)

SET(MY_TARGETLIB_PREFIX "lib")
SET(MY_TARGETLIB_SUFFIX ".so")
endif(WIN32)
endif(WIN32)
10 changes: 8 additions & 2 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 2.6)
project(LSPL_CORE)

include(../common/CMakeCommon.cmake)
find_package(Boost COMPONENTS system)
find_package(Boost COMPONENTS system timer)

if(WIN32)
else(WIN32)
Expand Down Expand Up @@ -30,6 +30,7 @@ set(LSPL_CORE_SOURCES
src/main/lspl/morphology/AotMorphology.cpp

src/main/lspl/patterns/Alternative.cpp
src/main/lspl/patterns/matchers/BaseComparator.cpp
src/main/lspl/patterns/matchers/Context.cpp
src/main/lspl/patterns/matchers/LoopMatcher.cpp
src/main/lspl/patterns/matchers/Matcher.cpp
Expand All @@ -51,8 +52,13 @@ set(LSPL_CORE_SOURCES
src/main/lspl/patterns/restrictions/DictionaryRestriction.cpp
src/main/lspl/patterns/restrictions/AgreementRestriction.cpp
src/main/lspl/patterns/restrictions/AndRestriction.cpp
src/main/lspl/patterns/restrictions/NotRestriction.cpp
src/main/lspl/patterns/restrictions/OrRestriction.cpp
src/main/lspl/patterns/restrictions/SharedRestriction.cpp

src/main/lspl/patterns/parsers/CharacterSets.cpp
src/main/lspl/patterns/parsers/Functions.cpp

src/main/lspl/patterns/Pattern.cpp
src/main/lspl/patterns/PatternBuilder.cpp
src/main/lspl/utils/Conversion.cpp
Expand Down Expand Up @@ -150,7 +156,7 @@ set(LSPL_BENCHMARK_SOURCES

add_executable(lspl-benchmark ${LSPL_BENCHMARK_SOURCES})

target_link_libraries(lspl-benchmark lspl)
target_link_libraries(lspl-benchmark lspl ${Boost_TIMER_LIBRARY})

# Flags for effective error parsing

Expand Down
5 changes: 5 additions & 0 deletions core/src/main/lspl/base/BaseInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@
#define LSPL_REFCOUNT_CLASS(className) \
template<> lspl::uint lspl::base::IdentifiedObject<className>::count = 0;

namespace lspl {
typedef unsigned int uint;
typedef unsigned long long uint64;
}

#endif//_LSPL_BASE_BASEINTERNAL_H_
131 changes: 71 additions & 60 deletions core/src/main/lspl/morphology/Morphology.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,66 +25,77 @@ Morphology::~Morphology() {

}

struct AttributeInfo {
int bitsCount;
int bits[10];
const AttributeValue* values[10];
};

static AttributeInfo attributeInfos[10] = {
{
8,
{ rNominativ, rGenitiv, rDativ, rAccusativ, rInstrumentalis, rLocativ, rVocativ, rIndeclinable },
{ &AttributeValue::NOMINATIVE, &AttributeValue::GENITIVE, &AttributeValue::DATIVE, &AttributeValue::ACCUSATIVE, &AttributeValue::INSTRUMENTAL, &AttributeValue::PREPOSITIONAL, &AttributeValue::PREPOSITIONAL, &AttributeValue::UNINFLECTED }
},
{
2,
{ rSingular, rPlural },
{ &AttributeValue::SINGULAR, &AttributeValue::PLURAL }
},
{
3,
{ rMasculinum, rFeminum, rNeutrum },
{ &AttributeValue::MASCULINE, &AttributeValue::FEMININE, &AttributeValue::NEUTER }
},
{
2,
{ rComparative, rSuperlative },
{ &AttributeValue::COMPARATIVE, &AttributeValue::SUPERLATIVE }
},
{
3,
{ rPresentTense, rPastTense, rFutureTense },
{ &AttributeValue::PRESENT, &AttributeValue::PAST, &AttributeValue::FUTURE }
},
{
2,
{ rAnimative, rNonAnimative },
{ &AttributeValue::ANIMATE, &AttributeValue::INANIMATE }
},
{ // Форма
0,
{},
{}
},
{
1,
{ rImperative },
{ &AttributeValue::IMPERATIVE }
},
{
3,
{ rFirstPerson, rSecondPerson, rThirdPerson },
{ &AttributeValue::FIRST, &AttributeValue::SECOND, &AttributeValue::THIRD }
},
{
0,
{},
{}
}
};

AttributeKey Morphology::getAttributeKeyByValue(AttributeValue value) const {
if (value.type != AttributeType::INDEXED)
return AttributeKey::UNDEFINED;
for (uint i = 0; i < 10; ++i) {
for (uint j = 0; j < attributeInfos[i].bitsCount; ++j)
if ((*attributeInfos[i].values[j]) == value)
return AttributeKey(i + 1);
}
return AttributeKey::UNDEFINED;
}

AttributeValue Morphology::extractAttributeValue( uint64 attributeSet, AttributeKey key ) {
struct AttributeInfo {
int bitsCount;

int bits[10];
AttributeValue values[10];
};

static AttributeInfo attributeInfos[10] = {
{
8,
{ rNominativ, rGenitiv, rDativ, rAccusativ, rInstrumentalis, rLocativ, rVocativ, rIndeclinable },
{ AttributeValue::NOMINATIVE, AttributeValue::GENITIVE, AttributeValue::DATIVE, AttributeValue::ACCUSATIVE, AttributeValue::INSTRUMENTAL, AttributeValue::PREPOSITIONAL, AttributeValue::PREPOSITIONAL, AttributeValue::UNINFLECTED }
},
{
2,
{ rSingular, rPlural },
{ AttributeValue::SINGULAR, AttributeValue::PLURAL }
},
{
3,
{ rMasculinum, rFeminum, rNeutrum },
{ AttributeValue::MASCULINE, AttributeValue::FEMININE, AttributeValue::NEUTER }
},
{
2,
{ rComparative, rSuperlative },
{ AttributeValue::COMPARATIVE, AttributeValue::SUPERLATIVE }
},
{
3,
{ rPresentTense, rPastTense, rFutureTense },
{ AttributeValue::PRESENT, AttributeValue::PAST, AttributeValue::FUTURE }
},
{
2,
{ rAnimative, rNonAnimative },
{ AttributeValue::ANIMATE, AttributeValue::INANIMATE }
},
{ // Форма
0,
{},
{}
},
{
1,
{ rImperative },
{ AttributeValue::IMPERATIVE }
},
{
3,
{ rFirstPerson, rSecondPerson, rThirdPerson },
{ AttributeValue::FIRST, AttributeValue::SECOND, AttributeValue::THIRD }
},
{
0,
{},
{}
}
};

if ( key.index <= 0 || key.index > 10 ) // Если тип запрашиваемого аттрибута некорректен
return AttributeValue::UNDEFINED; // Возвращаем неопределенное значение
Expand All @@ -93,7 +104,7 @@ AttributeValue Morphology::extractAttributeValue( uint64 attributeSet, Attribute

for ( int i = 0; i < info.bitsCount; ++ i )
if ( attributeSet & ( ((uint64)1) << info.bits[ i ] ) )
return info.values[ i ];
return *(info.values[ i ]);

return AttributeValue::UNDEFINED;
}
Expand Down
5 changes: 5 additions & 0 deletions core/src/main/lspl/morphology/Morphology.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ class LSPL_EXPORT Morphology {
return result.release();
}

/**
* По признаку получить его вид. Работает только для индексных (не составных и не строковых) признаков
*/
virtual text::attributes::AttributeKey getAttributeKeyByValue(text::attributes::AttributeValue value) const;

virtual std::string getAttributesString( uint64 attValues ) = 0;

std::string upcase( const std::string & str ) { return upcase( str.c_str() ); }
Expand Down
9 changes: 8 additions & 1 deletion core/src/main/lspl/patterns/Alternative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,14 @@ void Alternative::dump( std::ostream & out, const std::string & tabs ) const {
getMatcher( i ).dump( out, tabs + "\t" );
}

out << "\n" << tabs << "] }";
out << "\n" << tabs << "], bindings = [\n";

for ( BindingMap::const_iterator it = bindings.begin(); it != bindings.end(); ++ it ) {
out << tabs << "\t" << it->first.getAbbrevation() << " : ";
it->second->dump(out);
out << '\n';
}
out << tabs << "] }";
}

} } // namespace lspl::patterns
4 changes: 2 additions & 2 deletions core/src/main/lspl/patterns/Alternative.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ class LSPL_EXPORT Alternative : public matchers::MatcherContainer {

public:

std::string transformSource;

/**
* Добавить связывания аттрибутов
*/
Expand Down Expand Up @@ -152,8 +154,6 @@ class LSPL_EXPORT Alternative : public matchers::MatcherContainer {
*/
std::string source;

std::string transformSource;

/**
* Список связываний аттрибутов шаблона
*/
Expand Down
31 changes: 19 additions & 12 deletions core/src/main/lspl/patterns/Pattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ void Pattern::dump( std::ostream & out, const std::string & tabs ) const {
out << "Pattern{ name = " << name << ", alternatives = [\n\t" << tabs;

bool first = true;
for( boost::ptr_vector<Alternative>::const_iterator altIt = alternatives.begin(); altIt != alternatives.end(); ++ altIt ) {
for( const std::unique_ptr<Alternative> &alt : alternatives ) {
if ( first ) {
first = false;
} else {
out << ",\n\t" << tabs;
}

altIt->dump( out, tabs + "\t" );
alt->dump( out, tabs + "\t" );
}

out << "\n" << tabs << "] }";
Expand All @@ -44,22 +44,29 @@ Alternative & Pattern::newAlternative( const std::string & source ) {

void Pattern::addAlternative( Alternative * alt ) {
alt->pattern = this;
alternatives.push_back( alt );
alternatives.emplace_back( alt );
}

void Pattern::addAlternatives( boost::ptr_vector<Alternative> & r ) {
for( Alternative & alt : r ) {
alt.pattern = this;
void Pattern::addAlternatives( std::vector<std::unique_ptr<Alternative>> & r ) {
for( const std::unique_ptr<Alternative> & alt : r ) {
alt->pattern = this;
}
alternatives.transfer( alternatives.end(), r.begin(), r.end(), r );
int oldSize = alternatives.size();
alternatives.resize(oldSize + r.size());
std::move(r.begin(), r.end(), alternatives.begin() + oldSize);
}

void Pattern::mergePattern ( Pattern &other ) {
addAlternatives(other.alternatives);
other.alternatives.clear();
}

void Pattern::updateDependencies() {
dependencies.clear();

for( const Alternative & alt : alternatives ) {
for( const std::unique_ptr<Alternative> & alt : alternatives ) {

for( const Pattern * ptr : alt.getDependencies() ) {
for( const Pattern * ptr : alt->getDependencies() ) {
bool found = false;

for( const Pattern * dep : dependencies ) {
Expand All @@ -82,7 +89,7 @@ void Pattern::removeDuplicateAlternatives() {
while ( i < alternatives.size() ) {
bool found = false;
for ( uint j = 0; j < i; ++ j ) {
if ( alternatives[i].equals( alternatives[j] ) ) {
if ( alternatives[i]->equals( *alternatives[j] ) ) {
found = true;
break;
}
Expand Down Expand Up @@ -137,14 +144,14 @@ std::string Pattern::getSource() const {
std::string result = "";

bool first = true;
for( boost::ptr_vector<Alternative>::const_iterator altIt = alternatives.begin(); altIt != alternatives.end(); ++ altIt ) {
for( const std::unique_ptr<Alternative> &alt : alternatives ) {
if ( first ) {
first = false;
} else {
result += " | ";
}

result += altIt->getSource();
result += alt->getSource();
}

return result;
Expand Down
15 changes: 10 additions & 5 deletions core/src/main/lspl/patterns/Pattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
#define _LSPL_PATTERNS_PATTERN_H_

#include <string>
#include <memory>
#include "Forward.h"
#include "../text/Forward.h"

#include <boost/ptr_container/ptr_vector.hpp>

namespace lspl { namespace patterns {

/**
Expand Down Expand Up @@ -44,7 +43,13 @@ class LSPL_EXPORT Pattern : public base::RefCountObject, public base::Identified
/**
* Добавить альтернативы к шаблону
*/
void addAlternatives( boost::ptr_vector<Alternative> & r );
void addAlternatives( std::vector<std::unique_ptr<Alternative>> & r );

/**
* Перенести все альтернативы шаблона other к текущему (подразумевается, что
* они имеют одно имя)
*/
void mergePattern ( Pattern &other );

/**
* Обновить список зависимостей альтернативы
Expand Down Expand Up @@ -86,7 +91,7 @@ class LSPL_EXPORT Pattern : public base::RefCountObject, public base::Identified
/**
* Получить список внешних альтернатив шаблона
*/
const boost::ptr_vector<Alternative> & getAlternatives() const {
const std::vector<std::unique_ptr<Alternative>> & getAlternatives() const {
return alternatives;
}

Expand All @@ -104,7 +109,7 @@ class LSPL_EXPORT Pattern : public base::RefCountObject, public base::Identified
/**
* Список альтернатив шаблона
*/
boost::ptr_vector<Alternative> alternatives;
std::vector<std::unique_ptr<Alternative>> alternatives;

/**
* Количество объектов в памяти
Expand Down
Loading