コード例 #1
0
def test_load():
    nlp = load('absa/classifier-rest-0.1')
    text = ("We are great fans of Slack, but we wish the subscriptions "
            "were more accessible to small startups.")
    slack, price = nlp(text, aspects=['slack', 'price'])
    assert slack.sentiment == Sentiment.positive
    assert price.sentiment == Sentiment.negative
コード例 #2
0
def single_row_sents(tweet):
    '''
    Parameters:
        tweet:
            Type: String
            Tweet text.
    Preforms ABSA on a single tweet.
    '''
    topics = getTopics()
    aspectsArray = []
    for key in topics.keys():
        for item in topics[key]:
            aspectsArray.append(item)

    nlp = absa.load()
    for i in range(10):
        print('')

    resultsDict = {}

    print(tweet)

    results = nlp(tweet, aspects=aspectsArray)
    print(results)
    for term in aspectsArray:
        if term in tweet.lower():
            if results[term].sentiment == absa.Sentiment.negative:
                resultsDict[term] = 1
            elif results[term].sentiment == absa.Sentiment.positive:
                resultsDict[term] = 2
        else:
            resultsDict[term] = 0

    return pd.DataFrame([resultsDict])
def experiment(models: Dict[str, str]):
    np.random.seed(0)
    utils.setup_logger(HERE / 'logs' / 'recognition-key-token.log')
    logger.info('Begin Evaluation: the Key Token Recognition')

    for is_test, domain in itertools.product([False, True],
                                             ['restaurant', 'laptop']):
        name = models[domain]
        nlp = absa.load(name)

        y_ref, y_new, mask = retrieve_labels(nlp, domain, is_test)
        max_score = sum(mask) / len(mask)
        max_score_matrix = confusion_matrix(y_ref, y_new)

        random = extension.RandomPatternRecognizer()
        attention_1 = extension.AttentionPatternRecognizer(max_patterns=5,
                                                           is_scaled=False,
                                                           add_diagonal=False)
        attention_2 = extension.AttentionPatternRecognizer(max_patterns=5,
                                                           is_scaled=True,
                                                           add_diagonal=False)
        attention_3 = extension.AttentionPatternRecognizer(max_patterns=5,
                                                           is_scaled=True,
                                                           add_diagonal=True)
        gradient = extension.GradientPatternRecognizer(max_patterns=5)
        basic = absa.BasicPatternRecognizer(max_patterns=5)
        recognizers = [
            random, attention_1, attention_2, attention_3, gradient, basic
        ]

        results = []
        for recognizer in recognizers:
            nlp.professor.pattern_recognizer = recognizer
            result = evaluate(nlp, domain, is_test, repr(recognizer))
            results.append(result)

        logger.info(
            f'{domain.upper()} {"TEST" if is_test else "TRAIN"} DOMAIN\n'
            f'Examples that have at least one key token: '
            f'{max_score:.4f}\ny_ref means a prediction without a mask, '
            f'and y_new with a single mask.\n'
            f'Confusion Matrix (y_ref, y_new):\n{max_score_matrix}\n\n'
            f'Random Pattern Recognizer\n'
            f'Acc.: {results[0][0] / max_score:.4f} ({results[0][0]:.4f})\n'
            f'Confusion Matrix (y_ref, y_new):\n{results[0][1]}\n\n'
            f'Attention Pattern Recognizer (without scaling and diagonal)\n'
            f'Acc.: {results[1][0] / max_score:.4f} ({results[1][0]:.4f})\n'
            f'Confusion Matrix (y_ref, y_new):\n{results[1][1]}\n\n'
            f'Attention Pattern Recognizer (without diagonal)\n'
            f'Acc.: {results[2][0] / max_score:.4f} ({results[2][0]:.4f})\n'
            f'Confusion Matrix (y_ref, y_new):\n{results[2][1]}\n\n'
            f'Attention Pattern Recognizer\n'
            f'Acc.: {results[3][0] / max_score:.4f} ({results[3][0]:.4f})\n'
            f'Confusion Matrix (y_ref, y_new):\n{results[3][1]}\n\n'
            f'Gradient Pattern Recognizer\n'
            f'Acc.: {results[4][0] / max_score:.4f} ({results[4][0]:.4f})\n'
            f'Confusion Matrix (y_ref, y_new):\n{results[4][1]}\n\n'
            f'Basic Pattern Recognizer\n'
            f'Acc.: {results[5][0] / max_score:.4f} ({results[5][0]:.4f})\n'
            f'Confusion Matrix (y_ref, y_new):\n{results[5][1]}\n\n')
