Cambridge SMT System
RulePattern.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 uk.ac.cam.eng.extraction.Rule;
23 
30 public class RulePattern {
31 
32  private SidePattern sourcePattern;
33  private SidePattern targetPattern;
34 
35  private RulePattern(SidePattern sourcePattern, SidePattern targetPattern) {
36  this.sourcePattern = sourcePattern;
37  this.targetPattern = targetPattern;
38  }
39 
40  public static RulePattern parsePattern(String patternString) {
41  // X_W-W_X
42  String[] sourceTarget = patternString.split("-");
43  if (sourceTarget.length != 2) {
44  System.err.println("Malformed pattern: " + patternString);
45  System.exit(1);
46  }
47  return new RulePattern(SidePattern.parsePattern(sourceTarget[0]),
48  SidePattern.parsePattern(sourceTarget[1]));
49  }
50 
51 
52  public static RulePattern getPattern(Rule rule) {
53  return new RulePattern(rule.source().toPattern(),
54  rule.target().toPattern());
55  }
56 
57  public boolean isSwappingNT() {
58  if (!sourcePattern.hasMoreThan1NT()) {
59  return false;
60  }
61  return (sourcePattern.getFirstNT() != targetPattern.getFirstNT());
62  }
63 
64  /*
65  * (non-Javadoc)
66  *
67  * @see java.lang.Object#hashCode()
68  */
69  @Override
70  public int hashCode() {
71  final int prime = 31;
72  int result = 1;
73  result = prime * result
74  + ((sourcePattern == null) ? 0 : sourcePattern.hashCode());
75  result = prime * result
76  + ((targetPattern == null) ? 0 : targetPattern.hashCode());
77  return result;
78  }
79 
80  /*
81  * (non-Javadoc)
82  *
83  * @see java.lang.Object#equals(java.lang.Object)
84  */
85  @Override
86  public boolean equals(Object obj) {
87  if (this == obj)
88  return true;
89  if (obj == null)
90  return false;
91  if (getClass() != obj.getClass())
92  return false;
93  RulePattern other = (RulePattern) obj;
94  if (sourcePattern == null) {
95  if (other.sourcePattern != null)
96  return false;
97  } else if (!sourcePattern.equals(other.sourcePattern))
98  return false;
99  if (targetPattern == null) {
100  if (other.targetPattern != null)
101  return false;
102  } else if (!targetPattern.equals(other.targetPattern))
103  return false;
104  return true;
105  }
106 
107  /*
108  * (non-Javadoc)
109  *
110  * @see java.lang.Object#toString()
111  */
112  @Override
113  public String toString() {
114  return sourcePattern.toString() + " " + targetPattern.toString();
115  }
116 }
static RulePattern getPattern(Rule rule)
static RulePattern parsePattern(String patternString)
static SidePattern parsePattern(String patternString)