|
| 1 | +# Copyright 2019 The Forte Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import os |
| 16 | +import yaml |
| 17 | +from forte.common.configuration import Config |
| 18 | +from forte.data.caster import MultiPackBoxer |
| 19 | +from forte.data.readers import TerminalReader |
| 20 | +from forte.data.multi_pack import MultiPack |
| 21 | + |
| 22 | +from forte.pipeline import Pipeline |
| 23 | +from forte_wrapper.vader import VaderSentimentProcessor |
| 24 | +from forte_wrapper.twitter import TweetSearchProcessor |
| 25 | +from forte.data.selector import RegexNameMatchSelector |
| 26 | + |
| 27 | + |
| 28 | +if __name__ == "__main__": |
| 29 | + # Load config file |
| 30 | + config_file = os.path.join(os.path.dirname(__file__), 'config.yml') |
| 31 | + config = yaml.safe_load(open(config_file, "r")) |
| 32 | + config = Config(config, default_hparams=None) |
| 33 | + |
| 34 | + # Build pipeline and add the reader, which will read query from terminal. |
| 35 | + nlp: Pipeline = Pipeline() |
| 36 | + nlp.set_reader(reader=TerminalReader()) |
| 37 | + |
| 38 | + # Start to work on multi-packs in the rest of the pipeline, so we use a |
| 39 | + # boxer to change this. |
| 40 | + nlp.add(MultiPackBoxer(), config=config.boxer) |
| 41 | + |
| 42 | + # Search tweets. |
| 43 | + nlp.add(TweetSearchProcessor(), config=config.twitter_search) |
| 44 | + |
| 45 | + # Conduct sentiment analysis. |
| 46 | + pattern = rf"{config.twitter_search.response_pack_name_prefix}_\d" |
| 47 | + selector_hit = RegexNameMatchSelector(select_name=pattern) |
| 48 | + nlp.add(component=VaderSentimentProcessor(), |
| 49 | + selector=selector_hit, config=config.vader_sentiment) |
| 50 | + |
| 51 | + nlp.initialize() |
| 52 | + |
| 53 | + # process dataset |
| 54 | + m_pack: MultiPack |
| 55 | + for m_pack in nlp.process_dataset(): |
| 56 | + print('The number of datapacks(including query) is', len(m_pack.packs)) |
| 57 | + |
| 58 | + tweets, pos_sentiment, neg_sentiment, neutral_sentiment = 0, 0, 0, 0 |
| 59 | + |
| 60 | + for name, pack in m_pack.iter_packs(): |
| 61 | + # Do not process the query datapack |
| 62 | + if name == config.twitter_search.query_pack_name: |
| 63 | + continue |
| 64 | + |
| 65 | + tweets += 1 |
| 66 | + for doc in pack.get(config.vader_sentiment.entry_type): |
| 67 | + print('Tweet: ', doc.text) |
| 68 | + print('Sentiment Compound Score: ', |
| 69 | + doc.sentiment['compound']) |
| 70 | + |
| 71 | + compound_score = doc.sentiment['compound'] |
| 72 | + if compound_score >= 0.05: |
| 73 | + pos_sentiment += 1 |
| 74 | + elif compound_score <= -0.05: |
| 75 | + neg_sentiment += 1 |
| 76 | + else: |
| 77 | + neutral_sentiment += 1 |
| 78 | + |
| 79 | + print('The number of tweets retrieved: ', tweets) |
| 80 | + print('The proportion of positive sentiment: ', pos_sentiment / tweets) |
| 81 | + print('The proportion of negative sentiment: ', neg_sentiment / tweets) |
| 82 | + print('The proportion of neutral sentiment: ', |
| 83 | + neutral_sentiment / tweets) |
| 84 | + |
| 85 | + print('Done') |
0 commit comments