コード例 #4
0
def single_user_sents(tweets):
    '''
    Parameters:
        tweets:
            Type: 1 Column<String> Dataframe
            List of tweets.
    Preforms ABSA on tweets in a dataframe, for use within the interface. 
    '''
    print("Analyzing....")
    print(tweets)
    topics = getTopics()
    aspectsArray = []
    for key in topics.keys():
        for item in topics[key]:
            aspectsArray.append(item)

    nlp = absa.load()
    os.system('cls')

    myResults = []

    for tweet in tweets:
        resultsDict = {}
        results = nlp(tweet, aspects=aspectsArray)
        for term in aspectsArray:
            if term in tweet.lower():
                if results[term].sentiment == absa.Sentiment.negative:
                    resultsDict[term] = 1
                elif results[term].sentiment == absa.Sentiment.positive:
                    resultsDict[term] = 2
            else:
                resultsDict[term] = 0
        myResults.append(resultsDict)

    return pd.DataFrame(myResults)
コード例 #5
0
def evaluate_with_wrong_aspect(domain: str, seed: int) -> np.ndarray:
    name = PRETRAINED_MODEL_NAMES[domain]
    dataset = build_dataset(domain, seed)
    nlp = absa.load(name)

    metric = absa.training.ConfusionMatrix(num_classes=3)
    confusion_matrix = nlp.evaluate(dataset, metric, batch_size=32)
    confusion_matrix = confusion_matrix.numpy()
    return confusion_matrix
コード例 #6
0
def beginCache(filename):
    '''
    Parameters:
        filename:
            Type: String
            Filename
    When given a number of lines to run on, runs ABSA on said number of needed rows. For a more in-depth
    description of ABSA functionality within the project, see xgboost_SA_attempt2.aspectifyv2. 
    '''

    topics = getTopics()
    aspectsArray = []

    if not filename:
        filename = glob.glob('./data/user_data/*.csv')[0]
    else:
        filename = './data/user_data/' + filename
    data = pd.read_csv(filename)

    data = data.drop(data.columns[[0, 1, 3, 4, 6]], axis=1)
    data = data.dropna()
    data.columns = ['tweet', 'party']
    data.reset_index(drop=True, inplace=True)
    for key in topics.keys():
        for item in topics[key]:
            aspectsArray.append(item)
            data[item] = np.nan

    nlp = absa.load()
    for i in range(10):
        print('')

    counter = 0
    locs = []

    for index, row in data.iterrows():
        needToRun = []
        for aspect in aspectsArray:
            if np.isnan(pd.to_numeric(row[aspect], errors='coerce')):
                needToRun.append(aspect)
        if len(needToRun) > 0:
            if detect(row['tweet']) == 'en':
                results = nlp(row['tweet'], aspects=needToRun)
                for term in needToRun:
                    if term in row['tweet'].lower():
                        if results[term].sentiment == absa.Sentiment.negative:
                            data.at[index, term] = 1
                        elif results[
                                term].sentiment == absa.Sentiment.positive:
                            data.at[index, term] = 2
                    else:
                        data.at[index, term] = 0
            else:
                locs.append(index)
        data.to_csv('./data/user_data/completedSentiments.csv',
                    index=None,
                    mode='w')
