|
| 1 | +package edu.stanford.nlp.parser.lexparser; |
| 2 | + |
| 3 | +import java.io.Serializable; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | + |
| 8 | +import edu.stanford.nlp.ling.HasWord; |
| 9 | +import edu.stanford.nlp.ling.Word; |
| 10 | +import edu.stanford.nlp.trees.DiskTreebank; |
| 11 | +import edu.stanford.nlp.trees.HeadFinder; |
| 12 | +import edu.stanford.nlp.trees.LeftHeadFinder; |
| 13 | +import edu.stanford.nlp.trees.MemoryTreebank; |
| 14 | +import edu.stanford.nlp.trees.PennTreeReaderFactory; |
| 15 | +import edu.stanford.nlp.trees.Tree; |
| 16 | +import edu.stanford.nlp.trees.TreeNormalizer; |
| 17 | +import edu.stanford.nlp.trees.TreeReaderFactory; |
| 18 | +import edu.stanford.nlp.trees.TreeTransformer; |
| 19 | +import edu.stanford.nlp.trees.international.italian.ItalianTreebankLanguagePack; |
| 20 | +import edu.stanford.nlp.util.StringUtils; |
| 21 | +import edu.stanford.nlp.util.logging.Redwood; |
| 22 | + |
| 23 | +/** |
| 24 | + * Bare-bones implementation of a ParserParams for the Italian Turin treebank. |
| 25 | + * <br> |
| 26 | + * Suitable for use in the SR Parser. Will need additional work to function in the PCFG. |
| 27 | + * Also, would likely function better with a new headfinder. |
| 28 | + */ |
| 29 | +public class ItalianTreebankParserParams extends AbstractTreebankParserParams { |
| 30 | + /** A logger for this class */ |
| 31 | + private static final Redwood.RedwoodChannels log = Redwood.channels(ItalianTreebankParserParams.class); |
| 32 | + |
| 33 | + public ItalianTreebankParserParams() { |
| 34 | + super(new ItalianTreebankLanguagePack()); |
| 35 | + // TODO: make a Italian specific HeadFinder or build one that can be learned |
| 36 | + headFinder = new LeftHeadFinder(); |
| 37 | + } |
| 38 | + |
| 39 | + private HeadFinder headFinder; |
| 40 | + |
| 41 | + private TreeNormalizer normalizer = null; |
| 42 | + |
| 43 | + static final String[] EMPTY_SISTERS = new String[0]; |
| 44 | + |
| 45 | + @Override |
| 46 | + public HeadFinder headFinder() { |
| 47 | + return headFinder; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public HeadFinder typedDependencyHeadFinder() { |
| 52 | + return headFinder; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public TreeTransformer collinizer() { |
| 57 | + return new TreeCollinizer(tlp, true, false, 0); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public TreeTransformer collinizerEvalb() { |
| 62 | + return collinizer(); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public String[] sisterSplitters() { |
| 67 | + // TODO: the SR Parser does not use this code path, so it is not implemented |
| 68 | + return EMPTY_SISTERS; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public Tree transformTree(Tree t, Tree root) { |
| 73 | + // TODO: the SR Parser does not use this code path, so it is not implemented |
| 74 | + return t; |
| 75 | + } |
| 76 | + |
| 77 | + public static class ItalianSubcategoryStripper extends TreeNormalizer { |
| 78 | + @Override |
| 79 | + public String normalizeNonterminal(String category) { |
| 80 | + // The stanza script leaves the fancy endings on the tags |
| 81 | + // but simplifies the constiituency tags |
| 82 | + List<String> pieces = StringUtils.split(category, "~"); |
| 83 | + |
| 84 | + return pieces.get(0); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + TreeNormalizer buildNormalizer() { |
| 89 | + return new ItalianSubcategoryStripper(); |
| 90 | + } |
| 91 | + |
| 92 | + /** {@inheritDoc} */ |
| 93 | + @Override |
| 94 | + public TreeReaderFactory treeReaderFactory() { |
| 95 | + if (normalizer == null) { |
| 96 | + normalizer = buildNormalizer(); |
| 97 | + } |
| 98 | + return new PennTreeReaderFactory(normalizer); |
| 99 | + } |
| 100 | + |
| 101 | + |
| 102 | + @Override |
| 103 | + public void display() { |
| 104 | + String params = "Using ItalianTreebankParserParams"; |
| 105 | + log.info(params); |
| 106 | + } |
| 107 | + |
| 108 | + /** {@inheritDoc} */ |
| 109 | + @Override |
| 110 | + public List<? extends HasWord> defaultTestSentence() { |
| 111 | + List<Word> ret = new ArrayList<>(); |
| 112 | + String[] sent = {"Questo", "è", "un", "test", "."}; |
| 113 | + for (String str : sent) { |
| 114 | + ret.add(new Word(str)); |
| 115 | + } |
| 116 | + return ret; |
| 117 | + } |
| 118 | + |
| 119 | + private static final long serialVersionUID = 9824524678L; |
| 120 | +} |
0 commit comments