This repository has been archived by the owner on May 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__main__.py
135 lines (115 loc) · 4.53 KB
/
__main__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import getopt
import sys
import os
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/")
# Process
from Analysis.process import OpenSynonyms
# Analysis
from Analysis import computation
# Shelves
from Corpus.collatinus import Collatini
from Corpus.greek import Greek
# Commandlines
from Tools.cmd import color
# We set up a list of available to run corpus
# Tuples
AvailableCorpus = [
["Greek", Greek, "(Greek) Corpus based on LSJ"],
["Collatinus", Collatini, "(Latin) Corpus based on Collatinus lexicons"]
]
AvailableAlgorythm = [
["CosineSim", computation.CosineSim, "A Cosine similarity algorythm using normal frequencies"],
["TfIDFCosineSim", computation.TfIdfCosineSim, "A Cosine similarity algorythm using Tf-Idf weighted frequencies"]
]
if __name__ == "__main__":
try:
opts, args = getopt.getopt(sys.argv[1:], "hc:", ["corpus=", "help", "algorythm=", "force=", "csv", "csv=", "search="])
except getopt.GetoptError:
opts = []
corpus = None
algorythm = AvailableAlgorythm[0][1]
algorythmString = AvailableAlgorythm[0][0]
force = False
export = None
search = None
for o in opts:
if o[0] in ["h", "--help"]:
print("""{0}Help for Open Philology Synonym Generator{1}
{3}Commands:{1}
{2}--corpus= , c{1}\t Define the corpus
{2}--algorythm= , a{1}\t Define the algorythm you're using
{2}--search=POS,{1}\t Search for synonyms given. Parameter is using format PartOfSpeach,lemma
{2}--csv,--csv=path {1}\t Export results to csv. If --csv=path, then exporting to given path
{2}--force=0{1}\t Force the reconstruction of the cache. --force=1 means you reconstruct. Default 0""".format(color.DARKCYAN, color.END, color.BLUE, color.UNDERLINE))
print ("\n{0}Corpora:{1}".format(color.UNDERLINE, color.END))
i = 0
for corpus in AvailableCorpus:
print ("{0}\t {1} \t\te.g. {4}--corpus={2}, -c{3}, -c{2}{5}".format(color.BLUE + corpus[0] + color.END, corpus[2], corpus[0], i, color.DARKCYAN, color.END))
i += 1
print ("\n{0}Algorythms:{1}".format(color.UNDERLINE, color.END))
i = 0
for algo in AvailableAlgorythm:
print ("{0}\t {1} \t\te.g. {4}--algorythm={2}, -a {3}, -a {2}{5}".format(color.BLUE + algo[0] + color.END, algo[2], algo[0], i, color.DARKCYAN, color.END))
i += 1
sys.exit()
if o[0] in ["--force"]:
if o[1].isdigit():
z = int(o[1])
if z == 1:
force = True
if o[0] in ["c", "--corpus"]:
if o[1].isdigit():
z = int(o[1])
if len(AvailableCorpus) - 1 > z:
corpus = AvailableCorpus[z]
else:
match = [group for group in AvailableCorpus if group[0] == o[1]]
if len(match) == 1:
corpus = match[0]
if o[0] in ["a", "--algorythm"]:
if o[1].isdigit():
z = int(o[1])
if len(AvailableAlgorythm) - 1 > z:
algorythm = AvailableAlgorythm[z][1]
algorythmString = AvailableAlgorythm[z][0]
else:
match = [group for group in AvailableAlgorythm if group[0] == o[1]]
if len(match) == 1:
algorythm = match[0][1]
else:
print("Unknown algorythm")
sys.exit()
if o[0] in ["--csv"]:
export = "csv"
path = None
if len(o[1]) > 0:
path = o[1]
if o[0] in ["--search"]:
r = o[1].split(",")
search = (r[0], "".join(r[1:]))
if corpus is None:
print("Unknown Corpus")
sys.exit()
instance = OpenSynonyms(
corpus=corpus[1],
algorythm=algorythm
)
instance.generate()
instance.analyse(debug=True)
if type(search) == tuple:
try:
results = instance.search(
POS=search[0],
lemma=search[1]
)
except Exception as E:
print (E)
sys.exit()
print ("{1}Score{0}\t\t\t{2}Lemma{0}".format(color.END, color.DARKCYAN, color.BLUE))
print ("{1}----------------------{0}".format(color.END, color.RED))
for result in results:
print ("{1}{3}{0}\t{2}{4}{0}".format(color.END, color.DARKCYAN, color.BLUE, result[1], result[0]))
if export == "csv":
instance.to_csv(debug=True)