# Joint Extraction of Multiple Relations and Entities from Building Code Clauses
## 2 Methodology
### Model Training and Inference  
In the training phase, the subject extractor and the object and predicate extractor can be jointly trained since they share the same input of the code clauses. For each training instance, a subject entity is randomly selected from the training data set of subject entities as the specified input to the object and predicate extractor. By comparing the model output with the actual labeled results, the loss functions of the subject extractor and the object and predicate extractor can be calculated; these losses are then combined to calculate the total loss, as follows:  
\[\mathcal{L}=\mathcal{L}_{se}+\mathcal{L}_{ope}. \tag{16}\]  
The total loss function in Equation (16) can be optimized using the Adam algorithm [35] to consider the mutual influence of the errors arising in the extraction of the subject entities, object entities, and predicates such that the errors that occur in each task are constrained by the others.  
```
1: Define \(n\leftarrow\) clause length
2: Initialize \(\mathcal{T}\leftarrow\{\}\)
3: Initialize \(\mathcal{S}\leftarrow\{\}\)
4: Obtain SE-TA\({}_{\text{Gsta}}(S)\) using Equation (7)
5: Obtain SE-TA\({}_{\text{Gand}}(S)\) using Equation (9)
6:for\(i\gets 1\) to \(n\)do
7:if SE-TA\({}_{\text{Gsta}}(S)[i]\
eq\) \"\(\mathcal{O}\)\" then
8:for\(j\gets i\) to \(n\)do
9:if SE-TA\({}_{\text{Gsta}}(S)[i]=\) SE-TA\({}_{\text{Gsta}}(S)[j]\)then
10:\(s\gets S[i:j]\)
11:\(\mathcal{S}\leftarrow\mathcal{S}\cup s\)
12: Break
13:for\(s^{*}\gets s\) in \(\mathcal{S}\)do
14: Obtain OPE-TA\({}_{\text{Gsta}}(S)\) using Equation (12)
15: Obtain OPE-TA\({}_{\text{Gend}}(S)\) using Equation (14)
16:for\(i\gets 1\) to \(n\)do
17:if OPE-TA\({}_{\text{Gsta}}(S)[i]\
eq\) \"\(\mathcal{O}\)\" then
18:for\(j\gets i\) to \(n\)do
19:if OPE-TA\({}_{\text{Gsta}}(S)[i]=\) OPE-TAG\({}_{\text{Gend}}(S)[j]\)then
20:\(o\leftarrow[S[i:j]]\)
21:\(p\leftarrow\) OPE-TAG\({}_{\text{Gsta}}(S)[i]\)
22:\(t\gets<s^{*},p,o>\)
23:\(\mathcal{T}\leftarrow\mathcal{T}\cup t\)
24: Break
25:return\(\mathcal{T}\)
```  
**Algorithm 1** Inference Algorithm.  
Algorithm 1 presents the pseudocode for decoding the tagging results from the subject extractor and the object and predicate extractor. Lines 1-3 represent the initialization process for each input clause \(S\) expressed in natural language. Lines 4-12 describe the algorithm for deriving candidate subject entities from the tagging results of the subject extractor. Then, Lines 13-24 describe the algorithm for acquiring all candidate triplets from the tagging results of the object and predicate extractor. In detail, for a sentence with \(k\) subject entities, the entire task is finally deconstructed into two tasks for subject-entity tagging and \(2k\) tasks for object-entity and predicate tagging. By means of this inference algorithm, the triplets can be directly identified from the start/end positions and tags of the entities, thus helping to accelerate the inference process and reduce the demand for computing resources. Moreover, nonrelated pairs can be excluded through the judicious recognition of the subject entities, and the overlapping relation problem can be naturally resolved.