Bin ZHANG commited on
Commit
1d1a9db
1 Parent(s): 15551ec

sample code from ch1

Browse files
Files changed (1) hide show
  1. natural-language-processing_ch1.py +45 -0
natural-language-processing_ch1.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ text = """Dear Amazon, last week I ordered an Optimus Prime action figure
2
+ from your online store in Germany. Unfortunately, when I opened the package,
3
+ I discovered to my horror that I had been sent an action figure of Megatron
4
+ instead! As a lifelong enemy of the Decepticons, I hope you can understand my
5
+ dilemma. To resolve the issue, I demand an exchange of Megatron for the
6
+ Optimus Prime figure I ordered. Enclosed are copies of my records concerning
7
+ this purchase. I expect to hear from you soon. Sincerely, Bumblebee."""
8
+
9
+ from transformers import pipeline
10
+
11
+ classifier = pipeline("text-classification")
12
+
13
+ import pandas as pd
14
+
15
+ outputs = classifier(text)
16
+ pd.DataFrame(outputs)
17
+
18
+ #named entity recognition (NER)
19
+ ner_tagger = pipeline("ner", aggregation_strategy="simple")
20
+ outputs = ner_tagger(text)
21
+ pd.DataFrame(outputs)
22
+
23
+ reader = pipeline("question-answering")
24
+ question = "What does the customer want?"
25
+ outputs = reader(question=question, context=text)
26
+ pd.DataFrame([outputs])
27
+
28
+ summarizer = pipeline("summarization")
29
+ outputs = summarizer(text, max_length=45, clean_up_tokenization_spaces=True)
30
+ print(outputs[0]['summary_text'])
31
+
32
+
33
+ translator = pipeline("translation_en_to_de",
34
+ model="Helsinki-NLP/opus-mt-en-de")
35
+ outputs = translator(text, clean_up_tokenization_spaces=True, min_length=100)
36
+ print(outputs[0]['translation_text'])
37
+
38
+
39
+ generator = pipeline("text-generation")
40
+ response = "Dear Bumblebee, I am sorry to hear that your order was mixed up."
41
+ prompt = text + "\n\nCustomer service response:\n" + response
42
+ outputs = generator(prompt, max_length=200)
43
+ print(outputs[0]['generated_text'])
44
+
45
+