| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
import os, sys, re |
|---|
| 4 |
from popen2 import * |
|---|
| 5 |
|
|---|
| 6 |
prefix = 'xml' |
|---|
| 7 |
lang_re = re.compile('^(\w*)-(\w*)$') |
|---|
| 8 |
|
|---|
| 9 |
if len(sys.argv) == 2: |
|---|
| 10 |
steps = eval(sys.argv[1]) |
|---|
| 11 |
else: |
|---|
| 12 |
steps = range(1, 8) |
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
def gunzip(path): |
|---|
| 16 |
for item in os.listdir(path): |
|---|
| 17 |
item = os.path.join(path, item) |
|---|
| 18 |
if os.path.isdir(item): |
|---|
| 19 |
gunzip(item) |
|---|
| 20 |
else: |
|---|
| 21 |
if item[-3:] == '.gz': |
|---|
| 22 |
command = 'gunzip %s' % (item,) |
|---|
| 23 |
print command |
|---|
| 24 |
os.system(command) |
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
def xces2text(files, corpus, lang1, lang2): |
|---|
| 28 |
lang1_file = open('%s.%s' % (corpus, lang1), 'w+') |
|---|
| 29 |
lang2_file = open('%s.%s' % (corpus, lang2), 'w+') |
|---|
| 30 |
for file in files: |
|---|
| 31 |
command = '../bin/xces2text.perl < %s' % (file) |
|---|
| 32 |
print command |
|---|
| 33 |
in_file, out_file = popen2(command) |
|---|
| 34 |
is_lang1 = True |
|---|
| 35 |
for line in in_file.readlines(): |
|---|
| 36 |
if line.startswith('- '): |
|---|
| 37 |
line = line[2:] |
|---|
| 38 |
if is_lang1: |
|---|
| 39 |
lang1_file.write(line.lower()) |
|---|
| 40 |
else: |
|---|
| 41 |
lang2_file.write(line.lower()) |
|---|
| 42 |
is_lang1 = not is_lang1 |
|---|
| 43 |
in_file.close() |
|---|
| 44 |
out_file.close() |
|---|
| 45 |
lang1_file.close() |
|---|
| 46 |
lang2_file.close() |
|---|
| 47 |
|
|---|
| 48 |
for lang in os.listdir(prefix): |
|---|
| 49 |
match = lang_re.match(lang) |
|---|
| 50 |
if match: |
|---|
| 51 |
lang1 = match.group(1) |
|---|
| 52 |
lang2 = match.group(2) |
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
gunzip(os.path.join(prefix, lang)) |
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
files = [] |
|---|
| 59 |
os.chdir(os.path.join(prefix, lang)) |
|---|
| 60 |
for genre in os.listdir('.'): |
|---|
| 61 |
os.chdir(genre) |
|---|
| 62 |
for year in os.listdir('.'): |
|---|
| 63 |
os.chdir(year) |
|---|
| 64 |
for file in os.listdir('.'): |
|---|
| 65 |
files.append(os.path.join(prefix, lang, genre, year, file)) |
|---|
| 66 |
os.chdir('..') |
|---|
| 67 |
os.chdir('..') |
|---|
| 68 |
os.chdir('../..') |
|---|
| 69 |
|
|---|
| 70 |
xces2text(files, '%s' % (lang,), lang1, lang2) |
|---|