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
|
%global _empty_manifest_terminate_build 0
Name: python-customized-KoNLPy
Version: 0.0.64
Release: 1
Summary: KoNLPy wrapping package
License: GNU General Public License v3 (GPLv3)
URL: https://github.com/lovit/customized_konlpy
Source0: https://mirrors.nju.edu.cn/pypi/web/packages/52/02/eb65f75bf275a49ba6faf189cb7566d5de41e971e435a60332591323b365/customized-konlpy-0.0.64.tar.gz
BuildArch: noarch
Requires: python3-Jpype1
Requires: python3-konlpy
%description
# customized KoNLPy
한국어 자연어처리를 할 수 있는 파이썬 패키지, KoNLPy의 customized version입니다.
customized_KoNLPy는 확실히 알고 있는 단어들에 대해서는 라이브러리를 거치지 않고 주어진 어절을 아는 단어들로 토크나이징 / 품사판별을 하는 기능을 제공합니다. 이를 위해 template 기반 토크나이징을 수행합니다.
사전: {'아이오아이': 'Noun', '는': 'Josa'}
탬플릿: Noun + Josa
위와 같은 단어 리스트와 탬플릿이 있다면 '아이오아이는' 이라는 어절은 [('아이오아이', 'Noun'), ('는', 'Josa')]로 분리됩니다.
## Install
$ git clone https://github.com/lovit/customized_konlpy.git
$ pip install customized_konlpy
### Requires
- JPype >= 0.6.1
- KoNLPy >= 0.4.4
## Usage
### Part of speech tagging
KoNLPy와 동일하게 Twitter.pos(phrase)를 입력합니다. 각 어절별로 사용자 사전에 알려진 단어가 인식되면 customized_tagger로 어절을 분리하며, 사용자 사전에 알려지지 않은 단어로 구성된 어절은 트위터 형태소 분석기로 처리합니다.
twitter.pos('우리아이오아이는 이뻐요')
> [('우리', 'Noun'), ('아이오', 'Noun'), ('아이', 'Noun'), ('는', 'Josa'), ('이뻐', 'Adjective'), ('요', 'Eomi')]
'아이오아이'가 알려진 단어가 아니었기 때문에 트위터 분석기에서 단어를 제대로 인식하지 못합니다. 아래의 사용자 사전으로 단어 추가를 한 뒤 동일한 작업을 수행하면 아래와 같은 결과를 얻을 수 있습니다.
twitter.pos('우리아이오아이는 이뻐요')
> [('우리', 'Modifier'), ('아이오아이', 'Noun'), ('는', 'Josa'), ('이뻐', 'Adjective'), ('요', 'Eomi')]
twitter.pos('트와이스tt는 좋아요')
> [('트와이스', 'Noun'), ('tt', 'Noun'), ('는', 'Josa'), ('좋', 'Adjective'), ('아요', 'Eomi')]
### Add words to dictioanry
ckonlpy.tag의 Twitter는 add_dictionary를 통하여 str 혹은 list of str 형식의 사용자 사전을 추가할 수 있습니다.
from ckonlpy.tag import Twitter
twitter.add_dictionary('아이오아이', 'Noun')
twitter.add_dictionary(['트와이스', 'tt'], 'Noun')
트위터 한국어 분석기에서 이용하지 않는 품사 (단어 클래스)를 추가하고 싶을 경우에는 반드시 force=True로 설정해야 합니다.
twitter.add_dictionary('lovit', 'Name', force=True)
### Add template to customized tagger
현재 사용중인 탬플릿 기반 토크나이저는 코드 사용 중 탬플릿을 추가할 수 있습니다. 현재 사용중인 탬플릿의 리스트는 아래처럼 확인할 수 있습니다.
twitter.template_tagger.templates
> [('Noun', 'Josa'), ('Modifier', 'Noun'), ('Modifier', 'Noun', 'Josa')]
탬플릿은 tuple of str 형식으로 입력합니다.
twitter.template_tagger.add_a_template(('Noun', 'Noun', 'Josa'))
### Set templates tagger selector
Templates를 이용하여도 후보가 여러 개 나올 수 있습니다. 여러 개 후보 중에서 best 를 선택하는 함수를 직접 디자인 할 수 도 있습니다. 이처럼 몇 개의 점수 기준을 만들고, 각 기준의 weight를 부여하는 방식은 트위터 분석기에서 이용하는 방식인데, 직관적이고 튜닝 가능해서 매우 좋은 방식이라 생각합니다.
my_weights = [
('num_nouns', -0.1),
('num_words', -0.2),
('no_noun', -1),
('len_sum_of_nouns', 0.2)
]
def my_evaluate_function(candidate):
num_nouns = len([word for word, pos, begin, e in candidate if pos == 'Noun'])
num_words = len(candidate)
has_no_nouns = (num_nouns == 0)
len_sum_of_nouns = 0 if has_no_nouns else sum(
(len(word) for word, pos, _, _ in candidate if pos == 'Noun'))
scores = (num_nouns, num_words, has_no_nouns, len_sum_of_nouns)
score = sum((score * weight for score, (_, weight) in zip(scores, my_weights)))
return score
위의 예제처럼 my_weights 와 my_evaluate_function 함수를 정의하여 twitter.set_evaluator()에 입력하면, 해당 함수 기준으로 best candidate를 선택합니다.
twitter.set_evaluator(my_weights, my_evaluate_function)
### Postprocessor
passwords, stopwords, passtags, 단어 치환을 위한 후처리를 할 수 있습니다.
passwords 에 등록된 단어, (단어, 품사)만 출력됩니다.
from ckonlpy.tag import Postprocessor
passwords = {'아이오아이', ('정말', 'Noun')}
postprocessor = Postprocessor(twitter, passwords = passwords)
postprocessor.pos('우리아이오아이는 정말 이뻐요')
# [('아이오아이', 'Noun'), ('정말', 'Noun')]
stopwords 에 등록된 단어, (단어, 품사)는 출력되지 않습니다.
stopwords = {'는'}
postprocessor = Postprocessor(twitter, stopwords = stopwords)
postprocessor.pos('우리아이오아이는 정말 이뻐요')
# [('우리', 'Modifier'), ('아이오아이', 'Noun'), ('정말', 'Noun'), ('이뻐', 'Adjective'), ('요', 'Eomi')]
특정 품사를 지정하면, 해당 품사만 출력됩니다.
passtags = {'Noun'}
postprocessor = Postprocessor(twitter, passtags = passtags)
postprocessor.pos('우리아이오아이는 정말 이뻐요')
# [('아이오아이', 'Noun'), ('정말', 'Noun')]
치환할 단어, (단어, 품사)를 dict 형식으로 정의하면 tag 에서 단어가 치환되어 출력됩니다.
replace = {'아이오아이': '아이돌', ('이뻐', 'Adjective'): '예쁘다'}
postprocessor = Postprocessor(twitter, replace = replace)
postprocessor.pos('우리아이오아이는 정말 이뻐요')
# [('우리', 'Modifier'), ('아이돌', 'Noun'), ('는', 'Josa'), ('정말', 'Noun'), ('예쁘다', 'Adjective'), ('요', 'Eomi')]
연속된 단어를 하나의 단어루 묶기 위해서 nested tuple 이나 tuple of str 형식의 ngram 을 입력할 수 있습니다. tuple of str 의 형식으로 입력된 ngram 은 Noun 으로 인식됩니다.
ngrams = [(('미스', '함무라비'), 'Noun'), ('바람', '의', '나라')]
postprocessor = Postprocessor(twitter, ngrams = ngrams)
postprocessor.pos('미스 함무라비는 재밌는 드라마입니다')
# [('미스 - 함무라비', 'Noun'), ('는', 'Josa'), ('재밌는', 'Adjective'), ('드라마', 'Noun'), ('입니', 'Adjective'), ('다', 'Eomi')]
### Loading wordset
utils 에는 stopwords, passwords, replace word pair 를 파일로 저장하였을 경우, 이를 손쉽게 불러오는 함수가 있습니다.
load_wordset 은 set of str 혹은 set of tuple 을 return 합니다. 예시의 passwords.txt 의 내용은 아래와 같습니다. 단어의 품사는 한 칸 띄어쓰기로 구분합니다. stopwords.txt 도 동일한 포멧입니다.
아이오아이
아이오아이 Noun
공연
load_wordset 을 이용하는 예시코드 입니다.
from ckonlpy.utils import load_wordset
passwords = load_wordset('./passwords.txt')
print(passwords) # {('아이오아이', 'Noun'), '아이오아이', '공연'}
stopwords = load_wordset('./stopwords.txt')
print(stopwords) # {'은', '는', ('이', 'Josa')}
치환할 단어쌍은 tap 구분이 되어있습니다. 치환될 단어에 품사 태그가 있을 경우 한 칸 띄어쓰기로 구분합니다.
str\tstr
str str\tstr
아래는 replacewords.txt 의 예시입니다.
아빠 아버지
엄마 Noun 어머니
load_replace_wordpair 을 이용하는 예시코드 입니다.
from ckonlpy.utils import load_replace_wordpair
replace = load_replace_wordpair('./replacewords.txt')
print(replace) # {'아빠': '아버지', ('엄마', 'Noun'): '어머니'}
ngram 단어들의 각 단어는 한 칸 띄어쓰기로, ngram 의 품사는 tap 으로 구분되어 있습니다.
str str
str str\tstr
아래는 ngrams.txt 의 예시입니다.
바람 의 나라
미스 함무라비 Noun
load_ngram 을 이용하는 예시코드 입니다.
from ckonlpy.utils import load_ngram
ngrams = load_ngram('./ngrams.txt')
print(ngrams) # [('바람', '의', '나라'), (('미스', '함무라비'), 'Noun')]
## 0.0.6+ vs 0.0.5x
0.0.5x 에서의 변수와 함수의 이름, 변수의 타입 일부를 변경하였습니다.
| 변경 전 | 변경 후 |
| --- | --- |
| ckonlpy.tag.Twitter._loaded_twitter_default_dictionary | ckonlpy.tag.Twitter.use_twitter_dictionary |
| ckonlpy.tag.Twitter._dictionary | ckonlpy.tag.Twitter.dictionary |
| ckonlpy.tag.Twitter._customized_tagger | ckonlpy.tag.Twitter.template_tagger |
| ckonlpy.tag.Postprocessor.tag | ckonlpy.tag.Postprocessor.pos |
| ckonlpy.custom_tag.SimpleSelector | ckonlpy.custom_tag.SimpleEvalator |
| ckonlpy.custom_tag.SimpleSelector.score | ckonlpy.custom_tag.SimpleEvalator.evaluate |
| ckonlpy.tag.Twitter.set_selector | ckonlpy.tag.AbstractTagger.set_evaluator |
| ckonlpy.custom_tag.SimpleSelector.weight | ckonlpy.custom_tag.SimpleEvaluator.weight |
| 변경 후 | 변경 이유 |
| --- | --- |
| ckonlpy.tag.Twitter.use_twitter_dictionary | konlpy.tag.Twitter 의 사전 사용 유무 |
| ckonlpy.tag.Twitter.dictionary | public 으로 변환하였습니다 |
| ckonlpy.tag.Twitter.template_tagger | Template 기반으로 작동하는 tagger 임을 명시하고, public 으로 변환하였습니다 |
| ckonlpy.tag.Postprocessor.pos | 기본 tagger 의 결과를 후처리하는 기능이기 때문에 동일한 함수명으로 통일하였습니다 |
| ckonlpy.custom_tag.SimpleEvalator | 클래스 이름을 Selector 에서 Evaluator 로 변경하였습니다 |
| ckonlpy.custom_tag.SimpleEvalator.evaluate | 품사열 후보의 점수 계산 부분을 score --> evaluate 로 함수명을 변경하였습니다 |
| ckonlpy.tag.AbstractTagger.set_evaluator | 품사열 후보의 점수 계산 함수를 설정하는 함수의 이름을 변경하였습니다. 해당 함수는 ckonlpy.tag.Twitter 에서 ckonlpy.tag.AbstractTagger 로 이동하였습니다 |
| ckonlpy.custom_tag.SimpleEvaluator.weight | {str:float} 형식의 weight 를 [(str, float)] 형식으로 변경하였습니다 |
%package -n python3-customized-KoNLPy
Summary: KoNLPy wrapping package
Provides: python-customized-KoNLPy
BuildRequires: python3-devel
BuildRequires: python3-setuptools
BuildRequires: python3-pip
%description -n python3-customized-KoNLPy
# customized KoNLPy
한국어 자연어처리를 할 수 있는 파이썬 패키지, KoNLPy의 customized version입니다.
customized_KoNLPy는 확실히 알고 있는 단어들에 대해서는 라이브러리를 거치지 않고 주어진 어절을 아는 단어들로 토크나이징 / 품사판별을 하는 기능을 제공합니다. 이를 위해 template 기반 토크나이징을 수행합니다.
사전: {'아이오아이': 'Noun', '는': 'Josa'}
탬플릿: Noun + Josa
위와 같은 단어 리스트와 탬플릿이 있다면 '아이오아이는' 이라는 어절은 [('아이오아이', 'Noun'), ('는', 'Josa')]로 분리됩니다.
## Install
$ git clone https://github.com/lovit/customized_konlpy.git
$ pip install customized_konlpy
### Requires
- JPype >= 0.6.1
- KoNLPy >= 0.4.4
## Usage
### Part of speech tagging
KoNLPy와 동일하게 Twitter.pos(phrase)를 입력합니다. 각 어절별로 사용자 사전에 알려진 단어가 인식되면 customized_tagger로 어절을 분리하며, 사용자 사전에 알려지지 않은 단어로 구성된 어절은 트위터 형태소 분석기로 처리합니다.
twitter.pos('우리아이오아이는 이뻐요')
> [('우리', 'Noun'), ('아이오', 'Noun'), ('아이', 'Noun'), ('는', 'Josa'), ('이뻐', 'Adjective'), ('요', 'Eomi')]
'아이오아이'가 알려진 단어가 아니었기 때문에 트위터 분석기에서 단어를 제대로 인식하지 못합니다. 아래의 사용자 사전으로 단어 추가를 한 뒤 동일한 작업을 수행하면 아래와 같은 결과를 얻을 수 있습니다.
twitter.pos('우리아이오아이는 이뻐요')
> [('우리', 'Modifier'), ('아이오아이', 'Noun'), ('는', 'Josa'), ('이뻐', 'Adjective'), ('요', 'Eomi')]
twitter.pos('트와이스tt는 좋아요')
> [('트와이스', 'Noun'), ('tt', 'Noun'), ('는', 'Josa'), ('좋', 'Adjective'), ('아요', 'Eomi')]
### Add words to dictioanry
ckonlpy.tag의 Twitter는 add_dictionary를 통하여 str 혹은 list of str 형식의 사용자 사전을 추가할 수 있습니다.
from ckonlpy.tag import Twitter
twitter.add_dictionary('아이오아이', 'Noun')
twitter.add_dictionary(['트와이스', 'tt'], 'Noun')
트위터 한국어 분석기에서 이용하지 않는 품사 (단어 클래스)를 추가하고 싶을 경우에는 반드시 force=True로 설정해야 합니다.
twitter.add_dictionary('lovit', 'Name', force=True)
### Add template to customized tagger
현재 사용중인 탬플릿 기반 토크나이저는 코드 사용 중 탬플릿을 추가할 수 있습니다. 현재 사용중인 탬플릿의 리스트는 아래처럼 확인할 수 있습니다.
twitter.template_tagger.templates
> [('Noun', 'Josa'), ('Modifier', 'Noun'), ('Modifier', 'Noun', 'Josa')]
탬플릿은 tuple of str 형식으로 입력합니다.
twitter.template_tagger.add_a_template(('Noun', 'Noun', 'Josa'))
### Set templates tagger selector
Templates를 이용하여도 후보가 여러 개 나올 수 있습니다. 여러 개 후보 중에서 best 를 선택하는 함수를 직접 디자인 할 수 도 있습니다. 이처럼 몇 개의 점수 기준을 만들고, 각 기준의 weight를 부여하는 방식은 트위터 분석기에서 이용하는 방식인데, 직관적이고 튜닝 가능해서 매우 좋은 방식이라 생각합니다.
my_weights = [
('num_nouns', -0.1),
('num_words', -0.2),
('no_noun', -1),
('len_sum_of_nouns', 0.2)
]
def my_evaluate_function(candidate):
num_nouns = len([word for word, pos, begin, e in candidate if pos == 'Noun'])
num_words = len(candidate)
has_no_nouns = (num_nouns == 0)
len_sum_of_nouns = 0 if has_no_nouns else sum(
(len(word) for word, pos, _, _ in candidate if pos == 'Noun'))
scores = (num_nouns, num_words, has_no_nouns, len_sum_of_nouns)
score = sum((score * weight for score, (_, weight) in zip(scores, my_weights)))
return score
위의 예제처럼 my_weights 와 my_evaluate_function 함수를 정의하여 twitter.set_evaluator()에 입력하면, 해당 함수 기준으로 best candidate를 선택합니다.
twitter.set_evaluator(my_weights, my_evaluate_function)
### Postprocessor
passwords, stopwords, passtags, 단어 치환을 위한 후처리를 할 수 있습니다.
passwords 에 등록된 단어, (단어, 품사)만 출력됩니다.
from ckonlpy.tag import Postprocessor
passwords = {'아이오아이', ('정말', 'Noun')}
postprocessor = Postprocessor(twitter, passwords = passwords)
postprocessor.pos('우리아이오아이는 정말 이뻐요')
# [('아이오아이', 'Noun'), ('정말', 'Noun')]
stopwords 에 등록된 단어, (단어, 품사)는 출력되지 않습니다.
stopwords = {'는'}
postprocessor = Postprocessor(twitter, stopwords = stopwords)
postprocessor.pos('우리아이오아이는 정말 이뻐요')
# [('우리', 'Modifier'), ('아이오아이', 'Noun'), ('정말', 'Noun'), ('이뻐', 'Adjective'), ('요', 'Eomi')]
특정 품사를 지정하면, 해당 품사만 출력됩니다.
passtags = {'Noun'}
postprocessor = Postprocessor(twitter, passtags = passtags)
postprocessor.pos('우리아이오아이는 정말 이뻐요')
# [('아이오아이', 'Noun'), ('정말', 'Noun')]
치환할 단어, (단어, 품사)를 dict 형식으로 정의하면 tag 에서 단어가 치환되어 출력됩니다.
replace = {'아이오아이': '아이돌', ('이뻐', 'Adjective'): '예쁘다'}
postprocessor = Postprocessor(twitter, replace = replace)
postprocessor.pos('우리아이오아이는 정말 이뻐요')
# [('우리', 'Modifier'), ('아이돌', 'Noun'), ('는', 'Josa'), ('정말', 'Noun'), ('예쁘다', 'Adjective'), ('요', 'Eomi')]
연속된 단어를 하나의 단어루 묶기 위해서 nested tuple 이나 tuple of str 형식의 ngram 을 입력할 수 있습니다. tuple of str 의 형식으로 입력된 ngram 은 Noun 으로 인식됩니다.
ngrams = [(('미스', '함무라비'), 'Noun'), ('바람', '의', '나라')]
postprocessor = Postprocessor(twitter, ngrams = ngrams)
postprocessor.pos('미스 함무라비는 재밌는 드라마입니다')
# [('미스 - 함무라비', 'Noun'), ('는', 'Josa'), ('재밌는', 'Adjective'), ('드라마', 'Noun'), ('입니', 'Adjective'), ('다', 'Eomi')]
### Loading wordset
utils 에는 stopwords, passwords, replace word pair 를 파일로 저장하였을 경우, 이를 손쉽게 불러오는 함수가 있습니다.
load_wordset 은 set of str 혹은 set of tuple 을 return 합니다. 예시의 passwords.txt 의 내용은 아래와 같습니다. 단어의 품사는 한 칸 띄어쓰기로 구분합니다. stopwords.txt 도 동일한 포멧입니다.
아이오아이
아이오아이 Noun
공연
load_wordset 을 이용하는 예시코드 입니다.
from ckonlpy.utils import load_wordset
passwords = load_wordset('./passwords.txt')
print(passwords) # {('아이오아이', 'Noun'), '아이오아이', '공연'}
stopwords = load_wordset('./stopwords.txt')
print(stopwords) # {'은', '는', ('이', 'Josa')}
치환할 단어쌍은 tap 구분이 되어있습니다. 치환될 단어에 품사 태그가 있을 경우 한 칸 띄어쓰기로 구분합니다.
str\tstr
str str\tstr
아래는 replacewords.txt 의 예시입니다.
아빠 아버지
엄마 Noun 어머니
load_replace_wordpair 을 이용하는 예시코드 입니다.
from ckonlpy.utils import load_replace_wordpair
replace = load_replace_wordpair('./replacewords.txt')
print(replace) # {'아빠': '아버지', ('엄마', 'Noun'): '어머니'}
ngram 단어들의 각 단어는 한 칸 띄어쓰기로, ngram 의 품사는 tap 으로 구분되어 있습니다.
str str
str str\tstr
아래는 ngrams.txt 의 예시입니다.
바람 의 나라
미스 함무라비 Noun
load_ngram 을 이용하는 예시코드 입니다.
from ckonlpy.utils import load_ngram
ngrams = load_ngram('./ngrams.txt')
print(ngrams) # [('바람', '의', '나라'), (('미스', '함무라비'), 'Noun')]
## 0.0.6+ vs 0.0.5x
0.0.5x 에서의 변수와 함수의 이름, 변수의 타입 일부를 변경하였습니다.
| 변경 전 | 변경 후 |
| --- | --- |
| ckonlpy.tag.Twitter._loaded_twitter_default_dictionary | ckonlpy.tag.Twitter.use_twitter_dictionary |
| ckonlpy.tag.Twitter._dictionary | ckonlpy.tag.Twitter.dictionary |
| ckonlpy.tag.Twitter._customized_tagger | ckonlpy.tag.Twitter.template_tagger |
| ckonlpy.tag.Postprocessor.tag | ckonlpy.tag.Postprocessor.pos |
| ckonlpy.custom_tag.SimpleSelector | ckonlpy.custom_tag.SimpleEvalator |
| ckonlpy.custom_tag.SimpleSelector.score | ckonlpy.custom_tag.SimpleEvalator.evaluate |
| ckonlpy.tag.Twitter.set_selector | ckonlpy.tag.AbstractTagger.set_evaluator |
| ckonlpy.custom_tag.SimpleSelector.weight | ckonlpy.custom_tag.SimpleEvaluator.weight |
| 변경 후 | 변경 이유 |
| --- | --- |
| ckonlpy.tag.Twitter.use_twitter_dictionary | konlpy.tag.Twitter 의 사전 사용 유무 |
| ckonlpy.tag.Twitter.dictionary | public 으로 변환하였습니다 |
| ckonlpy.tag.Twitter.template_tagger | Template 기반으로 작동하는 tagger 임을 명시하고, public 으로 변환하였습니다 |
| ckonlpy.tag.Postprocessor.pos | 기본 tagger 의 결과를 후처리하는 기능이기 때문에 동일한 함수명으로 통일하였습니다 |
| ckonlpy.custom_tag.SimpleEvalator | 클래스 이름을 Selector 에서 Evaluator 로 변경하였습니다 |
| ckonlpy.custom_tag.SimpleEvalator.evaluate | 품사열 후보의 점수 계산 부분을 score --> evaluate 로 함수명을 변경하였습니다 |
| ckonlpy.tag.AbstractTagger.set_evaluator | 품사열 후보의 점수 계산 함수를 설정하는 함수의 이름을 변경하였습니다. 해당 함수는 ckonlpy.tag.Twitter 에서 ckonlpy.tag.AbstractTagger 로 이동하였습니다 |
| ckonlpy.custom_tag.SimpleEvaluator.weight | {str:float} 형식의 weight 를 [(str, float)] 형식으로 변경하였습니다 |
%package help
Summary: Development documents and examples for customized-KoNLPy
Provides: python3-customized-KoNLPy-doc
%description help
# customized KoNLPy
한국어 자연어처리를 할 수 있는 파이썬 패키지, KoNLPy의 customized version입니다.
customized_KoNLPy는 확실히 알고 있는 단어들에 대해서는 라이브러리를 거치지 않고 주어진 어절을 아는 단어들로 토크나이징 / 품사판별을 하는 기능을 제공합니다. 이를 위해 template 기반 토크나이징을 수행합니다.
사전: {'아이오아이': 'Noun', '는': 'Josa'}
탬플릿: Noun + Josa
위와 같은 단어 리스트와 탬플릿이 있다면 '아이오아이는' 이라는 어절은 [('아이오아이', 'Noun'), ('는', 'Josa')]로 분리됩니다.
## Install
$ git clone https://github.com/lovit/customized_konlpy.git
$ pip install customized_konlpy
### Requires
- JPype >= 0.6.1
- KoNLPy >= 0.4.4
## Usage
### Part of speech tagging
KoNLPy와 동일하게 Twitter.pos(phrase)를 입력합니다. 각 어절별로 사용자 사전에 알려진 단어가 인식되면 customized_tagger로 어절을 분리하며, 사용자 사전에 알려지지 않은 단어로 구성된 어절은 트위터 형태소 분석기로 처리합니다.
twitter.pos('우리아이오아이는 이뻐요')
> [('우리', 'Noun'), ('아이오', 'Noun'), ('아이', 'Noun'), ('는', 'Josa'), ('이뻐', 'Adjective'), ('요', 'Eomi')]
'아이오아이'가 알려진 단어가 아니었기 때문에 트위터 분석기에서 단어를 제대로 인식하지 못합니다. 아래의 사용자 사전으로 단어 추가를 한 뒤 동일한 작업을 수행하면 아래와 같은 결과를 얻을 수 있습니다.
twitter.pos('우리아이오아이는 이뻐요')
> [('우리', 'Modifier'), ('아이오아이', 'Noun'), ('는', 'Josa'), ('이뻐', 'Adjective'), ('요', 'Eomi')]
twitter.pos('트와이스tt는 좋아요')
> [('트와이스', 'Noun'), ('tt', 'Noun'), ('는', 'Josa'), ('좋', 'Adjective'), ('아요', 'Eomi')]
### Add words to dictioanry
ckonlpy.tag의 Twitter는 add_dictionary를 통하여 str 혹은 list of str 형식의 사용자 사전을 추가할 수 있습니다.
from ckonlpy.tag import Twitter
twitter.add_dictionary('아이오아이', 'Noun')
twitter.add_dictionary(['트와이스', 'tt'], 'Noun')
트위터 한국어 분석기에서 이용하지 않는 품사 (단어 클래스)를 추가하고 싶을 경우에는 반드시 force=True로 설정해야 합니다.
twitter.add_dictionary('lovit', 'Name', force=True)
### Add template to customized tagger
현재 사용중인 탬플릿 기반 토크나이저는 코드 사용 중 탬플릿을 추가할 수 있습니다. 현재 사용중인 탬플릿의 리스트는 아래처럼 확인할 수 있습니다.
twitter.template_tagger.templates
> [('Noun', 'Josa'), ('Modifier', 'Noun'), ('Modifier', 'Noun', 'Josa')]
탬플릿은 tuple of str 형식으로 입력합니다.
twitter.template_tagger.add_a_template(('Noun', 'Noun', 'Josa'))
### Set templates tagger selector
Templates를 이용하여도 후보가 여러 개 나올 수 있습니다. 여러 개 후보 중에서 best 를 선택하는 함수를 직접 디자인 할 수 도 있습니다. 이처럼 몇 개의 점수 기준을 만들고, 각 기준의 weight를 부여하는 방식은 트위터 분석기에서 이용하는 방식인데, 직관적이고 튜닝 가능해서 매우 좋은 방식이라 생각합니다.
my_weights = [
('num_nouns', -0.1),
('num_words', -0.2),
('no_noun', -1),
('len_sum_of_nouns', 0.2)
]
def my_evaluate_function(candidate):
num_nouns = len([word for word, pos, begin, e in candidate if pos == 'Noun'])
num_words = len(candidate)
has_no_nouns = (num_nouns == 0)
len_sum_of_nouns = 0 if has_no_nouns else sum(
(len(word) for word, pos, _, _ in candidate if pos == 'Noun'))
scores = (num_nouns, num_words, has_no_nouns, len_sum_of_nouns)
score = sum((score * weight for score, (_, weight) in zip(scores, my_weights)))
return score
위의 예제처럼 my_weights 와 my_evaluate_function 함수를 정의하여 twitter.set_evaluator()에 입력하면, 해당 함수 기준으로 best candidate를 선택합니다.
twitter.set_evaluator(my_weights, my_evaluate_function)
### Postprocessor
passwords, stopwords, passtags, 단어 치환을 위한 후처리를 할 수 있습니다.
passwords 에 등록된 단어, (단어, 품사)만 출력됩니다.
from ckonlpy.tag import Postprocessor
passwords = {'아이오아이', ('정말', 'Noun')}
postprocessor = Postprocessor(twitter, passwords = passwords)
postprocessor.pos('우리아이오아이는 정말 이뻐요')
# [('아이오아이', 'Noun'), ('정말', 'Noun')]
stopwords 에 등록된 단어, (단어, 품사)는 출력되지 않습니다.
stopwords = {'는'}
postprocessor = Postprocessor(twitter, stopwords = stopwords)
postprocessor.pos('우리아이오아이는 정말 이뻐요')
# [('우리', 'Modifier'), ('아이오아이', 'Noun'), ('정말', 'Noun'), ('이뻐', 'Adjective'), ('요', 'Eomi')]
특정 품사를 지정하면, 해당 품사만 출력됩니다.
passtags = {'Noun'}
postprocessor = Postprocessor(twitter, passtags = passtags)
postprocessor.pos('우리아이오아이는 정말 이뻐요')
# [('아이오아이', 'Noun'), ('정말', 'Noun')]
치환할 단어, (단어, 품사)를 dict 형식으로 정의하면 tag 에서 단어가 치환되어 출력됩니다.
replace = {'아이오아이': '아이돌', ('이뻐', 'Adjective'): '예쁘다'}
postprocessor = Postprocessor(twitter, replace = replace)
postprocessor.pos('우리아이오아이는 정말 이뻐요')
# [('우리', 'Modifier'), ('아이돌', 'Noun'), ('는', 'Josa'), ('정말', 'Noun'), ('예쁘다', 'Adjective'), ('요', 'Eomi')]
연속된 단어를 하나의 단어루 묶기 위해서 nested tuple 이나 tuple of str 형식의 ngram 을 입력할 수 있습니다. tuple of str 의 형식으로 입력된 ngram 은 Noun 으로 인식됩니다.
ngrams = [(('미스', '함무라비'), 'Noun'), ('바람', '의', '나라')]
postprocessor = Postprocessor(twitter, ngrams = ngrams)
postprocessor.pos('미스 함무라비는 재밌는 드라마입니다')
# [('미스 - 함무라비', 'Noun'), ('는', 'Josa'), ('재밌는', 'Adjective'), ('드라마', 'Noun'), ('입니', 'Adjective'), ('다', 'Eomi')]
### Loading wordset
utils 에는 stopwords, passwords, replace word pair 를 파일로 저장하였을 경우, 이를 손쉽게 불러오는 함수가 있습니다.
load_wordset 은 set of str 혹은 set of tuple 을 return 합니다. 예시의 passwords.txt 의 내용은 아래와 같습니다. 단어의 품사는 한 칸 띄어쓰기로 구분합니다. stopwords.txt 도 동일한 포멧입니다.
아이오아이
아이오아이 Noun
공연
load_wordset 을 이용하는 예시코드 입니다.
from ckonlpy.utils import load_wordset
passwords = load_wordset('./passwords.txt')
print(passwords) # {('아이오아이', 'Noun'), '아이오아이', '공연'}
stopwords = load_wordset('./stopwords.txt')
print(stopwords) # {'은', '는', ('이', 'Josa')}
치환할 단어쌍은 tap 구분이 되어있습니다. 치환될 단어에 품사 태그가 있을 경우 한 칸 띄어쓰기로 구분합니다.
str\tstr
str str\tstr
아래는 replacewords.txt 의 예시입니다.
아빠 아버지
엄마 Noun 어머니
load_replace_wordpair 을 이용하는 예시코드 입니다.
from ckonlpy.utils import load_replace_wordpair
replace = load_replace_wordpair('./replacewords.txt')
print(replace) # {'아빠': '아버지', ('엄마', 'Noun'): '어머니'}
ngram 단어들의 각 단어는 한 칸 띄어쓰기로, ngram 의 품사는 tap 으로 구분되어 있습니다.
str str
str str\tstr
아래는 ngrams.txt 의 예시입니다.
바람 의 나라
미스 함무라비 Noun
load_ngram 을 이용하는 예시코드 입니다.
from ckonlpy.utils import load_ngram
ngrams = load_ngram('./ngrams.txt')
print(ngrams) # [('바람', '의', '나라'), (('미스', '함무라비'), 'Noun')]
## 0.0.6+ vs 0.0.5x
0.0.5x 에서의 변수와 함수의 이름, 변수의 타입 일부를 변경하였습니다.
| 변경 전 | 변경 후 |
| --- | --- |
| ckonlpy.tag.Twitter._loaded_twitter_default_dictionary | ckonlpy.tag.Twitter.use_twitter_dictionary |
| ckonlpy.tag.Twitter._dictionary | ckonlpy.tag.Twitter.dictionary |
| ckonlpy.tag.Twitter._customized_tagger | ckonlpy.tag.Twitter.template_tagger |
| ckonlpy.tag.Postprocessor.tag | ckonlpy.tag.Postprocessor.pos |
| ckonlpy.custom_tag.SimpleSelector | ckonlpy.custom_tag.SimpleEvalator |
| ckonlpy.custom_tag.SimpleSelector.score | ckonlpy.custom_tag.SimpleEvalator.evaluate |
| ckonlpy.tag.Twitter.set_selector | ckonlpy.tag.AbstractTagger.set_evaluator |
| ckonlpy.custom_tag.SimpleSelector.weight | ckonlpy.custom_tag.SimpleEvaluator.weight |
| 변경 후 | 변경 이유 |
| --- | --- |
| ckonlpy.tag.Twitter.use_twitter_dictionary | konlpy.tag.Twitter 의 사전 사용 유무 |
| ckonlpy.tag.Twitter.dictionary | public 으로 변환하였습니다 |
| ckonlpy.tag.Twitter.template_tagger | Template 기반으로 작동하는 tagger 임을 명시하고, public 으로 변환하였습니다 |
| ckonlpy.tag.Postprocessor.pos | 기본 tagger 의 결과를 후처리하는 기능이기 때문에 동일한 함수명으로 통일하였습니다 |
| ckonlpy.custom_tag.SimpleEvalator | 클래스 이름을 Selector 에서 Evaluator 로 변경하였습니다 |
| ckonlpy.custom_tag.SimpleEvalator.evaluate | 품사열 후보의 점수 계산 부분을 score --> evaluate 로 함수명을 변경하였습니다 |
| ckonlpy.tag.AbstractTagger.set_evaluator | 품사열 후보의 점수 계산 함수를 설정하는 함수의 이름을 변경하였습니다. 해당 함수는 ckonlpy.tag.Twitter 에서 ckonlpy.tag.AbstractTagger 로 이동하였습니다 |
| ckonlpy.custom_tag.SimpleEvaluator.weight | {str:float} 형식의 weight 를 [(str, float)] 형식으로 변경하였습니다 |
%prep
%autosetup -n customized-KoNLPy-0.0.64
%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-customized-KoNLPy -f filelist.lst
%dir %{python3_sitelib}/*
%files help -f doclist.lst
%{_docdir}/*
%changelog
* Mon May 15 2023 Python_Bot <Python_Bot@openeuler.org> - 0.0.64-1
- Package Spec generated
|