Skip to content

Commit

Permalink
Added ParallelTreeBank and moved getName and setName methods from Par…
Browse files Browse the repository at this point in the history
…seTreeDrawable.
  • Loading branch information
Olcay Taner YILDIZ committed May 17, 2022
1 parent f7b8e6c commit ed8547c
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 1 deletion.
68 changes: 68 additions & 0 deletions Sources/ParseTree/ParallelTreeBank.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//
// File.swift
//
//
// Created by Olcay Taner YILDIZ on 17.05.2022.
//

import Foundation

open class ParallelTreeBank{

public var fromTreeBank, toTreeBank : TreeBank

init(folder1: String, folder2: String){
fromTreeBank = TreeBank(folder: folder1)
toTreeBank = TreeBank(folder: folder2)
removeDifferentTrees()
}

init(folder1: String, folder2: String, pattern: String){
fromTreeBank = TreeBank(folder: folder1, pattern: pattern)
toTreeBank = TreeBank(folder: folder2, pattern: pattern)
removeDifferentTrees()
}

public func removeDifferentTrees(){
var i : Int = 0
var j : Int = 0
while (i < fromTreeBank.size() && j < toTreeBank.size()){
if fromTreeBank.get(index: i).getName() < toTreeBank.get(index: j).getName(){
fromTreeBank.removeTree(index: i);
} else {
if toTreeBank.get(index: j).getName() > fromTreeBank.get(index: i).getName(){
toTreeBank.removeTree(index: j);
} else {
i = i + 1
j = j + 1
}
}
}
while (i < fromTreeBank.size()){
fromTreeBank.removeTree(index: i);
}
while (j < toTreeBank.size()){
toTreeBank.removeTree(index: j);
}
}

public func size() -> Int{
return fromTreeBank.size()
}

public func fromTree(index: Int) -> ParseTree{
return fromTreeBank.get(index: index)
}

public func toTree(index: Int) -> ParseTree{
return toTreeBank.get(index: index)
}

public func getFromTreeBank() -> TreeBank{
return fromTreeBank
}

public func getToTreeBank() -> TreeBank{
return toTreeBank
}
}
9 changes: 9 additions & 0 deletions Sources/ParseTree/ParseTree.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ open class ParseTree{

private static var sentenceLabels : [String] = ["SINV", "SBARQ", "SBAR", "SQ", "S"]
public var root: ParseNode? = nil
public var name: String = ""

/**
* Empty constructor for ParseTree. Initializes the root node to null.
Expand Down Expand Up @@ -47,6 +48,14 @@ open class ParseTree{
}
}

public func setName(name: String){
self.name = name
}

public func getName() -> String{
return name
}

/**
* Gets the next leaf node after the given leaf node in the ParseTree.
- Parameters:
Expand Down
8 changes: 7 additions & 1 deletion Sources/ParseTree/TreeBank.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ open class TreeBank {
public init(folder: String){
let fileManager = FileManager.default
do {
let listOfFiles = try fileManager.contentsOfDirectory(atPath: folder)
var listOfFiles : [String] = try fileManager.contentsOfDirectory(atPath: folder)
listOfFiles.sort()
for file in listOfFiles {
let thisDirectory = URL(fileURLWithPath: folder)
let url = thisDirectory.appendingPathComponent(file)
let parseTree = ParseTree(url: url)
if parseTree.getRoot() != nil{
parseTree.setName(name: file)
parseTrees.append(parseTree)
}
}
Expand Down Expand Up @@ -103,5 +105,9 @@ open class TreeBank {
public func get(index: Int) -> ParseTree{
return parseTrees[index]
}

public func removeTree(index: Int){
parseTrees.remove(at: index)
}

}
17 changes: 17 additions & 0 deletions Tests/ParseTreeTests/ParallelTreeBankTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import XCTest
@testable import ParseTree
import ParseTree

final class ParallelTreeBankTest: XCTestCase {

func testSize() {
let thisSourceFile = URL(fileURLWithPath: #file)
let thisDirectory = thisSourceFile.deletingLastPathComponent()
let treeBank1 = ParallelTreeBank(folder1: thisDirectory.appendingPathComponent("trees").path, folder2: thisDirectory.appendingPathComponent("trees2").path)
XCTAssertEqual(3, treeBank1.size())
}

static var allTests = [
("testExample1", testSize),
]
}
1 change: 1 addition & 0 deletions Tests/ParseTreeTests/trees2/0001.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
( (S (NP-SBJ (DT The) (NNP Ways) (CC and) (NNP Means) (NNP Committee)) (VP (MD will) (VP (VB hold) (NP (NP (DT a) (NN hearing)) (PP (IN on) (NP (DT the) (NN bill)) ) ) (NP-TMP (IN next) (NNP Tuesday)) ) ) (. .)) )
1 change: 1 addition & 0 deletions Tests/ParseTreeTests/trees2/0002.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
( (S (NP-SBJ-1 (PRP We)) (VP (VBP 're) (VP (IN about) (S (NP-SBJ (-NONE- *-1)) (VP (TO to) (VP (VB see) (SBAR (IN if) (S (NP-SBJ (NN advertising)) (VP (VBZ works))) ) ) ) ) ) ) (. .)) )
1 change: 1 addition & 0 deletions Tests/ParseTreeTests/trees2/0014.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
( (NP (NN Ad) (NNS Notes) (: ...) (. .)) )
1 change: 1 addition & 0 deletions Tests/ParseTreeTests/trees2/0015.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
( (NP-HLN (NNP ARNOLD) (NNP ADVERTISING) (: :)) )

0 comments on commit ed8547c

Please sign in to comment.