コード例 #7
0
def evaluate(domain: str):
    name = PRETRAINED_MODEL_NAMES[domain]
    dataset = absa.load_examples('semeval', domain, test=True)
    nlp = absa.load(name)

    metric = absa.training.ConfusionMatrix(num_classes=3)
    confusion_matrix = nlp.evaluate(dataset, metric, batch_size=32)
    confusion_matrix = confusion_matrix.numpy()
    return confusion_matrix
コード例 #8
0
def evaluate_without_aspect(domain: str) -> np.ndarray:
    name = PRETRAINED_MODEL_NAMES[domain]
    tokenizer = transformers.BertTokenizer.from_pretrained(name)
    dataset = build_dataset(domain, unknown_token=tokenizer.unk_token)
    nlp = absa.load(name)

    metric = absa.training.ConfusionMatrix(num_classes=3)
    confusion_matrix = nlp.evaluate(dataset, metric, batch_size=32)
    confusion_matrix = confusion_matrix.numpy()
    return confusion_matrix
def test_semeval_classification_restaurant():
    examples = absa.load_examples(dataset='semeval',
                                  domain='restaurant',
                                  test=True)
    nlp = absa.load('absa/classifier-rest-0.2')
    metric = ConfusionMatrix(num_classes=3)
    confusion_matrix = nlp.evaluate(examples, metric, batch_size=32)
    confusion_matrix = confusion_matrix.numpy()
    accuracy = np.diagonal(confusion_matrix).sum() / confusion_matrix.sum()
    assert round(accuracy, 4) >= .8517
コード例 #10
0
def evaluate_with_enriched_text(domain: str, template: str,
                                template_sent: Sentiment,
                                seed: int) -> np.ndarray:
    name = PRETRAINED_MODEL_NAMES[domain]
    dataset = build_dataset(domain, template, template_sent, seed)
    nlp = absa.load(name)

    metric = absa.training.ConfusionMatrix(num_classes=3)
    confusion_matrix = nlp.evaluate(dataset, metric, batch_size=32)
    confusion_matrix = confusion_matrix.numpy()
    return confusion_matrix
コード例 #11
0
def beginCache(ideal):
    '''
    When given a number of lines to run on, runs ABSA on said number of needed rows. For a more in-depth
    description of ABSA functionality within the project, see xgboost_SA_attempt2.aspectifyv2. 
    '''
    topics = getTopics()

    aspectsArray = []

    if path.exists('sentiments.csv'):
        data = pd.read_csv('sentiments.csv')
        for key in topics.keys():
            for item in topics[key]:
                aspectsArray.append(item)
                if item not in data.columns:
                    data[item] = np.nan
    else:
        data = pd.read_csv('../data/data12_04_2020_22-58.csv')
        data = data.drop(data.columns[[0, 1, 3, 4, 6]], axis=1)
        data = data.dropna()
        data.columns = ['tweet', 'party']
        data.reset_index(drop=True, inplace=True)
        for key in topics.keys():
            for item in topics[key]:
                aspectsArray.append(item)
                data[item] = np.nan

    nlp = absa.load()
    for i in range(10):
        print('')

    counter = 0

    for index, row in data.iterrows():
        if counter < ideal:
            needToRun = []
            for aspect in aspectsArray:
                if np.isnan(pd.to_numeric(row[aspect], errors='coerce')):
                    needToRun.append(aspect)
            if len(needToRun) > 0:
                results = nlp(row['tweet'], aspects=needToRun)
                for term in needToRun:
                    if term in row['tweet'].lower():
                        if results[term].sentiment == absa.Sentiment.negative:
                            data.at[index, term] = 1
                        elif results[
                                term].sentiment == absa.Sentiment.positive:
                            data.at[index, term] = 2
                    else:
                        data.at[index, term] = 0
                counter = counter + 1

    data.to_csv('sentiments.csv', index=None, mode='w')
