Skip to content

Commit

Permalink
Rearrange source files
Browse files Browse the repository at this point in the history
  • Loading branch information
rolczynski committed Oct 27, 2020
1 parent 17a5607 commit a7b0a15
Show file tree
Hide file tree
Showing 40 changed files with 68 additions and 28 deletions.
Empty file modified .gitignore
100644 → 100755
Empty file.
Empty file modified LICENSE
100644 → 100755
Empty file.
Empty file modified README.md
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion aspect_based_sentiment_analysis/__init__.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from .text_splitters import sentencizer

from . import plots
from .plots import explain
from .plots import display

from . import training
from . import recognizers
Expand Down
Empty file modified aspect_based_sentiment_analysis/alignment.py
100644 → 100755
Empty file.
25 changes: 22 additions & 3 deletions aspect_based_sentiment_analysis/aux_models.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,30 @@
from typing import List

import tensorflow as tf
import transformers
from transformers import TFPreTrainedModel

from .data_types import Pattern
from .data_types import TokenizedExample


def get_key_set(patterns: List[Pattern], n: int, k: int = 1):
"""
Parameters
----------
patterns
n
The number of elements in the key set.
k
The number of the sorted (from the most important) candidates
of the key sets.
Returns
-------
"""


class ReferenceRecognizer(ABC):
""" """

Expand All @@ -19,6 +37,8 @@ def __call__(
attention_grads: tf.Tensor
) -> bool:
""" """
if not aspect:
return True


