Ontology plays an important role in knowledge representation, especially in the domain of information retrieval. However, building ontology remains a challenging problem because it is a time-consuming task for experts. To overcome these drawbacks, we propose a novel approach called the Automatic Thai Legal Ontology Building (ATOB) algorithm for automatic legal ontology building and to improve the court sentences retrieval process. The ATOB can automatically generate seed ontology and expand the ontology using Thai legal terminology, i.e. TLlexicon. The expansion process is terminated automatically by the threshold parameter. Moreover, the ATOB applies the concept of the ant colony algorithm to improve the court sentences retrieval process. We conclude that the effective ontology should be weight-embedded. The empirical results demonstrate that the performance of the ATOB algorithm is better than that of the traditional search method. The performance figures for the ATOB framework measured in terms of precision, recall, F-measure and diversity are 0.90, 0.91, 0.90 and 0.39, respectively.