Skip to content

Commit

Permalink
Initialized children of ParseNode to empty list in the beginning.
Browse files Browse the repository at this point in the history
  • Loading branch information
Olcay Taner YILDIZ committed May 2, 2022
1 parent 08fc514 commit f7b8e6c
Showing 1 changed file with 58 additions and 62 deletions.
120 changes: 58 additions & 62 deletions Sources/ParseTree/ParseNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Dictionary

open class ParseNode : Equatable{

public var children: [ParseNode]? = nil
public var children: [ParseNode] = []
public var parent: ParseNode? = nil
public var data: Symbol? = nil

Expand Down Expand Up @@ -74,15 +74,14 @@ open class ParseNode : Equatable{
var parenthesisCount : Int = 0
var childLine : String = ""
self.parent = parent
children = []
if isLeaf {
data = Symbol(name: line)
} else {
data = Symbol(name: String(line[line.index(line.startIndex, offsetBy: 1)..<line.range(of: " ")!.lowerBound]))
if line.firstIndex(of: ")") == line.lastIndex(of: ")") {
let start = line.index(line.firstIndex(of: " ")!, offsetBy: 1)
let end = line.firstIndex(of: ")")!
children!.append(ParseNode(parent: self, line: String(line[start..<end]), isLeaf: true))
children.append(ParseNode(parent: self, line: String(line[start..<end]), isLeaf: true))
} else {
let emptyIndex = Int(line.distance(from: line.startIndex, to: line.firstIndex(of: " ")!))
for i in emptyIndex + 1..<line.count {
Expand All @@ -97,7 +96,7 @@ open class ParseNode : Equatable{
}
}
if parenthesisCount == 0 && childLine != "" {
children!.append(ParseNode(parent: self, line: childLine.trimmingCharacters(in: .whitespacesAndNewlines), isLeaf: false))
children.append(ParseNode(parent: self, line: childLine.trimmingCharacters(in: .whitespacesAndNewlines), isLeaf: false))
childLine = ""
}
}
Expand All @@ -114,10 +113,9 @@ open class ParseNode : Equatable{
- data: Data for this node.
*/
public init(left: ParseNode, right: ParseNode, data: Symbol){
children = []
children!.append(left)
children.append(left)
left.parent = self
children!.append(right)
children.append(right)
right.parent = self
self.data = data
}
Expand All @@ -130,8 +128,7 @@ open class ParseNode : Equatable{
- data: Data for this node.
*/
public init(left: ParseNode, data: Symbol){
children = []
children!.append(left)
children.append(left)
left.parent = self
self.data = data
}
Expand All @@ -142,7 +139,6 @@ open class ParseNode : Equatable{
- data: Data for this node.
*/
public init(data: Symbol){
children = []
self.data = data
}

Expand All @@ -160,7 +156,7 @@ open class ParseNode : Equatable{
switch direction{
case SearchDirectionType.LEFT:
for item in priorityList {
for child in children! {
for child in children {
if child.getData()!.trimSymbol().getName() == item {
return child
}
Expand All @@ -171,9 +167,9 @@ open class ParseNode : Equatable{
}
case SearchDirectionType.RIGHT:
for item in priorityList {
var j : Int = children!.count - 1
var j : Int = children.count - 1
while j >= 0 {
let child = children![j]
let child = children[j]
if child.getData()!.trimSymbol().getName() == item {
return child
}
Expand All @@ -194,7 +190,7 @@ open class ParseNode : Equatable{
- Returns: Head node of the descendant leaves of this current node.
*/
public func headLeaf() -> ParseNode?{
if children!.count > 0 {
if children.count > 0 {
let head = headChild()
if head != nil {
return head!.headLeaf()
Expand Down Expand Up @@ -303,15 +299,15 @@ open class ParseNode : Equatable{
- child Child node to be added.
*/
public func addChild(child: ParseNode){
children?.append(child)
children.append(child)
child.parent = self
}

/**
* Recursive method to restore the parents of all nodes below this node in the hierarchy.
*/
public func correctParents(){
for child in children!{
for child in children{
child.parent = self
child.correctParents()
}
Expand All @@ -324,7 +320,7 @@ open class ParseNode : Equatable{
- child: Child node to be added.
*/
public func addChild(index: Int, child: ParseNode){
children?.insert(child, at: index)
children.insert(child, at: index)
child.parent = self
}

Expand All @@ -335,7 +331,7 @@ open class ParseNode : Equatable{
- child: Child node to be replaced.
*/
public func setChild(index: Int, child: ParseNode){
children?[index] = child
children[index] = child
}

/**
Expand All @@ -344,9 +340,9 @@ open class ParseNode : Equatable{
- child: Child node to be deleted.
*/
public func removeChild(child: ParseNode){
for i in 0..<children!.count{
if children?[i] === child{
children?.remove(at: i)
for i in 0..<children.count{
if children[i] === child{
children.remove(at: i)
break
}
}
Expand All @@ -357,11 +353,11 @@ open class ParseNode : Equatable{
- Returns: Number of all leaf nodes in the current subtree.
*/
public func leafCount() -> Int{
if children!.count == 0{
if children.count == 0{
return 1
} else {
var sum : Int = 0
for child in children!{
for child in children{
sum += child.leafCount()
}
return sum
Expand All @@ -373,9 +369,9 @@ open class ParseNode : Equatable{
- Returns: Number of all nodes in the current subtree.
*/
public func nodeCount() -> Int{
if children!.count > 0{
if children.count > 0{
var sum : Int = 1
for child in children!{
for child in children{
sum += child.nodeCount()
}
return sum
Expand All @@ -390,9 +386,9 @@ open class ParseNode : Equatable{
- Returns: Number of all nodes, which have more than one children, in the current subtree.
*/
public func nodeCountWithMultipleChildren() -> Int{
if children!.count > 1{
if children.count > 1{
var sum : Int = 1
for child in children!{
for child in children{
sum += child.nodeCountWithMultipleChildren()
}
return sum
Expand All @@ -405,8 +401,8 @@ open class ParseNode : Equatable{
* Recursive method to remove all punctuation nodes from the current subtree.
*/
public func stripPunctuation(){
children!.removeAll() {Word.isPunctuationSymbol(surfaceForm: $0.getData()!.getName())}
for child in children!{
children.removeAll() {Word.isPunctuationSymbol(surfaceForm: $0.getData()!.getName())}
for child in children{
child.stripPunctuation()
}
}
Expand All @@ -416,7 +412,7 @@ open class ParseNode : Equatable{
- Returns: Number of children of this node.
*/
public func numberOfChildren() -> Int{
return children!.count
return children.count
}

/**
Expand All @@ -426,23 +422,23 @@ open class ParseNode : Equatable{
- Returns: i'th child of this node.
*/
public func getChild(i: Int) -> ParseNode{
return children![i]
return children[i]
}

/**
* Returns the first child of this node.
- Returns: First child of this node.
*/
public func firstChild() -> ParseNode{
return children!.first!
return children.first!
}

/**
* Returns the last child of this node.
- Returns: Last child of this node.
*/
public func lastChild() -> ParseNode{
return children!.last!
return children.last!
}

/**
Expand All @@ -463,7 +459,7 @@ open class ParseNode : Equatable{
*/
public func getChildIndex(child: ParseNode) -> Int{
var index: Int = 0
for child1 in children!{
for child1 in children{
if child1 === child{
return index
}
Expand All @@ -479,7 +475,7 @@ open class ParseNode : Equatable{
- Returns: True if the given node is descendant of this node.
*/
public func isDescendant(node: ParseNode) -> Bool{
for child in children!{
for child in children{
if child === node{
return true
} else {
Expand All @@ -497,9 +493,9 @@ open class ParseNode : Equatable{
* node.
*/
public func previousSibling() -> ParseNode?{
for i in 1..<parent!.children!.count{
if parent?.children![i] === self{
return parent?.children![i - 1]
for i in 1..<parent!.children.count{
if parent?.children[i] === self{
return parent?.children[i - 1]
}
}
return nil
Expand All @@ -511,9 +507,9 @@ open class ParseNode : Equatable{
* node.
*/
public func nextSibling() -> ParseNode?{
for i in 0..<parent!.children!.count - 1{
if parent?.children![i] === self{
return parent?.children![i + 1]
for i in 0..<parent!.children.count - 1{
if parent?.children[i] === self{
return parent?.children[i + 1]
}
}
return nil
Expand Down Expand Up @@ -552,7 +548,7 @@ open class ParseNode : Equatable{
*/
public func wordCount(excludeStopWords: Bool) -> Int{
var sum = 0
if children?.count == 0{
if children.count == 0{
if !excludeStopWords{
sum = 1
} else {
Expand Down Expand Up @@ -580,7 +576,7 @@ open class ParseNode : Equatable{
} else {
sum = 0
}
for aChild in children!{
for aChild in children{
sum = sum + aChild.wordCount(excludeStopWords: excludeStopWords)
}
return sum
Expand All @@ -593,11 +589,11 @@ open class ParseNode : Equatable{
- list: Returned span list.
*/
public func constituentSpanList(startIndex: Int, list: inout [ConstituentSpan]){
if children!.count > 0 {
if children.count > 0 {
list.append(ConstituentSpan(constituent: data!, start: startIndex, end: startIndex + leafCount()))
}
var total : Int = 0
for parseNode in children! {
for parseNode in children {
parseNode.constituentSpanList(startIndex: startIndex + total, list: &list)
total += parseNode.leafCount()
}
Expand All @@ -607,7 +603,7 @@ open class ParseNode : Equatable{
- Returns: true if this node is leaf, false otherwise.
*/
public func isLeaf() -> Bool{
return children!.count == 0
return children.count == 0
}

/**
Expand All @@ -623,7 +619,7 @@ open class ParseNode : Equatable{
- Returns: A sentence which contains all words in the subtree rooted at this node.
*/
public func toSentence() -> String{
if children!.count == 0 {
if children.count == 0 {
if getData() != nil && !isDummyNode() {
return " " + getData()!.getName().replacingOccurrences(of: "-LRB-", with: "(").replacingOccurrences(of: "-RRB-", with: ")").replacingOccurrences(of: "-LSB-", with: "[").replacingOccurrences(of: "-RSB-", with: "]").replacingOccurrences(of: "-LCB-", with: "{").replacingOccurrences(of: "-RCB-", with: "}").replacingOccurrences(of: "-lrb-", with: "(").replacingOccurrences(of: "-rrb-", with: ")").replacingOccurrences(of: "-lsb-", with: "[").replacingOccurrences(of: "-rsb-", with: "]").replacingOccurrences(of: "-lcb-", with: "{").replacingOccurrences(of: "-rcb-", with: "}")
} else {
Expand All @@ -635,7 +631,7 @@ open class ParseNode : Equatable{
}
} else {
var st : String = ""
for aChild in children! {
for aChild in children {
st = st + aChild.toSentence()
}
return st
Expand All @@ -647,15 +643,15 @@ open class ParseNode : Equatable{
- Returns: A string which contains all words in the subtree rooted at this node.
*/
open func description() -> String{
if children!.count < 2 {
if children!.count < 1 {
if children.count < 2 {
if children.count < 1 {
return getData()!.getName()
} else {
return "(" + data!.getName() + " " + firstChild().description() + ")"
}
} else {
var st : String = "(" + data!.getName()
for aChild in children! {
for aChild in children {
st = st + " " + aChild.description()
}
return st + ") "
Expand All @@ -681,17 +677,17 @@ open class ParseNode : Equatable{
- node: Node to be swapped.
*/
public func moveLeft(node: ParseNode){
for i in 0..<children!.count {
if children?[i] === node {
for i in 0..<children.count {
if children[i] === node {
if i == 0 {
children!.swapAt(0, children!.count - 1)
children.swapAt(0, children.count - 1)
} else {
children!.swapAt(i, (i - 1) % children!.count)
children.swapAt(i, (i - 1) % children.count)
}
return
}
}
for aChild in children! {
for aChild in children {
aChild.moveLeft(node: node)
}
}
Expand All @@ -703,17 +699,17 @@ open class ParseNode : Equatable{
- node: Node to be swapped.
*/
public func moveRight(node: ParseNode){
for i in 0..<children!.count {
if children?[i] === node {
if i == children!.count - 1{
children!.swapAt(0, children!.count - 1)
for i in 0..<children.count {
if children[i] === node {
if i == children.count - 1{
children.swapAt(0, children.count - 1)
} else {
children!.swapAt(i, (i + 1) % children!.count)
children.swapAt(i, (i + 1) % children.count)
}
return
}
}
for aChild in children! {
for aChild in children {
aChild.moveRight(node: node)
}
}
Expand Down

0 comments on commit f7b8e6c

Please sign in to comment.