haven-jeon / PyKoSpacing

Automatic Korean word spacing with Python
GNU General Public License v3.0
397 stars 118 forks source link

added function to ignore spaces on non-korean and non-ascii symbols #58

Closed ksh108405 closed 1 year ago

ksh108405 commented 1 year ago

Created a simple function to solve #52. This code add new parameter only_ko on __call__ of Spacing class. The only_ko parameter works on make_pred_sents method, and when it is True, It only put spaces when current character is (korean or ascii-symbol) or next character is (korean or ascii-symbol). default is set to False, so I expect there's no compatibility issues.

ksh108405 commented 1 year ago

I'm improving it by completely subtract non-korean or non-ascii-symbol before model prediction, then merges it again on make_pred_sents. It will be able to solve the issue better.

ksh108405 commented 1 year ago

Now support customizable pattern to ignore (not only Korean or ascii-symbol), based on regex pattern. (default is my own regex to match non-Korean and non-ascii symbol characters, words, or a sentence.)

Added both pre-processing (delete characters before model prediction, then merges it), and post-processing (ignore spaces on characters which match with pattern).

However, the issue still remains both pre-processing and post-processing. The former has an issue of whether to put space before the subtracted text or after of it. The latter not completely solve #52, since English in model input can cause incorrect outputs. So, I added another option to combine both, which the model predicts on both pre-processed text and original text. It works like pre-processing strategy, but uses predictions from the original text to whether to put space after or before the subtracted text. (But it has to predict twice, which almost doubles the time consumption.)

Both can be turned on by the ignore parameter. ignore=none: default ignore=pre: ignore by pre-processing (space is inserted after the subtracted text. (can be wrong.)) ignore=pre2: combined (see above) ignore=post: ignore by post-processing

ksh108405 commented 1 year ago

Example

Input 친구와함께bmw썬바이저를썼다. ignore=none 친구와 함께 bm w 썬바이저를 썼다. ignore=pre 친구와 함께bmw 썬바이저를 썼다. ignore=pre2 친구와 함께 bmw 썬바이저를 썼다. ignore=post 친구와 함께 bm w 썬바이저를 썼다.

Input chicken박스를열고닭다리를꺼내입에문다.crispy한튀김옷덕에내입주변은glossy해진다. ignore=none chicken박스를 열고 닭다리를 꺼내 입에 문다. crispy 한튀김 옷 덕에 내 입 주변은 glossy해진다. ignore=pre chicken박스를 열고 닭다리를 꺼내 입에 문다.crispy 한 튀김옷 덕에 내 입 주변은glossy 해진다. ignore=pre2 chicken박스를 열고 닭다리를 꺼내 입에 문다. crispy 한 튀김옷 덕에 내 입 주변은 glossy해진다. ignore=post chicken박스를 열고 닭다리를 꺼내 입에 문다. crispy 한튀김 옷 덕에 내 입 주변은 glossy해진다.

Input 김형호영화시장분석가는'1987'의네이버영화정보네티즌10점평에서언급된단어들을지난해12월27일부터올해1월10일까지통계프로그램R과KoNLP패키지로텍스트마이닝하여분석했다. ignore=none 김형호 영화시장 분석가는 '1987'의 네이버 영화 정보 네티즌 10점 평에서 언급된 단어들을 지난해 12월 27일부터 올해 1월 10일까지 통계 프로그램 R과 KoNLP 패키지로 텍스트마이닝하여 분석했다. ignore=pre 김형호 영화시장 분석가는 '1987'의 네이버 영화 정보 네티즌 10점 평에서 언급된 단어들을 지난해 12월 27일부터 올해 1월 10일까지 통계 프로그램R과KoNLP 패키지로 텍스트마이닝하여 분석했다. ignore=pre2 김형호 영화시장 분석가는 '1987'의 네이버 영화 정보 네티즌 10점 평에서 언급된 단어들을 지난해 12월 27일부터 올해 1월 10일까지 통계 프로그램 R과 KoNLP 패키지로 텍스트마이닝하여 분석했다. ignore=post 김형호 영화시장 분석가는 '1987'의 네이버 영화 정보 네티즌 10점 평에서 언급된 단어들을 지난해 12월 27일부터 올해 1월 10일까지 통계 프로그램 R과 KoNLP 패키지로 텍스트마이닝하여 분석했다.

haven-jeon commented 1 year ago

PR 주셔서 감사합니다. 사용자 분들의 이해를 돕기 위해 README.md 업데이트 부탁드려도 될까요?

ksh108405 commented 1 year ago

Updated README.md with explanations and examples for ignore and ignore_pattern parameter.

haven-jeon commented 1 year ago

감사합니다. merge 했어요..