class PatternRecognizer(ABC):
Expand All @@ -34,8 +54,7 @@ def __call__(
""" """


class BasicReferenceRecognizer(ReferenceRecognizer,
transformers.TFPreTrainedModel):
class BasicReferenceRecognizer(ReferenceRecognizer, TFPreTrainedModel):
"""
Briefly, it represents a text and an aspect as two vectors, and predicts
that a text relates to an aspect if the cosine similarity is bigger than
Expand Down
Empty file modified aspect_based_sentiment_analysis/data_types.py
100644 → 100755
Empty file.
Empty file modified aspect_based_sentiment_analysis/loads.py
100644 → 100755
Empty file.
Empty file modified aspect_based_sentiment_analysis/models.py
100644 → 100755
Empty file.
29 changes: 18 additions & 11 deletions aspect_based_sentiment_analysis/pipelines.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -258,18 +258,25 @@ def __call__(
return completed_subtask
return completed_task

def transform(
self,
text: str,
aspects: List[str]
) -> CompletedTask:
task = self.preprocess(text, aspects)
input_batch = self.encode(task.tokenized_examples)
# def transform(
# self,
# text: str,
# aspects: List[str]
# ) -> CompletedTask:
# task = self.preprocess(text, aspects)
# input_batch = self.encode(task.tokenized_examples)
# output_batch = self.predict(input_batch)
# reviews = self.professor.review(task, output_batch) \
# if self.professor else None
# completed_task = self.postprocess(task, output_batch, reviews)
# return completed_task

def transform(self, batch, mask: List[List[int]] = None):
tokenized_examples = self.tokenize(batch, mask)
input_batch = self.encode(tokenized_examples)
output_batch = self.predict(input_batch)
reviews = self.professor.review(task, output_batch) \
if self.professor else None
completed_task = self.postprocess(task, output_batch, reviews)
return completed_task
predictions = self.review(tokenized_examples, output_batch)
return predictions

def preprocess(self, text: str, aspects: List[str]) -> Task:
texts = self.text_splitter(text) if self.text_splitter else [text]
Expand Down
17 changes: 14 additions & 3 deletions aspect_based_sentiment_analysis/plots.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
from typing import List
from typing import Tuple
import numpy as np
from IPython.core.display import display as ipython_display
from IPython.core.display import HTML
from .data_types import PredictedExample
from .data_types import Pattern
from .data_types import Pattern, Review


def html_escape(text):
Expand Down Expand Up @@ -45,7 +45,8 @@ def highlight_pattern(pattern: Pattern) -> str:
return highlighted_text


def explain(example: PredictedExample):
def display_html(patterns: List[Pattern]):
# TODO
aspect = example.aspect_representation
texts = [f'Words connected with the "{example.aspect}" aspect: <br>']
texts.extend(highlight_sequence(aspect.tokens, aspect.look_at))
Expand All @@ -56,3 +57,13 @@ def explain(example: PredictedExample):
text = ' '.join(texts)
html_text = HTML(text)
return html_text


def display_patterns(patterns: List[Pattern]):
html_text = display_html(patterns)
return ipython_display(html_text)


def display(review: Review):
html_text = display_html(review.patterns)
return ipython_display(html_text)
8 changes: 4 additions & 4 deletions aspect_based_sentiment_analysis/professors.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
@dataclass
class _Professor(ABC):
""" """
ref_recognizer: ReferenceRecognizer = None
reference_recognizer: ReferenceRecognizer = None
pattern_recognizer: PatternRecognizer = None

def make_decision(
Expand Down Expand Up @@ -57,10 +57,10 @@ def review(
output_batch: OutputBatch
) -> Iterable[Review]:
for example, args in zip(task, output_batch):
is_reference = self.ref_recognizer(example, *args) \
if self.ref_recognizer else None
is_reference = self.reference_recognizer(example, *args) \
if self.reference_recognizer else None
patterns = self.pattern_recognizer(example, *args) \
if self.pattern_recognizer and is_reference is not False else\
if self.pattern_recognizer and is_reference is not False else \
None
review = Review(is_reference, patterns)
yield review
Expand Down
11 changes: 7 additions & 4 deletions aspect_based_sentiment_analysis/recognizers.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class AttentionGradientProduct(PatternRecognizer):
percentile of the total information. Default 80% of weights magnitude.
"""
information_in_patterns: int = 80
is_pattern_scaled: bool = False

def __call__(
self,
Expand All @@ -76,8 +77,8 @@ def __call__(
# threshold = 0.05
# round_decimals = 2
#
# product = attentions * attention_grads
# product = tf.abs(product)
# product = attentions * tf.abs(attention_grads)

# product = tf.reduce_sum(product, axis=(0, 1))
# attention_grads = alignment.merge_input(
# product, alignment=example.alignment)
Expand All @@ -88,8 +89,10 @@ def __call__(
#
# mixtures = product[text_ids, :][:, text_ids]
# mixtures /= np.max(mixtures + 1e-9, axis=1).reshape(-1, 1)
# np.fill_diagonal(mixtures, 1)
# mixtures *= w.reshape(-1, 1)
# np.fill_diagonal(mixtures, 1) # as well, we could use max on diagonal
# mixtures /= L1 norm
# if self.is_pattern_scaled:
# mixtures *= w.reshape(-1, 1)
#
# mixtures = np.where(mixtures > threshold, mixtures, 0)
# mixtures = np.round(mixtures, decimals=round_decimals)
Expand Down
Empty file modified aspect_based_sentiment_analysis/text_splitters.py
100644 → 100755
Empty file.
Empty file modified aspect_based_sentiment_analysis/training/__init__.py
100644 → 100755
Empty file.
Empty file modified aspect_based_sentiment_analysis/training/callbacks.py
100644 → 100755
Empty file.
Empty file modified aspect_based_sentiment_analysis/training/classifier.py
100644 → 100755
Empty file.
Empty file modified aspect_based_sentiment_analysis/training/data_types.py
100644 → 100755
Empty file.
Empty file modified aspect_based_sentiment_analysis/training/datasets.py
100644 → 100755
Empty file.
Empty file modified aspect_based_sentiment_analysis/training/errors.py
100644 → 100755
Empty file.
Empty file modified aspect_based_sentiment_analysis/training/metrics.py
100644 → 100755
Empty file.
Empty file modified aspect_based_sentiment_analysis/training/routines.py
100644 → 100755
Empty file.
Empty file modified aspect_based_sentiment_analysis/utils.py
100644 → 100755
Empty file.
Empty file modified data/semeval/adapter.py
100644 → 100755
Empty file.
Empty file modified environment.yml
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion examples/pattens.ipynb
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
}
],
"source": [
"html = absa.explain(example)\n",
"html = absa.display_html(example)\n",
"display(html)"
],
"metadata": {
Expand Down
Empty file modified examples/patterns.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified examples/train_classifier.py
100644 → 100755
Empty file.
Empty file modified setup.py
100644 → 100755
Empty file.
Empty file modified tests/absa/test_alignment.py
100644 → 100755
Empty file.
Empty file modified tests/absa/test_dataset.py
100644 → 100755
Empty file.
Empty file modified tests/absa/test_loads.py
100644 → 100755
Empty file.
Empty file modified tests/absa/test_pipeline.py
100644 → 100755
Empty file.
Empty file modified tests/absa/test_recognizers.py
100644 → 100755
Empty file.
Empty file modified tests/absa/test_text_splitters.py
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion tests/conftest.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def pytest_configure(config):

def pytest_collection_modifyitems(config, items):
if config.getoption("--run-slow"):
# --run-run-slow given in cli: do not skip slow checks
# --run-slow given in cli: do not skip slow checks
return
reason_desc = "need --run-slow option to run"
skip_sanity_check = pytest.mark.skip(reason=reason_desc)
Expand Down
Empty file modified tests/data/test_semeval.py
100644 → 100755
Empty file.
Empty file modified tests/test_performance.py
100644 → 100755
Empty file.
Empty file modified tests/training/test_callbacks.py
100644 → 100755
Empty file.
Empty file modified tests/training/test_sanity_check.py
100644 → 100755
Empty file.

0 comments on commit a7b0a15

Please sign in to comment.