Cambridge SMT System
SidePattern.java
Go to the documentation of this file.
1 /*******************************************************************************
2  * Licensed under the Apache License, Version 2.0 (the "License");
3  * you may not use these files except in compliance with the License.
4  * You may obtain a copy of the License at
5  *
6  * http://www.apache.org/licenses/LICENSE-2.0
7  *
8  * Unless required by applicable law or agreed to in writing, software
9  * distributed under the License is distributed on an "AS IS" BASIS,
10  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11  * See the License for the specific language governing permissions and
12  * limitations under the License.
13  *
14  * Copyright 2014 - Juan Pino, Aurelien Waite, William Byrne
15  *******************************************************************************/
20 package uk.ac.cam.eng.rule.retrieval;
21 
22 import java.util.ArrayList;
23 import java.util.List;
24 
31 public class SidePattern {
32 
33  private List<String> pattern;
34  private int numberOfNT;
35 
36  public SidePattern(List<String> pattern) {
37  this.pattern = pattern;
38  numberOfNT = 0;
39  for (String elt : pattern) {
40  if (!elt.equals("w")) {
41  numberOfNT++;
42  }
43  }
44  }
45 
46  int size() {
47  return pattern.size();
48  }
49 
50  String get(int index) {
51  return pattern.get(index);
52  }
53 
54  public static SidePattern parsePattern(String patternString) {
55  String[] parts = patternString.split("_");
56  List<String> elements = new ArrayList<String>();
57  for (String part : parts) {
58  if (part.equals("V")) {
59  elements.add("-1");
60  } else if (part.equals("V1")) {
61  elements.add("-2");
62  } else if (part.equals("W")) {
63  elements.add("w");
64  } else {
65  throw new RuntimeException("Malformed pattern: " + patternString);
66  }
67  }
68  return new SidePattern(elements);
69  }
70 
71 
72  public boolean isPhrase() {
73  return (pattern.size() == 1 && pattern.get(0).equals("w"));
74  }
75 
76  public boolean hasMoreThan1NT() {
77  return (numberOfNT > 1);
78  }
79 
80  public int getFirstNT() {
81  for (String elt : pattern) {
82  if (!elt.equals("w")) {
83  return Integer.parseInt(elt);
84  }
85  }
86  return 0;
87  }
88 
89  /*
90  * (non-Javadoc)
91  *
92  * @see java.lang.Object#hashCode()
93  */
94  @Override
95  public int hashCode() {
96  final int prime = 31;
97  int result = 1;
98  result = prime * result + ((pattern == null) ? 0 : pattern.hashCode());
99  return result;
100  }
101 
102  /*
103  * (non-Javadoc)
104  *
105  * @see java.lang.Object#equals(java.lang.Object)
106  */
107  @Override
108  public boolean equals(Object obj) {
109  if (this == obj)
110  return true;
111  if (obj == null)
112  return false;
113  if (getClass() != obj.getClass())
114  return false;
115  SidePattern other = (SidePattern) obj;
116  if (pattern == null) {
117  if (other.pattern != null)
118  return false;
119  } else if (!pattern.equals(other.pattern))
120  return false;
121  return true;
122  }
123 
124  /*
125  * (non-Javadoc)
126  *
127  * @see java.lang.Object#toString()
128  */
129  @Override
130  public String toString() {
131  StringBuilder res = new StringBuilder();
132  if (!pattern.isEmpty()) {
133  res.append(pattern.get(0));
134  }
135  for (int i = 1; i < pattern.size(); i++) {
136  res.append("_").append(pattern.get(i));
137  }
138  return res.toString();
139  }
140 }
static SidePattern parsePattern(String patternString)