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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
|
%global _empty_manifest_terminate_build 0
Name: python-alpacka
Version: 0.1.2
Release: 1
Summary: The alpacka Python package, used to extract and visualize metadata from text data sets
License: LICENCE.txt
URL: https://github.com/BernhardMoller/alpacka
Source0: https://mirrors.nju.edu.cn/pypi/web/packages/3a/f1/4bb81f4735c77f7d4230c7c396e45f744b2a74f0851ccd442c0141424848/alpacka-0.1.2.tar.gz
BuildArch: noarch
Requires: python3-keras
Requires: python3-scikit-learn
Requires: python3-matplotlib
Requires: python3-tensorflow
Requires: python3-numpy
%description
# Example of application of the sdlabb package for metadata extaction
# NCOF method
Created 13-11-20
Author: Fredrik Möller
# Install the alpacka package
## Through pip or through github
`pip install alpacka`
[Link to github repo](https://github.com/BernhardMoller/alpacka)
### Set up
To be able to use the alpacka package a data set for the analysis is needed. For this demo we will use the "Twitter US Airline Sentiment" data set. Availabe at https://www.kaggle.com/crowdflower/twitter-airline-sentiment
# Step by step guide for the NCOF pipeline
### TLDR without explenations, minimum of commands to go from input to output.
```python
from alpacka.Pipeline import Pipeline
import pandas as pd
data = pd.read_csv('Tweets.csv')
score = data.airline_sentiment
score = pd.factorize(score)[0]
from nltk.corpus import stopwords
stop_words = set(stopwords.words('english'))
p = Pipeline(num_words=10000, class_perspective=2)
ncof, dictionary = p.ncof.calc_ncof(data.text, score,stop_words=stop_words)
inliers, pos_out, neg_out = p.ncof.split_score(ncof)
words_all = p.ncof.ind_2_txt(pos_out)
p.ncof.get_result(ncof,words_all)
```
## Long version
### Load and preprocess the data
Before you can apply the alpacka package you will need to load your for this demo we will not perform any data cleaning but this would be the time to do so.
We also load some stop words that will be used later.
```python
import pandas as pd
data = pd.read_csv('Tweets.csv')
from nltk.corpus import stopwords
stop_words = set(stopwords.words('english'))
data.head()
```
From the code we can see that the data has been loaded but the "airline_sentiment" are not integer labels, we need to fix this.
**IMPORTANT:** The alpacka package can only handle integer labels in the range of [0 n]. If your data set has labels in any other range you will need to translate them to the [0 n] range.
```python
print(data.airline_sentiment[:10])
score = pd.factorize(data.airline_sentiment)
print(score[:10])
score= score[0]
print(score[:10])
```
0 neutral
1 positive
2 neutral
3 negative
4 negative
5 negative
6 positive
7 neutral
8 positive
9 positive
Name: airline_sentiment, dtype: object
(array([0, 1, 0, ..., 0, 2, 0], dtype=int64), Index(['neutral', 'positive', 'negative'], dtype='object'))
[0 1 0 2 2 2 1 0 1 1]
We can now see that the data is transformed from stings to integers in the range of [0 2]. With the mapping neutral: 0 , positive: 1, negative: 2.
### Importing and initiating alpacka
Now we are ready to import and initiate alpacka.
```python
from alpacka.Pipeline import Pipeline
p = Pipeline(num_words=10000, class_perspective = 2)
```
There are some setting that we can make in the NCOF method that we need to specify before we start. One of which is how many unique tokens do we want to to take into consideration in the analysis, variable `num_words`. The defult setting for this variable is `None` meaning that all unique tokens will be used in the analysis. For "large" data sets this choice is quite ambitious given that the number of tokens that appear only once or twice in the a corpus. For this walkthrough we will limit the analysis to the $10,000$ most common words in the corpus.
Another setting that we need to specify is for what class we want the results presented for, variable `class_perspective`. As the NCOF method presents results regarding if a token is overrepresented in a class compared to the rest of the corpus, which class to investigate nees to be specified, the defult value of `class_perspective` is `1`. For this demo we have chosen the perspective of class 2 (negatives).
Now the `NCOF` and `TF-IDF` pipelines are initiated through the wrapper `Pipeline` and the induvidual analysis methods can be accessed be calling:
`p.ncof.some_functions()`
Now we are ready to start the analysis of our data.
Note that the `.some_functions()` function is a placeholder and do exist.
#### Calculate NCOF
Now we are ready to calculate the NCOF score for the review data and its scores. This is done by calling the `.calc_ncof(data,labels)` function. For this example the input in the `data` field is the texts, and the `labels` is the review scores.
```python
ncof, dictionary = p.ncof.calc_ncof(data.text, score)
```
NCOF score added under 'self.score' use self.get_--- to access the result
Stop words can also automatically be removed from the NCOF calculating by passing a set of stop words in the `.calc_ncof` function. The reason you might want to remove stop words before NCOF calculations is due tothe stop words altering frequency distrubution of "value adding" words by by increasing the frequencylimit of when a word is considered as an NCOF outliers. Top top is to test with and without the stop word removal and you will see the difference.
For this demo we keep the score where stop words has been removed.
```python
ncof_score, dictionary = p.ncof.calc_ncof(data.text, score,stop_words=stop_words)
```
NCOF score added under 'self.score' use self.get_--- to access the result
We now have an array, `ncof_score`, that contains the NCOF results for our data. This array will have the size `[1,num_words]` and positive and negatives values, indicating if a token is more or less common in investigated class (positives values), or the remaining classes (negative values).
```python
print(ncof_score[:10])
```
[-0.03774483 -0.02481352 -0.01183599 -0.00601852 -0.01548369 -0.04066156
-0.03846028 -0.00513828 0.00310846 -0.00088758]
In addition to an array with the scores the `.calc_ncof()` function returns a dictionary that maps the indexes in the `ncof_score ` array to its text representations, and can be accessed by calling:
```python
w = list(dictionary.items())[:10]
for item in w:
print(item)
```
(0, 'i')
(1, '@united')
(2, 'flight')
(3, '@usairways')
(4, '@americanair')
(5, '@southwestair')
(6, '@jetblue')
(7, 'get')
(8, 'cancelled')
(9, 'customer')
#### Sorting results
To sort the array into inliers and outliers for the positive and negative values the function `.split_score()`needs to be called. The inliers can be accessed through:
**Important:** The the results returned as `pos_outliers` will containd the NCOF outliers that are identified from the class selected as the `class_perspective`. Meaning that they are positive from the perspective that are chosen, in out case we use a positive notation for the negative sentiment due to our selected class perspective.
```python
inliers, pos_outliers,neg_outliers = p.ncof.split_score(ncof_score)
```
Inliers added under 'self.inliers'
Positive outliers added under 'self.pos_outliers'
Negative outliers added under 'self.neg_outliers'
use self.get_--- to access the result
Which will return the indexes of the words in the dictionary that are considered as outliers in the NCOF results.
The results are sorted within the `ncof_pos `and `ncof_neg ` as the following:
ncof_pos[0] = $\mu+\sigma\leq result <\mu+2\sigma$
ncof_pos[1] = $\mu+2\sigma\leq result <\mu+3\sigma$
ncof_pos[2] = $\mu+3\sigma\leq result$
ncof_neg[0] = $\mu-\sigma\geq result >\mu-2\sigma$
ncof_neg[1] = $\mu-2\sigma\geq result >\mu-3\sigma$
ncof_neg[2] = $\mu-3\sigma\geq result$
#### Plotting results
These results can be plotted by calling the function `.scatter()` which will give visual information regarding what tokens are over or under represented in the investigated class.
```python
p.ncof.scatter(ncof_score, inliers, pos_outliers,neg_outliers)
```
#### Converting results from indexes to text
Since it is quite difficult to interpret the socre for each the indexes directly, it is suggested that the indexes are transformed back to their text representations. This can be done by calling the `.ncof.ind_2_txt(data)`function, the function input should be either indexes of the positive or negative outlers.
```python
words_pos = p.ncof.ind_2_txt(pos_outliers)
words_neg = p.ncof.ind_2_txt(neg_outliers)
```
If the text results want to be cleaned from stop words for clarification. The function `.remove_stop_words(data,stop_words)` can be called. This functon compares the content of the input `data` to that of the input `stop-words` and removes any matches between them from the `data`. For this walkthrough we will use the stop words available from the NLTK package.
We have already removed the stop words from the data set when we calculated the NCOF score, so we will not need to preform this step. That is why this part is commented out.
```python
# words_pos = p.ncof.remove_stop_words(words_pos,stop_words)
# words_neg = p.ncof.remove_stop_words(words_neg,stop_words)
```
#### Print results to terminal.
We have now gone through all the steps required to produce, plot, and clean the reults from the NCOF analysis method. The last part is to either save the results to a file or to print them to the terminal. Since format to save the results to is a user preference no function for this is provided in the alpacka package, however the results can be printed to the terminal by calling the following function.
The input variable `sort` can be set to either `True` or `False` and decides if the results should be printed as alphabetically sorted or not.
```python
print(f"printing outliers from the investigated class")
p.ncof.get_result(ncof_score,words_pos, sort = True)
print(f" ")
print(f"printing outliers from the remaining classes")
p.ncof.get_result(ncof_score,words_neg, sort = True)
```
%package -n python3-alpacka
Summary: The alpacka Python package, used to extract and visualize metadata from text data sets
Provides: python-alpacka
BuildRequires: python3-devel
BuildRequires: python3-setuptools
BuildRequires: python3-pip
%description -n python3-alpacka
# Example of application of the sdlabb package for metadata extaction
# NCOF method
Created 13-11-20
Author: Fredrik Möller
# Install the alpacka package
## Through pip or through github
`pip install alpacka`
[Link to github repo](https://github.com/BernhardMoller/alpacka)
### Set up
To be able to use the alpacka package a data set for the analysis is needed. For this demo we will use the "Twitter US Airline Sentiment" data set. Availabe at https://www.kaggle.com/crowdflower/twitter-airline-sentiment
# Step by step guide for the NCOF pipeline
### TLDR without explenations, minimum of commands to go from input to output.
```python
from alpacka.Pipeline import Pipeline
import pandas as pd
data = pd.read_csv('Tweets.csv')
score = data.airline_sentiment
score = pd.factorize(score)[0]
from nltk.corpus import stopwords
stop_words = set(stopwords.words('english'))
p = Pipeline(num_words=10000, class_perspective=2)
ncof, dictionary = p.ncof.calc_ncof(data.text, score,stop_words=stop_words)
inliers, pos_out, neg_out = p.ncof.split_score(ncof)
words_all = p.ncof.ind_2_txt(pos_out)
p.ncof.get_result(ncof,words_all)
```
## Long version
### Load and preprocess the data
Before you can apply the alpacka package you will need to load your for this demo we will not perform any data cleaning but this would be the time to do so.
We also load some stop words that will be used later.
```python
import pandas as pd
data = pd.read_csv('Tweets.csv')
from nltk.corpus import stopwords
stop_words = set(stopwords.words('english'))
data.head()
```
From the code we can see that the data has been loaded but the "airline_sentiment" are not integer labels, we need to fix this.
**IMPORTANT:** The alpacka package can only handle integer labels in the range of [0 n]. If your data set has labels in any other range you will need to translate them to the [0 n] range.
```python
print(data.airline_sentiment[:10])
score = pd.factorize(data.airline_sentiment)
print(score[:10])
score= score[0]
print(score[:10])
```
0 neutral
1 positive
2 neutral
3 negative
4 negative
5 negative
6 positive
7 neutral
8 positive
9 positive
Name: airline_sentiment, dtype: object
(array([0, 1, 0, ..., 0, 2, 0], dtype=int64), Index(['neutral', 'positive', 'negative'], dtype='object'))
[0 1 0 2 2 2 1 0 1 1]
We can now see that the data is transformed from stings to integers in the range of [0 2]. With the mapping neutral: 0 , positive: 1, negative: 2.
### Importing and initiating alpacka
Now we are ready to import and initiate alpacka.
```python
from alpacka.Pipeline import Pipeline
p = Pipeline(num_words=10000, class_perspective = 2)
```
There are some setting that we can make in the NCOF method that we need to specify before we start. One of which is how many unique tokens do we want to to take into consideration in the analysis, variable `num_words`. The defult setting for this variable is `None` meaning that all unique tokens will be used in the analysis. For "large" data sets this choice is quite ambitious given that the number of tokens that appear only once or twice in the a corpus. For this walkthrough we will limit the analysis to the $10,000$ most common words in the corpus.
Another setting that we need to specify is for what class we want the results presented for, variable `class_perspective`. As the NCOF method presents results regarding if a token is overrepresented in a class compared to the rest of the corpus, which class to investigate nees to be specified, the defult value of `class_perspective` is `1`. For this demo we have chosen the perspective of class 2 (negatives).
Now the `NCOF` and `TF-IDF` pipelines are initiated through the wrapper `Pipeline` and the induvidual analysis methods can be accessed be calling:
`p.ncof.some_functions()`
Now we are ready to start the analysis of our data.
Note that the `.some_functions()` function is a placeholder and do exist.
#### Calculate NCOF
Now we are ready to calculate the NCOF score for the review data and its scores. This is done by calling the `.calc_ncof(data,labels)` function. For this example the input in the `data` field is the texts, and the `labels` is the review scores.
```python
ncof, dictionary = p.ncof.calc_ncof(data.text, score)
```
NCOF score added under 'self.score' use self.get_--- to access the result
Stop words can also automatically be removed from the NCOF calculating by passing a set of stop words in the `.calc_ncof` function. The reason you might want to remove stop words before NCOF calculations is due tothe stop words altering frequency distrubution of "value adding" words by by increasing the frequencylimit of when a word is considered as an NCOF outliers. Top top is to test with and without the stop word removal and you will see the difference.
For this demo we keep the score where stop words has been removed.
```python
ncof_score, dictionary = p.ncof.calc_ncof(data.text, score,stop_words=stop_words)
```
NCOF score added under 'self.score' use self.get_--- to access the result
We now have an array, `ncof_score`, that contains the NCOF results for our data. This array will have the size `[1,num_words]` and positive and negatives values, indicating if a token is more or less common in investigated class (positives values), or the remaining classes (negative values).
```python
print(ncof_score[:10])
```
[-0.03774483 -0.02481352 -0.01183599 -0.00601852 -0.01548369 -0.04066156
-0.03846028 -0.00513828 0.00310846 -0.00088758]
In addition to an array with the scores the `.calc_ncof()` function returns a dictionary that maps the indexes in the `ncof_score ` array to its text representations, and can be accessed by calling:
```python
w = list(dictionary.items())[:10]
for item in w:
print(item)
```
(0, 'i')
(1, '@united')
(2, 'flight')
(3, '@usairways')
(4, '@americanair')
(5, '@southwestair')
(6, '@jetblue')
(7, 'get')
(8, 'cancelled')
(9, 'customer')
#### Sorting results
To sort the array into inliers and outliers for the positive and negative values the function `.split_score()`needs to be called. The inliers can be accessed through:
**Important:** The the results returned as `pos_outliers` will containd the NCOF outliers that are identified from the class selected as the `class_perspective`. Meaning that they are positive from the perspective that are chosen, in out case we use a positive notation for the negative sentiment due to our selected class perspective.
```python
inliers, pos_outliers,neg_outliers = p.ncof.split_score(ncof_score)
```
Inliers added under 'self.inliers'
Positive outliers added under 'self.pos_outliers'
Negative outliers added under 'self.neg_outliers'
use self.get_--- to access the result
Which will return the indexes of the words in the dictionary that are considered as outliers in the NCOF results.
The results are sorted within the `ncof_pos `and `ncof_neg ` as the following:
ncof_pos[0] = $\mu+\sigma\leq result <\mu+2\sigma$
ncof_pos[1] = $\mu+2\sigma\leq result <\mu+3\sigma$
ncof_pos[2] = $\mu+3\sigma\leq result$
ncof_neg[0] = $\mu-\sigma\geq result >\mu-2\sigma$
ncof_neg[1] = $\mu-2\sigma\geq result >\mu-3\sigma$
ncof_neg[2] = $\mu-3\sigma\geq result$
#### Plotting results
These results can be plotted by calling the function `.scatter()` which will give visual information regarding what tokens are over or under represented in the investigated class.
```python
p.ncof.scatter(ncof_score, inliers, pos_outliers,neg_outliers)
```
#### Converting results from indexes to text
Since it is quite difficult to interpret the socre for each the indexes directly, it is suggested that the indexes are transformed back to their text representations. This can be done by calling the `.ncof.ind_2_txt(data)`function, the function input should be either indexes of the positive or negative outlers.
```python
words_pos = p.ncof.ind_2_txt(pos_outliers)
words_neg = p.ncof.ind_2_txt(neg_outliers)
```
If the text results want to be cleaned from stop words for clarification. The function `.remove_stop_words(data,stop_words)` can be called. This functon compares the content of the input `data` to that of the input `stop-words` and removes any matches between them from the `data`. For this walkthrough we will use the stop words available from the NLTK package.
We have already removed the stop words from the data set when we calculated the NCOF score, so we will not need to preform this step. That is why this part is commented out.
```python
# words_pos = p.ncof.remove_stop_words(words_pos,stop_words)
# words_neg = p.ncof.remove_stop_words(words_neg,stop_words)
```
#### Print results to terminal.
We have now gone through all the steps required to produce, plot, and clean the reults from the NCOF analysis method. The last part is to either save the results to a file or to print them to the terminal. Since format to save the results to is a user preference no function for this is provided in the alpacka package, however the results can be printed to the terminal by calling the following function.
The input variable `sort` can be set to either `True` or `False` and decides if the results should be printed as alphabetically sorted or not.
```python
print(f"printing outliers from the investigated class")
p.ncof.get_result(ncof_score,words_pos, sort = True)
print(f" ")
print(f"printing outliers from the remaining classes")
p.ncof.get_result(ncof_score,words_neg, sort = True)
```
%package help
Summary: Development documents and examples for alpacka
Provides: python3-alpacka-doc
%description help
# Example of application of the sdlabb package for metadata extaction
# NCOF method
Created 13-11-20
Author: Fredrik Möller
# Install the alpacka package
## Through pip or through github
`pip install alpacka`
[Link to github repo](https://github.com/BernhardMoller/alpacka)
### Set up
To be able to use the alpacka package a data set for the analysis is needed. For this demo we will use the "Twitter US Airline Sentiment" data set. Availabe at https://www.kaggle.com/crowdflower/twitter-airline-sentiment
# Step by step guide for the NCOF pipeline
### TLDR without explenations, minimum of commands to go from input to output.
```python
from alpacka.Pipeline import Pipeline
import pandas as pd
data = pd.read_csv('Tweets.csv')
score = data.airline_sentiment
score = pd.factorize(score)[0]
from nltk.corpus import stopwords
stop_words = set(stopwords.words('english'))
p = Pipeline(num_words=10000, class_perspective=2)
ncof, dictionary = p.ncof.calc_ncof(data.text, score,stop_words=stop_words)
inliers, pos_out, neg_out = p.ncof.split_score(ncof)
words_all = p.ncof.ind_2_txt(pos_out)
p.ncof.get_result(ncof,words_all)
```
## Long version
### Load and preprocess the data
Before you can apply the alpacka package you will need to load your for this demo we will not perform any data cleaning but this would be the time to do so.
We also load some stop words that will be used later.
```python
import pandas as pd
data = pd.read_csv('Tweets.csv')
from nltk.corpus import stopwords
stop_words = set(stopwords.words('english'))
data.head()
```
From the code we can see that the data has been loaded but the "airline_sentiment" are not integer labels, we need to fix this.
**IMPORTANT:** The alpacka package can only handle integer labels in the range of [0 n]. If your data set has labels in any other range you will need to translate them to the [0 n] range.
```python
print(data.airline_sentiment[:10])
score = pd.factorize(data.airline_sentiment)
print(score[:10])
score= score[0]
print(score[:10])
```
0 neutral
1 positive
2 neutral
3 negative
4 negative
5 negative
6 positive
7 neutral
8 positive
9 positive
Name: airline_sentiment, dtype: object
(array([0, 1, 0, ..., 0, 2, 0], dtype=int64), Index(['neutral', 'positive', 'negative'], dtype='object'))
[0 1 0 2 2 2 1 0 1 1]
We can now see that the data is transformed from stings to integers in the range of [0 2]. With the mapping neutral: 0 , positive: 1, negative: 2.
### Importing and initiating alpacka
Now we are ready to import and initiate alpacka.
```python
from alpacka.Pipeline import Pipeline
p = Pipeline(num_words=10000, class_perspective = 2)
```
There are some setting that we can make in the NCOF method that we need to specify before we start. One of which is how many unique tokens do we want to to take into consideration in the analysis, variable `num_words`. The defult setting for this variable is `None` meaning that all unique tokens will be used in the analysis. For "large" data sets this choice is quite ambitious given that the number of tokens that appear only once or twice in the a corpus. For this walkthrough we will limit the analysis to the $10,000$ most common words in the corpus.
Another setting that we need to specify is for what class we want the results presented for, variable `class_perspective`. As the NCOF method presents results regarding if a token is overrepresented in a class compared to the rest of the corpus, which class to investigate nees to be specified, the defult value of `class_perspective` is `1`. For this demo we have chosen the perspective of class 2 (negatives).
Now the `NCOF` and `TF-IDF` pipelines are initiated through the wrapper `Pipeline` and the induvidual analysis methods can be accessed be calling:
`p.ncof.some_functions()`
Now we are ready to start the analysis of our data.
Note that the `.some_functions()` function is a placeholder and do exist.
#### Calculate NCOF
Now we are ready to calculate the NCOF score for the review data and its scores. This is done by calling the `.calc_ncof(data,labels)` function. For this example the input in the `data` field is the texts, and the `labels` is the review scores.
```python
ncof, dictionary = p.ncof.calc_ncof(data.text, score)
```
NCOF score added under 'self.score' use self.get_--- to access the result
Stop words can also automatically be removed from the NCOF calculating by passing a set of stop words in the `.calc_ncof` function. The reason you might want to remove stop words before NCOF calculations is due tothe stop words altering frequency distrubution of "value adding" words by by increasing the frequencylimit of when a word is considered as an NCOF outliers. Top top is to test with and without the stop word removal and you will see the difference.
For this demo we keep the score where stop words has been removed.
```python
ncof_score, dictionary = p.ncof.calc_ncof(data.text, score,stop_words=stop_words)
```
NCOF score added under 'self.score' use self.get_--- to access the result
We now have an array, `ncof_score`, that contains the NCOF results for our data. This array will have the size `[1,num_words]` and positive and negatives values, indicating if a token is more or less common in investigated class (positives values), or the remaining classes (negative values).
```python
print(ncof_score[:10])
```
[-0.03774483 -0.02481352 -0.01183599 -0.00601852 -0.01548369 -0.04066156
-0.03846028 -0.00513828 0.00310846 -0.00088758]
In addition to an array with the scores the `.calc_ncof()` function returns a dictionary that maps the indexes in the `ncof_score ` array to its text representations, and can be accessed by calling:
```python
w = list(dictionary.items())[:10]
for item in w:
print(item)
```
(0, 'i')
(1, '@united')
(2, 'flight')
(3, '@usairways')
(4, '@americanair')
(5, '@southwestair')
(6, '@jetblue')
(7, 'get')
(8, 'cancelled')
(9, 'customer')
#### Sorting results
To sort the array into inliers and outliers for the positive and negative values the function `.split_score()`needs to be called. The inliers can be accessed through:
**Important:** The the results returned as `pos_outliers` will containd the NCOF outliers that are identified from the class selected as the `class_perspective`. Meaning that they are positive from the perspective that are chosen, in out case we use a positive notation for the negative sentiment due to our selected class perspective.
```python
inliers, pos_outliers,neg_outliers = p.ncof.split_score(ncof_score)
```
Inliers added under 'self.inliers'
Positive outliers added under 'self.pos_outliers'
Negative outliers added under 'self.neg_outliers'
use self.get_--- to access the result
Which will return the indexes of the words in the dictionary that are considered as outliers in the NCOF results.
The results are sorted within the `ncof_pos `and `ncof_neg ` as the following:
ncof_pos[0] = $\mu+\sigma\leq result <\mu+2\sigma$
ncof_pos[1] = $\mu+2\sigma\leq result <\mu+3\sigma$
ncof_pos[2] = $\mu+3\sigma\leq result$
ncof_neg[0] = $\mu-\sigma\geq result >\mu-2\sigma$
ncof_neg[1] = $\mu-2\sigma\geq result >\mu-3\sigma$
ncof_neg[2] = $\mu-3\sigma\geq result$
#### Plotting results
These results can be plotted by calling the function `.scatter()` which will give visual information regarding what tokens are over or under represented in the investigated class.
```python
p.ncof.scatter(ncof_score, inliers, pos_outliers,neg_outliers)
```
#### Converting results from indexes to text
Since it is quite difficult to interpret the socre for each the indexes directly, it is suggested that the indexes are transformed back to their text representations. This can be done by calling the `.ncof.ind_2_txt(data)`function, the function input should be either indexes of the positive or negative outlers.
```python
words_pos = p.ncof.ind_2_txt(pos_outliers)
words_neg = p.ncof.ind_2_txt(neg_outliers)
```
If the text results want to be cleaned from stop words for clarification. The function `.remove_stop_words(data,stop_words)` can be called. This functon compares the content of the input `data` to that of the input `stop-words` and removes any matches between them from the `data`. For this walkthrough we will use the stop words available from the NLTK package.
We have already removed the stop words from the data set when we calculated the NCOF score, so we will not need to preform this step. That is why this part is commented out.
```python
# words_pos = p.ncof.remove_stop_words(words_pos,stop_words)
# words_neg = p.ncof.remove_stop_words(words_neg,stop_words)
```
#### Print results to terminal.
We have now gone through all the steps required to produce, plot, and clean the reults from the NCOF analysis method. The last part is to either save the results to a file or to print them to the terminal. Since format to save the results to is a user preference no function for this is provided in the alpacka package, however the results can be printed to the terminal by calling the following function.
The input variable `sort` can be set to either `True` or `False` and decides if the results should be printed as alphabetically sorted or not.
```python
print(f"printing outliers from the investigated class")
p.ncof.get_result(ncof_score,words_pos, sort = True)
print(f" ")
print(f"printing outliers from the remaining classes")
p.ncof.get_result(ncof_score,words_neg, sort = True)
```
%prep
%autosetup -n alpacka-0.1.2
%build
%py3_build
%install
%py3_install
install -d -m755 %{buildroot}/%{_pkgdocdir}
if [ -d doc ]; then cp -arf doc %{buildroot}/%{_pkgdocdir}; fi
if [ -d docs ]; then cp -arf docs %{buildroot}/%{_pkgdocdir}; fi
if [ -d example ]; then cp -arf example %{buildroot}/%{_pkgdocdir}; fi
if [ -d examples ]; then cp -arf examples %{buildroot}/%{_pkgdocdir}; fi
pushd %{buildroot}
if [ -d usr/lib ]; then
find usr/lib -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/lib64 ]; then
find usr/lib64 -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/bin ]; then
find usr/bin -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/sbin ]; then
find usr/sbin -type f -printf "/%h/%f\n" >> filelist.lst
fi
touch doclist.lst
if [ -d usr/share/man ]; then
find usr/share/man -type f -printf "/%h/%f.gz\n" >> doclist.lst
fi
popd
mv %{buildroot}/filelist.lst .
mv %{buildroot}/doclist.lst .
%files -n python3-alpacka -f filelist.lst
%dir %{python3_sitelib}/*
%files help -f doclist.lst
%{_docdir}/*
%changelog
* Tue May 30 2023 Python_Bot <Python_Bot@openeuler.org> - 0.1.2-1
- Package Spec generated
|