Strumenti Utente

Strumenti Sito


ud_treebank

Differenze

Queste sono le differenze tra la revisione selezionata e la versione attuale della pagina.

Link a questa pagina di confronto

Entrambe le parti precedenti la revisione Revisione precedente
Prossima revisione
Revisione precedente
ud_treebank [2021/10/25 13:58]
65.21.180.48 versione precedente ripristinata (2021/06/03 11:26)
ud_treebank [2026/03/31 12:08] (versione attuale)
admin
Linea 1: Linea 1:
-====== Struttura di un treebank in “Universal Dependencies” ======+====== Struttura di un treebank “Universal Dependencies” ======
 ===== Scaricare il treebank di copto ===== ===== Scaricare il treebank di copto =====
 Copiate nella cartella "testi" la cartella "UD_Coptic-Scriptorium-master" scaricabile in formato compresso da questo [[https://github.com/UniversalDependencies/UD_Coptic-Scriptorium/archive/refs/heads/master.zip|link]] (si veda il file LICENSE.txt per riferimenti e condizioni di utilizzo). Copiate nella cartella "testi" la cartella "UD_Coptic-Scriptorium-master" scaricabile in formato compresso da questo [[https://github.com/UniversalDependencies/UD_Coptic-Scriptorium/archive/refs/heads/master.zip|link]] (si veda il file LICENSE.txt per riferimenti e condizioni di utilizzo).
 ===== Struttura dei record ===== ===== Struttura dei record =====
-Nel formato “Universal Dependencies” i record relativi alle singole parole unità corrispondono a singoli righi di testo con campi separati da TAB (caratteri di tabulazione), esemplificato dai record seguenti (tratti in particolare dal corpus di copto menzionato sopra):+Nel formato “Universal Dependencies” ([[https://universaldependencies.org]]) i record relativi alle singole parole unità corrispondono a singoli righi di testo con campi separati da TAB (caratteri di tabulazione), esemplificato dai record seguenti (tratti in particolare dal corpus di copto menzionato sopra):
 <code> <code>
 15 ⲕ ⲛⲧⲟⲕ PRON PPERS Definite=Def|Gender=Masc|Number=Sing|Person=2|PronType=Prs 16 nsubj _ _ 15 ⲕ ⲛⲧⲟⲕ PRON PPERS Definite=Def|Gender=Masc|Number=Sing|Person=2|PronType=Prs 16 nsubj _ _
Linea 51: Linea 51:
 # sent_id = shenoute_fox-XH204-216_s0006 # sent_id = shenoute_fox-XH204-216_s0006
 </code> </code>
----- 
-A differenza di quanto accade in altri sistemi di notazione, il sistema UD non include nel record della singola parola unità l'identificativo della frase che la contiene. L’aggiunta di questo campo può essere tuttavia utile per l’esplorazione del corpus (cf. lo script seguente). 
  
-<code python ud_explorer.py> +Esempio di frase completa: 
-# -*- coding: utf-8 -*- +<code> 
-#Copyright 2021 P. Milizia CC-BY 4.0 (https://creativecommons.org/licenses/by/4.0/) +# sent_id = shenoute_a22-a22_YA421-428_s0021 
-import re +# text_en = ... 
-import glob +# text = ⲁⲩⲱ ⲉⲩⲧⲛⲧⲱⲛ ⲉⲡⲟⲩⲟⲉⲓⲛ ⲙⲡⲉϩⲟⲟⲩ · 
- +1 ⲁⲩⲱ ⲁⲩⲱ CCONJ CONJ _ 4 cc _ _ 
- +2-4 ⲉⲩⲧⲛⲧⲱⲛ _ _ _ _ _ _ _ _ 
-#CLASSE PYTHON PER LA LETTURA DI UN TREEBANK UD +2 ⲉ ⲉⲣⲉ PART CFOC _ 4 mark _ _ 
-class UD: +3 ⲩ ⲛⲧⲟⲟⲩ PRON PPERS Definite=Def|Number=Plur|Person=3|PronType=Prs 4 nsubj _ _ 
- def __init__(self, data): +4 ⲧⲛⲧⲱⲛ ⲧⲟⲛⲧⲛ VERB VSTAT Mood=Ind|VerbForm=Fin 0 root _ Orig=ⲧ︤ⲛ︥ⲧⲱⲛ` 
- self.data=self.add_sent_id(data) +5-7 ⲉⲡⲟⲩⲟⲉⲓⲛ _ _ _ _ _ _ _ _ 
- self.tokens=self.create_tokens(self.data) +5 ⲉ ⲉ ADP PREP _ 7 case _ _ 
- self.sentences=self.create_sentences(self.tokens) +6 ⲡ ⲡ DET ART Definite=Def|Gender=Masc|Number=Sing|PronType=Art 7 det _ Entity=(abstract 
-  +7 ⲟⲩⲟⲉⲓⲛ ⲟⲩⲟⲉⲓⲛ NOUN N _ 4 obl _ _ 
- #aggiunge il campo sent_id +8-10 ⲙⲡⲉϩⲟⲟⲩ _ _ _ _ _ _ _ _ 
- def add_sent_id(self, text): +8 ⲙ ⲛ ADP PREP _ 10 case _ Orig=ⲙ̄ 
- sid_expr=re.compile('(?<=sent_id\s=\s).*') +9 ⲡⲉ ⲡ DET ART Definite=Def|Gender=Masc|Number=Sing|PronType=Art 10 det _ Entity=(time 
- text=re.split('\n',text) +10 ϩⲟⲟⲩ ϩⲟⲟⲩ NOUN N _ 7 nmod _ Entity=time)abstract)|Orig=ϩⲟⲟⲩ` 
- output='' +11 · · PUNCT PUNCT _ 4 punct _ _
- sid='' +
- for line in text: +
- found=re.search(sid_expr,line) +
- if found != None: +
- sid=found.group() +
- if line !='' and line[0] != '#': +
- line = line+'\t'+sid +
- output += line+'\n' +
- return output +
-  +
- #crea la lista dei tokens +
- def create_tokens(self, data): +
- expr=re.compile('\n([^#].*?)\t(.*?)\t(.*?)\t(.*?)\t(.*?)\t(.*?)\t(.*?)\t(.*?)\t(.*?)\t(.*?)\t(.*)') +
- tokens = re.findall(expr, data) +
- for i in range(len(tokens)): +
- tokens[i]=list(tokens[i]) +
- for ii in (5,9): +
- tokens[i][ii]=re.split('\|',tokens[i][ii]) +
- try: +
- tokens[i][0]=int(tokens[i][0]) +
- except ValueError: +
- pass +
- try: +
- tokens[i][6]=int(tokens[i][6]) +
- except ValueError: +
- pass +
- return tokens +
- +
- #crea il dizionario delle frasi +
- def create_sentences(self, tokens): +
- sentences={} +
- sid='' +
- for t in tokens: +
- if t[10] != sid: +
- sid=t[10] +
- sentences[sid]=['root'] +
- if type(t[0])==int: +
- tt=t[:] +
- sentences[sid].append(tt) +
- return sentences +
- +
- def parent_of(self, occurrence): +
- x=self.sentences[occurrence[10]][occurrence[0]] +
- return self.sentences[x[10]][x[6]] +
- +
- def grandparent_of(self, occurrence): +
- return self.parent_of(self.parent_of(occurrence)) +
- +
- def subsequent_of(self, occurrence): +
- try: +
- outp=self.sentences[occurrence[10]][occurrence[0]+1] +
- except IndexError: +
- outp=None +
- return outp +
- +
- def precedent_of(self, occurrence): +
- try: +
- outp=self.sentences[occurrence[10]][occurrence[0]-1] +
- except IndexError: +
- outp=None +
- except TypeError: +
- outp=None +
- return outp +
-  +
- def children_of(self,occurrence): +
- x=self.sentences[occurrence[10]][occurrence[0]] +
- return [t for t in self.sentences[occurrence[10]][1:] if self.parent_of(t)==x] +
- +
- def ancestors_of(self, occurrence): +
- x=self.sentences[occurrence[10]][occurrence[0]] +
- output = [x] +
- while x != 'root': +
- y=self.parent_of(x) +
- output.append(y) +
- x=y +
- return output +
- +
- def get_sentence(self, x): +
- return [j[1] for j in self.sentences[x][1:]] +
- +
- +
-#PREPARAZIONE DEI DATI PER LA CLASSE +
- +
-#lettura file +
-directory_name='testi/UD_Coptic-Scriptorium-master' +
-path = directory_name+'/*conllu' +
-files=glob.glob(path) +
-data='' +
-for filepath in files: +
- with open(filepath, 'r') as file: +
- file_data=file.read() +
- data+=file_data +
-#ordina i file e crea una variabile text_list con l'elenco dei testi +
-data=data.split('# newdoc ')[1:] +
-data.sort() +
-data='# newdoc '+'# newdoc '.join(data) +
-text_list=re.findall('#\snewdoc\s.*', data) +
- +
- +
-#ESEMPIO DI UTILIZZO +
- +
-#trova tutte le occorrenze di 'ⲙⲙⲟ' e di 'ⲛ' come marca dell'oggetto diretto +
-#alle seguenti condizioni: +
-#1) che la frase sia al passato perfettivo positivo +
-#2) che almeno un elemento sia interposto tra verbo e preposizione +
- +
-coptic=UD(data) +
- +
-found_tokens=[] +
- +
-def is_past(o): +
- answer = False +
- for child in coptic.children_of(o): +
- if child[1]=='ⲁ' and child[3]=='AUX': +
- answer = True +
- break +
- return answer +
- +
-for token in coptic.tokens: +
- if (token[1] == 'ⲙⲙⲟ' or token[1] == 'ⲛ') and token[7]=='case': +
- try: +
- regens = coptic.grandparent_of(token) +
- noun_pronoun = coptic.parent_of(token) +
- if noun_pronoun[7]=='obj' and is_past(regens) and (int(token[0]) - int(regens[0]))>1: +
- found_tokens.append(token) +
- except: +
- pass+
  
-for o in found_tokens: 
- print(o) 
 </code> </code>
ud_treebank.1635170312.txt.gz · Ultima modifica: 2021/10/25 13:58 da 65.21.180.48