ScalaConsultants / Aspect-Based-Sentiment-Analysis

💭 Aspect-Based-Sentiment-Analysis: Transformer & Explainable ML (TensorFlow)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

AttributeError with Tokenizer

adriguerra opened this issue · comments

I'm trying to reproduce the example in the README.

name = 'absa/classifier-rest-0.2'
model = absa.BertABSClassifier.from_pretrained(name)
tokenizer = absa.BertTokenizer.from_pretrained(name)
professor = absa.Professor()     # Explained in detail later on.
text_splitter = absa.sentencizer()  # The English CNN model from SpaCy.
nlp = absa.Pipeline(model, tokenizer, professor, text_splitter)

But I get an AttributeError with the tokenizer.

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-9-c6e986c7be44> in <module>
      1 name = 'absa/classifier-rest-0.2'
      2 model = absa.BertABSClassifier.from_pretrained(name)
----> 3 tokenizer = absa.BertTokenizer.from_pretrained(name)
      4 professor = absa.Professor()     # Explained in detail later on.
      5 text_splitter = absa.sentencizer()  # The English CNN model from SpaCy.

AttributeError: module 'aspect_based_sentiment_analysis' has no attribute 'BertTokenizer'

Could you also clarify how the professor works. The article is missing the hyperlink in the README: "In the article [here], we discuss in detail how the model and the professor work"

Thanks in advance.

This fixes the first issue:

from transformers import BertTokenizer
tokenizer = BertTokenizer.from_pretrained(model_name)

Thank you for your solution. Now I get another error:

Traceback (most recent call last):
File "/Users/petar.ulev/Documents/prepare_sentiment_data/spacystuff.py", line 29, in
task = nlp.preprocess(text=text, aspects=aspects)
File "/Users/petar.ulev/Documents/prepare_sentiment_data/venv/lib/python3.8/site-packages/aspect_based_sentiment_analysis/pipelines.py", line 213, in preprocess
spans = self.text_splitter(text) if self.text_splitter else [text]
File "/Users/petar.ulev/Documents/prepare_sentiment_data/venv/lib/python3.8/site-packages/aspect_based_sentiment_analysis/text_splitters.py", line 17, in wrapper
sentences = [sent.string.strip() for sent in doc.sents]
File "/Users/petar.ulev/Documents/prepare_sentiment_data/venv/lib/python3.8/site-packages/aspect_based_sentiment_analysis/text_splitters.py", line 17, in
sentences = [sent.string.strip() for sent in doc.sents]
AttributeError: 'spacy.tokens.span.Span' object has no attribute 'string'

Did you experience it as well?

Can you try this?

sentences = [sent.text.strip() for sent in doc.sents]