def test_inference():
    sentencier = absa.sentencizer()
    nlp = absa.load(text_splitter=sentencier)
    text = ("My wife and I and our 4 year old daughter stopped here "
            "Friday-Sunday. We arrived about midday and there was a queue to "
            "check in, which seemed to take ages to go down. Our check is was "
            "fairly quick but not sure why others in front of us took so "
            "long. Our room was \"ready\" although the sofa bed for our "
            "daughter hadn't been made.")
    aspects = [
        "reception", "bed", "service", "staff", "location", "public",
        "breakfast"
    ]
    nlp(text, aspects)
コード例 #13
0
def test_basic_reference_recognizer():
    text = 'the automobile is so cool and the service is prompt and curious.'
    examples = [
        Example(text, 'breakfast'),
        Example(text, 'service'),
        Example(text, 'car')
    ]
    recognizer = BasicReferenceRecognizer(weights=(-0.025, 44))
    nlp = absa.load('absa/classifier-rest-0.2',
                    reference_recognizer=recognizer)
    predictions = nlp.transform(examples)
    prediction_1, prediction_2, prediction_3 = predictions
    assert not prediction_1.review.is_reference
    assert prediction_2.review.is_reference
    assert prediction_3.review.is_reference
コード例 #14
0
def explainations():
    recognizer = absa.probing.AttentionGradientProduct()
    nlp = absa.load('absa/classifier-rest-0.1', pattern_recognizer=recognizer)
    text = ("I think the President is doing an excellent job handling the ongoing issues with China. Despite the nation's desperate "
    "attempts to interfere with our economy, our military has protected important trade regions and ensured financial success for years to come.")
            
    task = nlp(text, aspects=['economics', 'police', 'foreign_policy', "president", "immigration", "military", "abortion"])

    econ, poli, fp, pres, immi, milit, abor = task.batch
    econ_scores = np.round(econ.scores, decimals=3)

    print(f'Sentiment for "slack": {repr(econ.sentiment)}')
    print(f'Scores (neutral/negative/positive): {econ_scores}')

    html = absa.probing.explain(econ)
    display(html)
コード例 #15
0
def main():
	nlp = absa.load()
	print("Model Loaded")
	df = pd.read_csv('/home/luv/Downloads/Thesis project-20200829T070727Z-001/Thesis project/datasets/WOF_split_into_sentences.csv')
	test_sentences = list(df['Sentences'])
	keys = ['time','stage','minister','research','defence','slv','missiles','launch','technology','work','rocket','rameswaram','sarabhai','development','project','space','brahm']
	with open('/home/luv/Aspect-Based-Sentiment-Analysis/Custom/Results.csv',"w") as f:
		writer = csv.writer(f)
		writer.writerow(['word','Pos','Neu','Neg'])
		for key in keys:
			print("Using key : ",key)
			all_sentiments = sentim(key,test_sentences,nlp)
			row = make_row(key,all_sentiments)
			writer.writerow(row)	

	df = pd.read_csv('/home/luv/Aspect-Based-Sentiment-Analysis/Custom/Results.csv')
	print(df)
コード例 #16
0
def inputs(request):  # The cache function uses the `request` parameter.
    nlp = absa.load('absa/classifier-rest-0.1')
    text = ("We are great fans of Slack, but we wish the subscriptions "
            "were more accessible to small startups.")
    aspect = 'slack'
    example = Example(text, aspect)
    tokenized_examples = nlp.tokenize(examples=[example])
    input_batch = nlp.encode(tokenized_examples)
    output_batch = nlp.predict(input_batch)
    outputs = [tensor[0] for tensor in astuple(output_batch)]

    # Covert the tokenized example and EagerTensor's to the native python
    # objects to facilitate the serialization process.
    tokenized_example = tokenized_examples[0]
    example_dict = asdict(tokenized_example)
    model_outputs_native = [tensor.numpy().tolist() for tensor in outputs]
    return example_dict, model_outputs_native
