-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverbs_imp.py
59 lines (52 loc) · 2.61 KB
/
verbs_imp.py
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
from bs4 import BeautifulSoup
import re
import csv
import os
import logging
import pymorphy2
morph = pymorphy2.MorphAnalyzer()
table = [['File Name', 'Token', 'Lemma']]
if not os.path.exists('out_verbs_imp'):
os.mkdir('out_verbs_imp')
if not os.path.exists('logs'):
os.mkdir('logs')
logging.basicConfig(filename="logs/errors_verbs_imp.log", filemode="w", level=logging.ERROR)
log = logging.getLogger("ex")
for file in os.listdir('in'):
singleFileList = ''
try:
with open('in/' + file, 'r', encoding='utf8') as eafFile:
eaf = eafFile.read()
eaf = eaf.replace('part_of_speech', 'part of speech')
eaf = eaf.replace('parts_of_speech', 'part of speech')
eaf = eaf.replace('parts of speech', 'part of speech')
eaf = BeautifulSoup(eaf, 'xml')
grammaticalFeatures = eaf.find_all(LINGUISTIC_TYPE_REF=re.compile("(grammatical features)|(gram)|(grammatical_features)"))
for tier in grammaticalFeatures:
annotations = tier.find_all('ANNOTATION_VALUE', string=re.compile('Mood=Imp'))
for annotation in annotations:
try:
id = annotation.parent['ANNOTATION_REF']
except KeyError:
ts1 = annotation.parent['TIME_SLOT_REF1']
ts2 = annotation.parent['TIME_SLOT_REF2']
id = eaf.select_one('TIER[LINGUISTIC_TYPE_REF="words"] ALIGNABLE_ANNOTATION[TIME_SLOT_REF1="' + ts1 + '"][TIME_SLOT_REF2="' + ts2 + '"]')['ANNOTATION_ID']
partSpeech = eaf.select_one('TIER[LINGUISTIC_TYPE_REF="part of speech"] REF_ANNOTATION[ANNOTATION_REF="' + id + '"] ANNOTATION_VALUE')
if partSpeech == None:
partSpeech = eaf.select_one('TIER[LINGUISTIC_TYPE_REF="part of speech"] REF_ANNOTATION[ANNOTATION_ID="' + id + '"] ANNOTATION_VALUE')
id = partSpeech.parent['ANNOTATION_REF']
if partSpeech.string == 'VERB':
token = eaf.find(ANNOTATION_ID=id).ANNOTATION_VALUE.string
table.append([file, token, morph.parse(token)[0].normal_form])
singleFileList = singleFileList + token + '\n'
if singleFileList != '':
with open('out_verbs_imp/' + file + '.txt', 'w', encoding='utf8') as wordList:
wordList.write(singleFileList)
except Exception:
logging.exception(file)
print('Error processing the ' + file + '.')
continue
with open('verbs_imp.csv', 'w', encoding='utf8', newline='') as outFile:
writer = csv.writer(outFile, delimiter=';')
writer.writerows(table)
print('Done!')