여기서 문제는 일부 문자에 대해 하나 이상의 유니 코드 코드 포인트를 사용하는 유니 코드 텍스트 (utf-8에 대해 잊어 버려야합니다. 데이터를 올바른 파이썬 3 문자열로 디코딩 한 후 이야기하는 것입니다)가 ' 따라서 "±"와 "å"는 적절한 정규화 후에 하나의 문자로 존재할 수 있지만, 두 개의 마크를 모두 가지고있는 문자는 유니 코드에서 "결합 마크"문자 중 하나를 사용해야합니다.
즉, 파이썬 Counter
만으로는 최소한 추가 단계없이이 코드를 처리 할 수 있습니다. 파이썬 코드에서이 마커 문자에 대해 알아내는 방법은
입니다. 따라서 할 수있는 한 가지 방법은 텍스트를 사전 처리하는 것입니다. "순수 파이썬"코드를 사용하여 각 문자와 마킹이 정규화 된 목록입니다. 그러면 카운터가 그 일을 할 수 있습니다.
그것은을 따라 뭔가있을 수 :
이
import unicodedata
from collections import Counter
characters = []
text = ...
# Decompose all characters into plain letters + marking diacritics:
text = unicodedata.normalize("NFD", text)
for character in text:
if unicodedata.category(character)[0] == "M":
# character is a composing mark, so agregate it with
# previous character
characters[-1] += character
else:
characters.append(character)
counting = Counter(characters)
발견해 주셔서 감사합니다. – jsbueno
감사합니다. 테스트를 거쳤고 이중 마커가있는 글자에도 효과적입니다. :) 'unicodedata.normalize' 및'unicodedata.category'를 확인해야하므로 다른 사람이 필요하면 링크가 있습니다. https://docs.python.org/2/library/unicodedata.html#unicodedata.normal https://docs.python.org/2/library/unicodedata.html#unicodedata.category http : // www .fileformat.info/info/unicode/category/index.htm 도움을 주셔서 감사합니다. – user11448