def experiment(models: Dict[str, str], save_confusion_matrix: bool = True):
    utils.setup_logger(HERE / 'logs' / 'recognition-key-token-pair.log')
    logger.info('Begin Evaluation: the Key Token Pair Recognition')

    for domain in ['restaurant', 'laptop']:
        name = models[domain]
        nlp = absa.load(name)

        y_ref, y_new, mask = retrieve_labels(nlp, domain, parts=10)
        max_score = sum(mask) / len(mask)
        max_score_matrix = confusion_matrix(y_ref, y_new)

        random = extension.RandomPatternRecognizer()
        attention = extension.AttentionPatternRecognizer(max_patterns=5)
        gradient = extension.GradientPatternRecognizer(max_patterns=5)
        basic = absa.BasicPatternRecognizer(max_patterns=5)
        recognizers = [random, attention, gradient, basic]

        results = []
        for recognizer in recognizers:
            nlp.professor.pattern_recognizer = recognizer
            result = evaluate(nlp, domain, repr(recognizer))
            results.append(result)

        if save_confusion_matrix:
            plots.confusion_matrix.save_figure(results, domain)

        logger.info(
            f'{domain.upper()} DOMAIN\n'
            f'Examples that have at least one key token pair: '
            f'{max_score:.4f}\ny_ref means a prediction without a mask, '
            f'and y_new with a single mask.\n'
            f'Confusion Matrix (y_ref, y_new):\n{max_score_matrix}\n\n'
            f'Random Pattern Recognizer\n'
            f'Acc.: {results[0][0]/max_score:.4f} ({results[0][0]:.4f})\n'
            f'Confusion Matrix (y_ref, y_new):\n{results[0][1]}\n\n'
            f'Attention Pattern Recognizer\n'
            f'Acc.: {results[1][0]/max_score:.4f} ({results[1][0]:.4f})\n'
            f'Confusion Matrix (y_ref, y_new):\n{results[1][1]}\n\n'
            f'Gradient Pattern Recognizer\n'
            f'Acc.: {results[2][0]/max_score:.4f} ({results[2][0]:.4f})\n'
            f'Confusion Matrix (y_ref, y_new):\n{results[2][1]}\n\n'
            f'Basic Pattern Recognizer\n'
            f'Acc.: {results[3][0]/max_score:.4f} ({results[3][0]:.4f})\n'
            f'Confusion Matrix (y_ref, y_new):\n{results[3][1]}\n\n')
コード例 #18
0
def run(df, i):
    nlp = absa.load()

    df['Seat Sentiment'] = df['Comment'].apply(
        lambda x: infer_seat(nlp, x, ['seat', 'space', 'legroom']))
    df['Staff Sentiment'] = df['Comment'].apply(
        lambda x: infer_staff(nlp, x, ['staff', 'crew', 'attendants']))
    df['Food Sentiment'] = df['Comment'].apply(
        lambda x: infer_food(nlp, x, ['food', 'meal']))
    df['Baggage Sentiment'] = df['Comment'].apply(
        lambda x: infer_baggage(nlp, x, ['baggage', 'check in']))
    df['Time Sentiment'] = df['Comment'].apply(
        lambda x: infer_time(nlp, x, ['time', 'cancelled', 'speed']))
    df['Value for Money Sentiment'] = df['Comment'].apply(
        lambda x: infer_value(nlp, x, ['price', 'value']))

    df = df.drop(['Comment', 'Date', 'Class'], 1)
    df.to_csv('data/results/sentiments' + str(i + 1) + '.csv')
コード例 #19
0
ファイル: hybrid_SA.py プロジェクト: topliftarm/hon450-451
def aspectify(topics, dataframe, writeToFile):
    '''
    Parameters:
        topics:
            Type = Dict
            Contains all topics and keywords.
        dataframe:
            type = pandas.DataFrame
            Contains all text for a given dataset.
        writeToFile: 
            Type = Boolean
            If true, write to file, no return statement. 
            If false, return the ABSA results in array form.
    Runs ABSA on all texts to find sentiments. Uses keywords to determine overall sentiment for class. 

    Depreciated.
    '''
    aspectsArray = topics.keys()
    nlp = absa.load()
    overall = []

    for row in range(len(dataframe)):
        rowIQ = dataframe['tweet'][row]
        results = nlp(rowIQ, aspects=aspectsArray)
        mySents = []
        for key in aspectsArray:
            haveFound = False
            for term in topics[key]:
                if term in rowIQ and not haveFound:
                    if results[key].sentiment == absa.Sentiment.negative:
                        mySents.append(1)
                    elif results[key].sentiment == absa.Sentiment.positive:
                        mySents.append(2)
                    haveFound = True
            if not haveFound:
                mySents.append(0)
        overall.append(mySents)

    overall = np.asarray(overall)

    if writeToFile:
        np.savetxt('sentiments.txt', overall)
    return overall
