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

Bf/combine transformer embeddings #2558

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
36 changes: 36 additions & 0 deletions flair/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import typing
from abc import ABC, abstractmethod
from collections import Counter, defaultdict
from functools import lru_cache
from operator import itemgetter
from pathlib import Path
from typing import Callable, Dict, List, Optional, Union, cast
Expand Down Expand Up @@ -831,6 +832,41 @@ def clear_embeddings(self, embedding_names: List[str] = None):
for token in self:
token.clear_embeddings(embedding_names)

@lru_cache(maxsize=1) # cache last context, as training repeats calls
helpmefindaname marked this conversation as resolved.
Show resolved Hide resolved
def left_context(self, context_length: int, respect_document_boundaries: bool = True):
sentence = self
left_context: List[str] = []
while True:
sentence = sentence.previous_sentence()
if sentence is None:
break

if respect_document_boundaries and sentence.is_document_boundary:
break

left_context = [t.text for t in sentence.tokens] + left_context
if len(left_context) > context_length:
left_context = left_context[-context_length:]
break
return left_context

@lru_cache(maxsize=1) # cache last context, as training repeats calls
def right_context(self, context_length: int, respect_document_boundaries: bool = True):
sentence = self
right_context: List[str] = []
while True:
sentence = sentence.next_sentence()
if sentence is None:
break
if respect_document_boundaries and sentence.is_document_boundary:
break

right_context += [t.text for t in sentence.tokens]
if len(right_context) > context_length:
right_context = right_context[:context_length]
break
return right_context

def to_tagged_string(self, main_tag=None) -> str:
list = []
for token in self.tokens:
Expand Down
Loading