forked from PaddlePaddle/PaddleNLP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsenta_example.py
67 lines (55 loc) Β· 2.93 KB
/
senta_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
from pipelines import SentaPipeline
from pipelines.nodes import SentaProcessor, SentaVisualization, UIESenta
def format_print(results):
"""
Print Information in results.
"""
if "sr_save_path" in results:
print("\nText Result: ", results["sr_save_path"])
if "img_dict" in results:
print("Visualization Result: ")
for img_name in results["img_dict"]:
print("\t{}:{}".format(img_name, results["img_dict"][img_name]))
def senta_pipeline(args):
"""
Sentiment Analysis with Pipeline.
"""
# initializing SentaPipeline
preprocessor = SentaProcessor(max_examples=args.max_examples)
if not args.aspects:
schema = [{"θ―δ»·η»΄εΊ¦": ["θ§ηΉθ―", "ζ
ζεΎε[ζ£ε,θ΄ε,ζͺζε]"]}]
senta = UIESenta(schema=schema, model=args.model, batch_size=args.batch_size, aspects=args.aspects)
else:
schema = ["θ§ηΉθ―", "ζ
ζεΎε[ζ£ε,θ΄ε,ζͺζε]"]
senta = UIESenta(schema=schema, model=args.model, batch_size=args.batch_size)
visualization = SentaVisualization(font_name="SimHei")
senta_pipeline = SentaPipeline(preprocessor=preprocessor, senta=senta, visualization=visualization)
# run SentaPipeline for inputting file.
meta = {"file_path": args.file_path}
results = senta_pipeline.run(meta=meta)
format_print(results)
if __name__ == "__main__":
# yapf: disable
parser = argparse.ArgumentParser()
parser.add_argument("--file_path", required=True, type=str, help="The file that you want to perform sentiment analysis on.")
parser.add_argument("--max_examples", default=-1, type=int, help="The maxinum number of examples processed by pipline.")
parser.add_argument("--model", choices=['uie-senta-base', 'uie-senta-medium', 'uie-senta-mini', 'uie-senta-micro', 'uie-senta-nano'], default="uie-senta-base", help="The model name that you wanna use for sentiment analysis.")
parser.add_argument("--aspects", default=None, type=str, nargs="+", help="A list of pre-given aspects, that is to say, Pipeline only perform sentiment analysis on these pre-given aspects if you input it.")
parser.add_argument("--batch_size", default=4, type=int, help="Number of samples the model receives in one batch for sentiment inference.")
args = parser.parse_args()
# yapf: enable
senta_pipeline(args)