-
Notifications
You must be signed in to change notification settings - Fork 356
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cedict reader #138
Cedict reader #138
Changes from 7 commits
0ae1ffc
883041e
57c4bc3
c4c53bd
4cbe53d
349be0f
7c93cec
591c045
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,252 @@ | ||
""" | ||
CC-CEDICT is a continuation of the CEDICT project started by Paul Denisowski in 1997 with the aim | ||
to provide a complete downloadable Chinese to English dictionary with pronunciation in pinyin | ||
for the Chinese characters. | ||
|
||
Creative Commons Attribution-Share Alike 3.0 | ||
http://creativecommons.org/licenses/by-sa/3.0/ | ||
|
||
Referenced works: | ||
CEDICT - Copyright (C) 1997, 1998 Paul Andrew Denisowski | ||
|
||
CC-CEDICT can be downloaded from: | ||
http://www.mdbg.net/chindict/chindict.php?page=cc-cedict | ||
""" | ||
|
||
import gzip | ||
import re | ||
|
||
from conceptnet5.edges import make_edge | ||
from conceptnet5.formats.msgpack_stream import MsgpackStreamWriter | ||
from conceptnet5.nodes import standardized_concept_uri | ||
from conceptnet5.uri import Licenses | ||
|
||
DATASET = '/d/cc_cedict' | ||
LICENSE = Licenses.cc_sharealike | ||
SOURCE = [{'contributor': '/s/resource/cc_cedict/2017-10'}] | ||
|
||
LINE_REGEX = re.compile(r'(.+)\s(.+)\[.+\]\s/(.+)/') # separate traditional and simplified words | ||
DATE_RANGE_REGEX = re.compile(r'(.+?)\s\(.+\d.+\),') # date range | ||
PAREN_REGEX = re.compile(r'\(.+?\)') # parenthesis | ||
CHINESE_CHAR_REGEX = re.compile(r'([\u4e00-\u9fff]+[\|·]?)+') # Chinese characters | ||
BRACKETS_REGEX = re.compile(r'\[.+\]') # pronunciation | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this regex is too greedy and should have a I believe that in this definition:
it will match this text:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This particular definition would actually have everything inside the parentheses removed before matching the brackets, but the problem was true for a couple of other definitions, so I changed it. |
||
VARIANT_REGEX = re.compile(r'(see (also )?|(old )?variant of |archaic version of |also written)') | ||
LIT_FIG_REGEX = re.compile(r'(\b|\s)(fig|lit).\s') | ||
ABBR_REGEX = re.compile(r'(\b|\s)abbr. (to|of|for)') | ||
|
||
|
||
def remove_reference_syntax(definition): | ||
""" | ||
Example: Jiajiang county in Leshan 樂山|乐山[Le4 shan1] | ||
""" | ||
definition = CHINESE_CHAR_REGEX.sub('', definition) | ||
return BRACKETS_REGEX.sub('', definition) | ||
|
||
|
||
def remove_additional_info(definition): | ||
""" | ||
Remove the second sentence of the definition | ||
""" | ||
return definition.split(',')[0] | ||
|
||
|
||
def extract_person(match): | ||
""" | ||
Example: "Pierre-Auguste Renoir (1841-1919), French impressionist painter" | ||
Check if a date range is mentioned in a definition. This is usually the case when a person is | ||
being defined. In that case, we want to only extract the name, without the date range or the | ||
second, CV sentence. | ||
|
||
Returns: | ||
a list of names extracted from a definition | ||
""" | ||
person = match.groups()[0] | ||
if ',' in person: | ||
person = remove_additional_info(person) # skip the second sentence | ||
|
||
person = CHINESE_CHAR_REGEX.sub('', person) | ||
person = BRACKETS_REGEX.sub('', person) # delete pronunciation | ||
person = person.split(' or ') # Take care of "Frederic Chopin or Fryderyk Franciszek Chopin" | ||
return person | ||
|
||
|
||
def extract_measure_words(definition): | ||
""" | ||
Example: "CL:枝[zhi1],根[gen1],個|个[ge4],把[ba3]" | ||
""" | ||
words = definition[3:] # skip 'CL:' | ||
words = words.split(',') | ||
words = [BRACKETS_REGEX.sub('', word) for word in words] | ||
measure_words = [] | ||
for word in words: | ||
measure_words.extend(word.split('|')) | ||
return measure_words | ||
|
||
|
||
def extract_variants(definition): | ||
""" | ||
Example: "variant of 齊大非偶|齐大非偶[qi2 da4 fei1 ou3]" | ||
""" | ||
variants = VARIANT_REGEX.sub('', definition) | ||
variants = BRACKETS_REGEX.sub('', variants) | ||
variants = variants.split('|') | ||
return variants | ||
|
||
|
||
def extract_abbreviations(definition): | ||
""" | ||
abbr.for Luxembourg 盧森堡 | 卢森堡[Lu2 sen1 bao3] | ||
Only return a Chinese for which this word is an abbreviation. | ||
""" | ||
reference = re.search(CHINESE_CHAR_REGEX, definition) | ||
if reference: | ||
reference = reference.group(0) | ||
reference = reference.split('|') | ||
return reference | ||
return | ||
|
||
|
||
def handle_file(filename, output_file): | ||
out = MsgpackStreamWriter(output_file) | ||
|
||
for line in gzip.open(filename, 'rt'): | ||
|
||
# skip the intro information | ||
if line.startswith('#'): | ||
continue | ||
|
||
# parse the data to extract the traditional form, simplified form and the English definition | ||
traditional, simplified, definitions = re.match(LINE_REGEX, line).groups() | ||
|
||
# Make an edge between the traditional and simplified version | ||
edge = make_edge(rel='/r/Synonym', | ||
start=standardized_concept_uri('zh-Hant', traditional), | ||
end=standardized_concept_uri('zh-Hans', simplified), | ||
dataset=DATASET, | ||
license=LICENSE, | ||
sources=SOURCE) | ||
out.write(edge) | ||
|
||
definitions = re.split(r'\/|;', definitions) | ||
for definition in definitions: | ||
|
||
# Skip pronunciation information | ||
if 'Taiwan pr.' in definition or 'also pr.' in definition: | ||
continue | ||
|
||
# Check if it's the definition matches a person syntax, i.e. includes a date range | ||
person_match = re.match(DATE_RANGE_REGEX, definition) | ||
if person_match: | ||
persons = extract_person(person_match) | ||
for person in persons: | ||
edge = make_edge(rel='/r/Synonym', | ||
start=standardized_concept_uri('zh-Hant', traditional), | ||
end=standardized_concept_uri('en', person), | ||
dataset=DATASET, | ||
license=LICENSE, | ||
sources=SOURCE) | ||
out.write(edge) | ||
|
||
edge = make_edge(rel='/r/Synonym', | ||
start=standardized_concept_uri('zh-Hans', simplified), | ||
end=standardized_concept_uri('en', person), | ||
dataset=DATASET, | ||
license=LICENSE, | ||
sources=SOURCE) | ||
out.write(edge) | ||
continue | ||
|
||
# Remove clarifying information in parenthesis | ||
definition = PAREN_REGEX.sub('', definition) | ||
|
||
# Check if a word is a measure word | ||
if definition.startswith('CL:'): | ||
related_words = extract_measure_words(definition) | ||
for word in related_words: | ||
edge = make_edge(rel='/r/RelatedTo', | ||
start=standardized_concept_uri('zh-Hant', traditional), | ||
end=standardized_concept_uri('zh', word), | ||
dataset=DATASET, | ||
license=LICENSE, | ||
sources=SOURCE) | ||
out.write(edge) | ||
|
||
edge = make_edge(rel='/r/RelatedTo', | ||
start=standardized_concept_uri('zh-Hans', simplified), | ||
end=standardized_concept_uri('zh', word), | ||
dataset=DATASET, | ||
license=LICENSE, | ||
sources=SOURCE) | ||
out.write(edge) | ||
continue | ||
|
||
# Check if a word is a form/variant of a different word | ||
variant_match = re.match(VARIANT_REGEX, definition) | ||
if variant_match: | ||
variants = extract_variants(definition) | ||
for variant in variants: | ||
edge = make_edge(rel='/r/RelatedTo', | ||
start=standardized_concept_uri('zh-Hant', traditional), | ||
end=standardized_concept_uri('zh', variant), | ||
dataset=DATASET, | ||
license=LICENSE, | ||
sources=SOURCE) | ||
out.write(edge) | ||
|
||
edge = make_edge(rel='/r/RelatedTo', | ||
start=standardized_concept_uri('zh-Hans', simplified), | ||
end=standardized_concept_uri('zh', variant), | ||
dataset=DATASET, | ||
license=LICENSE, | ||
sources=SOURCE) | ||
out.write(edge) | ||
continue | ||
|
||
# Handle abbreviations | ||
if re.match(ABBR_REGEX, definition): | ||
abbreviations = extract_abbreviations(definition) | ||
if abbreviations: | ||
for abbr in abbreviations: | ||
edge = make_edge(rel='/r/RelatedTo', | ||
start=standardized_concept_uri('zh-Hant', traditional), | ||
end=standardized_concept_uri('zh', abbr), | ||
dataset=DATASET, | ||
license=LICENSE, | ||
sources=SOURCE) | ||
out.write(edge) | ||
|
||
edge = make_edge(rel='/r/RelatedTo', | ||
start=standardized_concept_uri('zh-Hans', simplified), | ||
end=standardized_concept_uri('zh', abbr), | ||
dataset=DATASET, | ||
license=LICENSE, | ||
sources=SOURCE) | ||
out.write(edge) | ||
continue | ||
|
||
# Remove 'lit.', 'fig.' | ||
definition = LIT_FIG_REGEX.sub('', definition) | ||
|
||
# Expand sth and sb | ||
definition = definition.replace('sth', 'something') | ||
definition = definition.replace('sb', 'someone') | ||
definition = remove_reference_syntax(definition) | ||
definition = remove_additional_info(definition) | ||
|
||
# Skip long definitions | ||
if len(definition.split()) < 6: | ||
edge = make_edge(rel='/r/Synonym', | ||
start=standardized_concept_uri('zh-Hant', traditional), | ||
end=standardized_concept_uri('en', definition), | ||
dataset=DATASET, | ||
license=LICENSE, | ||
sources=SOURCE) | ||
out.write(edge) | ||
|
||
edge = make_edge(rel='/r/RelatedTo', | ||
start=standardized_concept_uri('zh-Hans', simplified), | ||
end=standardized_concept_uri('en', definition), | ||
dataset=DATASET, | ||
license=LICENSE, | ||
sources=SOURCE) | ||
out.write(edge) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This appears to exclude the range from U+3400..U+4DBF, which appears in definitions such as:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For what it's worth, it also excludes the other CJK extensions with codepoints U+20000 and up, but CEDICT never uses those anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That was correct for a couple of definitions, so I ended up switching to
regex.compile('([\p{IsIdeo}]+[\|·]?)+')
, per your suggestion.