コード例 #20
0
def test_basic_pattern_recognizer():
    text = ("We are great fans of Slack, but we wish the subscriptions "
            "were more accessible to small startups.")
    example = Example(text, aspect='price')
    recognizer = BasicPatternRecognizer()
    nlp = absa.load('absa/classifier-rest-0.2', pattern_recognizer=recognizer)
    predictions = nlp.transform([example])
    prediction = next(predictions)
    assert isinstance(prediction, PredictedExample)
    assert not prediction.review.is_reference
    assert len(prediction.review.patterns) == 5
    pattern, *_ = prediction.review.patterns
    assert pattern.importance == 1
    assert list(zip(pattern.tokens, pattern.weights)) == \
        [('we', 0.25), ('are', 0.44), ('great', 0.88), ('fans', 0.92),
         ('of', 0.2), ('slack', 0.27), (',', 0.46), ('but', 1.0), ('we', 0.36),
         ('wish', 0.95), ('the', 0.16), ('subscriptions', 0.39), ('were', 0.23),
         ('more', 0.33), ('accessible', 0.24), ('to', 0.13), ('small', 0.14),
         ('startups', 0.24), ('.', 0.28)]
コード例 #21
0
def inputs():
    nlp = absa.load('absa/classifier-rest-0.1')
    text = ("We are great fans of Slack, but we wish the subscriptions "
            "were more accessible to small startups.")
    aspect = 'slack'
    example = Example(text, aspect)
    tokenized_examples = nlp.tokenize(examples=[example])
    input_batch = nlp.encode(tokenized_examples)
    output_batch = nlp.predict(input_batch)

    tokenized_example = tokenized_examples[0]
    attentions = alignment.merge_input_attentions(
        output_batch.attentions[0],
        alignment=tokenized_example.alignment
    )
    attention_grads = alignment.merge_input_attentions(
        output_batch.attention_grads[0],
        alignment=tokenized_example.alignment
    )
    return tokenized_example, attentions, attention_grads
コード例 #22
0
def aspect_based_sentiment_analysis(text, aspect1, aspect2):
    if text and aspect1 and aspect2:
        recognizer = absa.aux_models.BasicPatternRecognizer()
        absa_nlp = absa.load(pattern_recognizer=recognizer)
        a1, a2 = absa_nlp(text, aspects=[aspect1, aspect2]).examples
        st.subheader("Summary")
        a1_rounded_scores = np.round(a1.scores, decimals=3)
        a2_rounded_scores = np.round(a2.scores, decimals=3)
        st.write(f'{str(a1.sentiment)} for "{a1.aspect}"')
        st.write(f'Scores (neutral/negative/positive): {a1_rounded_scores}')
        if a1.review.patterns is not None:
            components.v1.html(
                absa.plots.display_html(a1.review.patterns)._repr_html_())
        st.write(f'{str(a2.sentiment)} for "{a2.aspect}"')
        st.write(f'Scores (neutral/negative/positive): {a2_rounded_scores}')
        if a2.review.patterns is not None:
            components.v1.html(
                absa.plots.display_html(a2.review.patterns)._repr_html_())
    else:
        st.write("Please enter aspect based sentiment analysis parameters.")
