forked from andre-martins/TurboParser
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENH Created an abstract layer for sequence models, and separated it f…
…rom the tagger.
- Loading branch information
1 parent
01b5c7a
commit bedee9d
Showing
39 changed files
with
862 additions
and
253 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright (c) 2012-2013 Andre Martins | ||
// All Rights Reserved. | ||
// | ||
// This file is part of TurboParser 2.1. | ||
// | ||
// TurboParser 2.1 is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Lesser General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// TurboParser 2.1 is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public License | ||
// along with TurboParser 2.1. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
#include "SequenceDictionary.h" | ||
#include "SequencePipe.h" | ||
#include <algorithm> | ||
|
||
void SequenceDictionary::CreateTagDictionary(SequenceReader *reader) { | ||
LOG(INFO) << "Creating tag dictionary..."; | ||
vector<int> tag_freqs; | ||
|
||
// Go through the corpus and build the label dictionary, | ||
// counting the frequencies. | ||
reader->Open(pipe_->GetOptions()->GetTrainingFilePath()); | ||
SequenceInstance *instance = | ||
static_cast<SequenceInstance*>(reader->GetNext()); | ||
while (instance != NULL) { | ||
int instance_length = instance->size(); | ||
for (int i = 0; i < instance_length; ++i) { | ||
int id; | ||
|
||
// Add tag to alphabet. | ||
id = tag_alphabet_.Insert(instance->GetTag(i)); | ||
if (id >= tag_freqs.size()) { | ||
CHECK_EQ(id, tag_freqs.size()); | ||
tag_freqs.push_back(0); | ||
} | ||
++tag_freqs[id]; | ||
} | ||
delete instance; | ||
instance = static_cast<SequenceInstance*>(reader->GetNext()); | ||
} | ||
reader->Close(); | ||
tag_alphabet_.StopGrowth(); | ||
|
||
LOG(INFO) << "Number of tags: " << tag_alphabet_.size(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Copyright (c) 2012-2013 Andre Martins | ||
// All Rights Reserved. | ||
// | ||
// This file is part of TurboParser 2.1. | ||
// | ||
// TurboParser 2.1 is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Lesser General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// TurboParser 2.1 is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public License | ||
// along with TurboParser 2.1. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
#ifndef SEQUENCEDICTIONARY_H_ | ||
#define SEQUENCEDICTIONARY_H_ | ||
|
||
#include "Dictionary.h" | ||
#include "TokenDictionary.h" | ||
#include "SerializationUtils.h" | ||
|
||
class Pipe; | ||
|
||
class SequenceDictionary : public Dictionary { | ||
public: | ||
SequenceDictionary() {} | ||
SequenceDictionary(Pipe* pipe) : pipe_(pipe) {} | ||
virtual ~SequenceDictionary() { Clear(); } | ||
|
||
virtual void Clear() { | ||
// Don't clear token_dictionary, since this class does not own it. | ||
tag_alphabet_.clear(); | ||
} | ||
|
||
virtual void Save(FILE *fs) { | ||
if (0 > tag_alphabet_.Save(fs)) CHECK(false); | ||
} | ||
|
||
void Load(FILE *fs) { | ||
if (0 > tag_alphabet_.Load(fs)) CHECK(false); | ||
tag_alphabet_.BuildNames(); | ||
} | ||
|
||
void AllowGrowth() { token_dictionary_->AllowGrowth(); } | ||
void StopGrowth() { token_dictionary_->StopGrowth(); } | ||
|
||
virtual void CreateTagDictionary(SequenceReader *reader); | ||
|
||
void BuildTagNames() { | ||
tag_alphabet_.BuildNames(); | ||
} | ||
|
||
const string &GetTagName(int tag) const { | ||
return tag_alphabet_.GetName(tag); | ||
} | ||
|
||
int GetBigramLabel(int left_tag, int tag) { | ||
CHECK_GE(left_tag, -1); | ||
CHECK_GE(tag, -1); | ||
//return (left_tag * tag_alphabet_.size() + tag); | ||
return ((1 + left_tag) * (1 + tag_alphabet_.size()) + (1 + tag)); | ||
} | ||
|
||
int GetTrigramLabel(int left_left_tag, int left_tag, int tag) { | ||
CHECK_GE(left_left_tag, -1); | ||
CHECK_GE(left_tag, -1); | ||
CHECK_GE(tag, -1); | ||
//return (left_tag * left_tag * tag_alphabet_.size() + | ||
// left_tag * tag_alphabet_.size() + tag); | ||
return ((1 + left_left_tag) * (1 + tag_alphabet_.size()) * | ||
(1 + tag_alphabet_.size()) + | ||
(1 + left_tag) * (1 + tag_alphabet_.size()) + (1 + tag)); | ||
} | ||
|
||
TokenDictionary *GetTokenDictionary() const { return token_dictionary_; } | ||
void SetTokenDictionary(TokenDictionary *token_dictionary) { | ||
token_dictionary_ = token_dictionary; | ||
} | ||
|
||
const Alphabet &GetTagAlphabet() const { return tag_alphabet_; }; | ||
|
||
protected: | ||
Pipe *pipe_; | ||
TokenDictionary *token_dictionary_; | ||
Alphabet tag_alphabet_; | ||
}; | ||
|
||
#endif /* SEQUENCEDICTIONARY_H_ */ |
Oops, something went wrong.