Difference between revisions of "Ltarlin1/Final Project"
(→Usage) |
(→Usage) |
||
| Line 129: | Line 129: | ||
Clone the github repository found [https://github.swarthmore.edu/ltarlin1/ling073-ltarlin1-autoglosser here]. The <code>autoglosser.py</code> file can be run with the following format: <code>python autoglosser.py [input file] [type] [POS tags] <dependencies to include - OPTIONAL></code>. Each of these arguments is explained in more detail below: | Clone the github repository found [https://github.swarthmore.edu/ltarlin1/ling073-ltarlin1-autoglosser here]. The <code>autoglosser.py</code> file can be run with the following format: <code>python autoglosser.py [input file] [type] [POS tags] <dependencies to include - OPTIONAL></code>. Each of these arguments is explained in more detail below: | ||
* '''input file''': This argument specifies the CG file to be diagrammed. The code expects the name of the file to follow the form "<label>_input.txt", but this parameter only requires the label. For example, if the desired input file were "fox_input.txt", this argument would simply be "fox". The exception to this rule is if the user wants to do batch processing, in which case this argument is simply the name of the file containing the other filenames. | * '''input file''': This argument specifies the CG file to be diagrammed. The code expects the name of the file to follow the form "<label>_input.txt", but this parameter only requires the label. For example, if the desired input file were "fox_input.txt", this argument would simply be "fox". The exception to this rule is if the user wants to do batch processing, in which case this argument is simply the name of the file containing the other filenames. | ||
| − | * '''type''': This argument specifies whether the input file is a single sentence or a list of filenames. The options are | + | * '''type''': This argument specifies whether the input file is a single sentence or a list of filenames. The options are m (for multiple, or batch processing) or s (for a single sentence). |
* '''POS tags''': This argument specifies whether the part-of-speech tags should be displayed below the text of the sentence. The options are 1 (for yes) or 0 (for no). | * '''POS tags''': This argument specifies whether the part-of-speech tags should be displayed below the text of the sentence. The options are 1 (for yes) or 0 (for no). | ||
* '''[OPTIONAL] Dependencies''' : This optional argument (or arguments) are the specific dependency types that should be displayed in the graph. If more than one is desired, simply list them here separated by spaces. For example, if you wanted to only see dependencies of type "det" and "amod", you would add "det amod" after the POS tag argument. | * '''[OPTIONAL] Dependencies''' : This optional argument (or arguments) are the specific dependency types that should be displayed in the graph. If more than one is desired, simply list them here separated by spaces. For example, if you wanted to only see dependencies of type "det" and "amod", you would add "det amod" after the POS tag argument. | ||
Revision as of 06:47, 10 May 2017
Contents
Code
The Problem
For my final project, I decided to create an autoglosser that would automatically convert the universal dependency files we worked with in class into visual form in Latex. There is a Latex package called tikz-dependency that allows for quick input of sentences and its corresponding dependencies, but it still requires the user to manually select each dependency. Therefore, my code automatically generates Latex code that fits the expected format for the tikz-dependency package and therefore can easily be displayed in a graph, as in the following example image:
Although this graph is fancy and colorful, the output of my code is a simplified version that includes all of the most relevant information and has some customizable features. A description of the code, the output, and how to run the program follows.
Previous Work
As discussed in the above section, the tikz-dependency package uses the tikz interface to facilitate the graph-making process. The syntax of a dependency in this format is strikingly similar to that which we saw in class (i.e. #4->6) because both refer to the "from word" and the "to word" using offset from the beginning of the sentence, so it was relatively simple to translate from one form to another.
Who Will Benefit
This tool would be useful for linguists who want to speed up the visual-making process when working with universal dependencies. It does not have much of an application outside the technical linguistics domain because generating the input files to my program requires prior knowledge of universal dependencies.
Basic Solution
My autoglosser parses each line of the CG file and separates it into the relevant portions--the words that make up the sentence, each corresponding part of speech, the other side of the dependency, and the type of dependence relationship. The more specific part-of-speech information present in the CG file is ignored because it would clutter the sentence diagram if it were included.
Therefore, a CG file like the one we looked at in class
"<The>" "The" det def sp @det #1->4 "<quick>" "quick" adj sint @amod #2->4 "<brown>" "brown" adj @amod #3->4 "<fox>" "fox" n sg @nsubj #4->5 "<jumps>" "jump" vblex pres p3 sg @root #5->0 "<over>" "over" pr @case #6->9 "<the>" "the" det def sp @det #7->9 "<lazy>" "lazy" adj sint @amod #8->9 "<dog>" "dog" n sg @obl #9->5 "<in>" "in" pr @case #10->13 "<the>" "the" det def sp @det #11->13 "<green>" "green" adj sint @amod #12->13 "<fields>" "field" n pl @obl #13->5 "<.>" "." sent
is turned into something like this:
words = ['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog', 'in', 'the', 'green', 'fields', '.'] info = [ ['"The"', 'det', 'def', 'sp', '@det', '#1->4'], ['"quick"', 'adj', 'sint', '@amod', '#2->4'], ['"brown"', 'adj', '@amod', '#3->4'], ['"fox"', 'n', 'sg', '@nsubj', '#4->5'], ['"jump"', 'vblex', 'pres', 'p3', 'sg', '@root', '#5->0'], ['"over"', 'pr', '@case', '#6->9'], ['"the"', 'det', 'def', 'sp', '@det', '#7->9'], ['"lazy"', 'adj', 'sint', '@amod', '#8->9'], ['"dog"', 'n', 'sg', '@obl', '#9->5'], ['"in"', 'pr', '@case', '#10->13'], ['"the"', 'det', 'def', 'sp', '@det', '#11->13'], ['"green"', 'adj', 'sint', '@amod', '#12->13'], ['"field"', 'n', 'pl', '@obl', '#13->5'], ['"."', 'sent']]
The next step that my program takes is using string formatting to extract the relevant aspects of this "info"--namely the "from word", the "to word", and the type of dependency (indicated by the '@' symbol). Once I have all of this information, I can simply plug it into the corresponding locations in the desired format. Therefore, the final Latex code outputted by the simplest version of my program is
\begin{dependency}
\begin{deptext}[column sep=0.2cm]
The \& quick \& brown \& fox \& jumps \& over \& the \& lazy \& dog \& in \& the \& green \& fields \& ROOT \\
\end{deptext}
\depedge{4}{1}{det}
\depedge{4}{2}{amod}
\depedge{4}{3}{amod}
\depedge{5}{4}{nsubj}
\depedge{14}{5}{root}
\depedge{9}{6}{case}
\depedge{9}{7}{det}
\depedge{9}{8}{amod}
\depedge{5}{9}{obl}
\depedge{13}{10}{case}
\depedge{13}{11}{det}
\depedge{13}{12}{amod}
\depedge{5}{13}{obl}
\end{dependency}
Once you add the line \usepackage{tikz-dependency} to a blank Latex file, you can add this code and get the resulting image:
More Complicated Solution
I also added several customizable features to the program. I will briefly explain how each works here.
Visible POS Tags
I added the capability to display the part of speech tags below the text of the sentence. However, since this option may not always be desired, one of the command line arguments for the program toggles it on or off. This is the same graph as above, except with the POS tags visible:
Dependency Filter
In some situations, it may be useful to only view dependencies of a certain type. My tool allows you to optionally specify specific tag names that you want to appear in the graph. If no individual dependency types are listed, it will default to showing all of them. This is the same sentence as the previous examples, but this time with only the 'amod' dependencies displayed:
Batch Diagramming
If this dependency visualization is done on a large scale, it would be tedious to run the program separately for each individual sentence in the corpus. To speed things up, I implemented an option to input a .txt file with a list of filenames. The autoglosser will take that list and generate a separate output file (containing Latex code) for each filename in the list. This makes large scale diagramming more efficient and only requires the user to construct one large file.
Hard-coded Parameters
There were several design choices that I made because I thought they looked the best, but these can be changed with simple edits to the autoglosser.py file.
Column/Row Spacing
I added a default 0.2cm gap between columns (words) and a default 0.2ex gap between rows (only relevant when POS tags are visible).
Word Spacing
When there was a dependency between two consecutive words, the label was sometimes wider than the connecting arrow. To clean up the visual, I added an extra 0.5cm between columns whenever this occurred.
Filename Format
In my code, I hard-coded the program to take input files in the form "<label>_input.txt" and to create output files in the form "<label>_output.txt".
Usage
Clone the github repository found here. The autoglosser.py file can be run with the following format: python autoglosser.py [input file] [type] [POS tags] <dependencies to include - OPTIONAL>. Each of these arguments is explained in more detail below:
- input file: This argument specifies the CG file to be diagrammed. The code expects the name of the file to follow the form "<label>_input.txt", but this parameter only requires the label. For example, if the desired input file were "fox_input.txt", this argument would simply be "fox". The exception to this rule is if the user wants to do batch processing, in which case this argument is simply the name of the file containing the other filenames.
- type: This argument specifies whether the input file is a single sentence or a list of filenames. The options are m (for multiple, or batch processing) or s (for a single sentence).
- POS tags: This argument specifies whether the part-of-speech tags should be displayed below the text of the sentence. The options are 1 (for yes) or 0 (for no).
- [OPTIONAL] Dependencies : This optional argument (or arguments) are the specific dependency types that should be displayed in the graph. If more than one is desired, simply list them here separated by spaces. For example, if you wanted to only see dependencies of type "det" and "amod", you would add "det amod" after the POS tag argument.
Evaluation
I evaluated the quality of the autoglosser by the extent to which it accomplished the things I set out to accomplish. On a most basic level, the project goal was to provide an automatic way to visualize the syntactic dependencies of a sentence in Latex. Not only did my code do this, it provided additional customization to selectively view certain portions of the diagram. It also allowed for automatic batch processing to expedite the visualization process for a large corpus. Although there are still ways that the functionality of the autoglosser could be expanded and the aesthetics improved, the results that I got are what I hoped to produce for this final project.