コード例 #23
0
def aspectifyV2(topics, dataframe, writeToFile):
    '''
    Parameters:
        topics:
            Type = Dict
            Contains all topics and keywords.
        dataframe:
            type = pandas.DataFrame
            Contains all text for a given dataset.
        writeToFile: 
            Type = Boolean
            If true, write to file, no return statement. 
            If false, return the ABSA results in array form.
    Runs ABSA on all texts to find sentiments. Uses keywords to determine sentiment per indivudal keyword. 
    '''
    aspectsArray = []
    for key in topics.keys():
        for item in topics[key]:
            aspectsArray.append(item)
    nlp = absa.load();
    overall = []

    for row in range(len(dataframe)):
        rowIQ = dataframe['text'][row];
        results = nlp(rowIQ, aspects=aspectsArray)
        mySents = [];
        for term in aspectsArray:
            if term in rowIQ:
                if results[term].sentiment == absa.Sentiment.negative:
                    mySents.append(1)
                elif results[term].sentiment == absa.Sentiment.positive:
                    mySents.append(2);
            else:
                mySents.append(0);
        overall.append(mySents)

    overall = np.asarray(overall);

    if writeToFile:
        np.savetxt('sentiments.csv', overall)
    return overall;
def experiment(models: Dict[str, str]):
    utils.setup_logger(HERE / 'logs' / 'recognition-key-token-triplet.log')
    logger.info('Begin Evaluation: the Key Token Triplet Recognition '
                '(Predicted Negative Examples)')

    for domain in ['restaurant', 'laptop']:
        name = models[domain]
        nlp = absa.load(name)

        y_ref, y_new, mask = retrieve_negative_labels(nlp, domain)
        max_score_matrix = confusion_matrix(y_ref, y_new)

        # Remember that we process only (predicted) negative examples,
        # therefore, we need to retrieve predicted negative labels.
        _, reference, _ = key_token_labels(nlp, domain, is_test=True)
        negative = reference == Sentiment.negative
        max_score = sum(mask) / sum(negative)

        logger.info(
            f'{domain.upper()} DOMAIN\n'
            f'Examples that have at least one key token triplet: '
            f'{max_score:.4f}\ny_ref means a prediction without a mask, '
            f'and y_new with a triplet mask.\n'
            f'Confusion Matrix (y_ref, y_new):\n{max_score_matrix}\n\n')
コード例 #25
0
def justScores():
    nlp = absa.load();

    tweet1 = "I think the President is doing an excellent job handling the ongoing issues with China. " +
    "Despite the nation's desperate attempts to interfere with our economy, our military has protected important " +
    "trade regions and ensured financial success for years to come."
    tweet2 = "Our President is a corrupt and terrible man. This is evident in how he has treated the leaders of numerous other nations, handled the nation's economy during the COVID situation, and protected corrupt police from justice." 

    results = nlp(tweet1, aspects=topics)
    mySents = []
    mySentsV2 = []
    for topic in topics:
        myChoice = topicsDict[topic]
        haveFound = False;
        for word in myChoice:
            if (word in tweet1) and not haveFound:
                if results[topic].sentiment == absa.Sentiment.negative:
                    mySents.append(1);
                elif results[topic].sentiment == absa.Sentiment.positive:
                    mySents.append(2);
                mySentsV2.append(np.round(results[topic].scores, decimals=3))
                #html = absa.probing.explain(results[topic])
                #display(html)
                haveFound = True;
                print('----------------------------------------------------------------------------------')
        if not haveFound:
            mySents.append(0)
            mySentsV2.append(0) #neutral

    print(mySents);
    print(mySentsV2);

    topics = ['economics', 'police', 'foreign_policy', "president", "immigration", "military", "abortion"]

    t = [2, 2, 2, 0, 0, 2, 0]
    t2 = [array([0.173, 0.395, 0.431]), array([0.235, 0.159, 0.606]), array([0.072, 0.076, 0.852]), 0, 0, array([0.02, 0.02, 0.96]), 0]
コード例 #26
0
def get_absa():
    absa_nlp = absa.load()
    return absa_nlp
コード例 #27
0
    'D:/School/CollegeJunior/Fall2020/LING495/Project/repo/political-sentiment-graph/sentiment-analysis'
)

ABSOLUTE_CACHE_FOLDER_PATH = "D:/School/CollegeJunior/Fall2020/LING495/Project/repo/political-sentiment-graph/sentiment-analysis/data"

cache = {}
try:
    cacheFile = open(f'{ABSOLUTE_CACHE_FOLDER_PATH}/tweet_sentiment_data.json')
    cache = json.load(cacheFile)
    print('loaded cache')
except:
    print('No saved cache found or saved cache corrupted')

afinn = Afinn()

nlp = absa.load("absa/classifier-lapt-0.2")

correct = 0
total = 0
wrongTweets = []  #{text: , predicted:, actual:,}
for topic in cache:
    for i in range(len(cache[topic]['tweets'])):
        total += 1
        tweet = cache[topic]['tweets'][i]
        actual = cache[topic]['labels'][i]
        sentimentObj, extraStuff = nlp(tweet, aspects=[topic, topic])
        predicted = 0
        if sentimentObj.sentiment == absa.Sentiment.negative:
            predicted = -1
        elif sentimentObj.sentiment == absa.Sentiment.positive:
            predicted = 1
コード例 #28
0
def load_bert():
    model = absa.load()
    return model
コード例 #29
0
    df["text_aspect"] = list(zip(df.full_message, df.best_aspect))
    return df


aspects = {
    "food": "food meal dinner drink dish snacks",
    "staff": "crew hostess onboarding service staff",
    "seat": "seat space knees comfort leg",
    "entertainment": "screen movie display video music",
    "luggage": "bag suitcase handbag",
}

if __name__ == "__main__":
    path = "data/evaluation/"
    file_name = "TEST_data_with_Topics.csv"
    df = pd.read_csv(path + file_name)

    # Applying ABSA
    nlp = absa.load(name='absa/classifier-rest-0.2')
    df = preprocess_for_ABSA(df)
    df["sentiment"] = df.text_aspect.apply(extract_sentiment)

    # reorder for output
    df["predicted_aspect"] = df["best_aspect"].copy()
    df = df[[
        "text_aspect", "predicted_aspect", "sentiment", "food", "staff",
        "seat", "entertainment", "luggage"
    ]].copy()
    print(df)
    df.to_csv(path + "TEST_data_with_Topics_Sentiments.csv")
コード例 #30
0
def evaluate_condition(domain: str, nlp_modified: bool = False) -> np.ndarray:
    dataset = aspect_condition.build_dataset(domain, 0)  # default seed=0
    metric = absa.training.ConfusionMatrix(num_classes=3)
    confusion_matrix = nlp.evaluate(dataset, metric, batch_size=32)
    confusion_matrix = confusion_matrix.numpy()
    return confusion_matrix


if __name__ == '__main__':
    os.chdir(HERE)
    utils.setup_logger(HERE / 'logs' / 'aux-model-reference.log')

    logger.info('Begin Evaluation: Basic Pattern Recognizer')
    for dataset_domain in ['restaurant', 'laptop']:
        lm_name = PRETRAINED_LM_NAMES[dataset_domain]
        nlp = absa.load(lm_name)

        accuracy = lambda m: np.diagonal(m).sum() / m.sum()
        ref_matrix = evaluate(dataset_domain)
        ref_acc = accuracy(ref_matrix)
        ref_test_matrix = evaluate_condition(dataset_domain)
        ref_test_acc = accuracy(ref_test_matrix)

        weights = train(dataset_domain)
        recognizer = absa.BasicReferenceRecognizer(weights=weights)
        nlp.professor.reference_recognizer = recognizer
        matrix = evaluate(dataset_domain, nlp_modified=True)
        acc = accuracy(matrix)

        test_matrix = evaluate_condition(dataset_domain, nlp_modified=True)
        test_acc = accuracy(test_matrix)