Skip to content

Commit 706ce7a

Browse files
authored
Add multimodality table to documentation (#639)
1 parent 5c8cd20 commit 706ce7a

File tree

7 files changed

+741
-64
lines changed

7 files changed

+741
-64
lines changed

docs/generate_model_js.py

Lines changed: 110 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
2-
2+
import os
3+
import glob
34

45
def get_key_val(part):
56
key_index_start = part.index('[')
@@ -13,48 +14,8 @@ def get_key_val(part):
1314
return key, val
1415

1516

16-
# Read the content from README.md
17-
with open('../README.md', 'r') as file:
18-
content = file.read()
19-
20-
# Extract the relevant information from the content
21-
models = []
22-
lines = content.split('\n')
23-
lines = lines[lines.index('## Models') + 4: lines.index('## Resources') - 2]
24-
25-
headers = []
26-
headers = lines[0].split('|')[1:-1]
27-
headers = [header.strip() for header in headers]
28-
29-
for line in lines[2:]:
30-
parts = line.split('|')[1:-1]
31-
parts = [part.strip() for part in parts]
32-
model = dict(zip(headers, parts))
33-
models.append(model)
34-
35-
year = None
36-
37-
for model in models:
38-
# handle empty years
39-
if model["Year"] == "":
40-
model["Year"] = year
41-
else:
42-
year = model["Year"]
43-
44-
# handle model, docs and paper part
45-
name_paper_str = model["Model and Paper"]
46-
47-
for i, part in enumerate(name_paper_str.split(', ')):
48-
key, val = get_key_val(part)
49-
50-
if i == 0:
51-
model["Name"] = key
52-
model["Link"] = val
53-
else:
54-
model[key] = val
55-
17+
def add_pytorch_tensorflow(model):
5618
# handle environment part
57-
5819
env_part = model["Environment"].split(', ')[0]
5920

6021
search_dict = {
@@ -74,28 +35,117 @@ def get_key_val(part):
7435
else:
7536
for header, _ in search_dict.items():
7637
model[header] = False
38+
39+
40+
def add_modalities_for_model(model):
41+
modalities_keywords = {
42+
"User Text": "user_text",
43+
"User Image": "user_image",
44+
"User Graph": "user_graph",
45+
"Item Text": "item_text",
46+
"Item Image": "item_image",
47+
"Item Graph": "item_graph",
48+
"Sentiment": "sentiment",
49+
"Review Text": "review_text",
50+
}
7751

78-
# remove non required keys
79-
model.pop("Model and Paper")
80-
model.pop("Environment")
81-
82-
# Get package name
83-
model_dir = model["Link"]
52+
for filename in glob.glob(f'../{model["Link"]}/*.py', recursive=True):
53+
with open(filename, 'r') as file:
54+
file_data = file.read()
55+
56+
for header, modality_keyword in modalities_keywords.items():
57+
is_found = modality_keyword in file_data
58+
if is_found:
59+
model[header] = True
60+
61+
# for user feature and item feature
62+
# >> if user feature is found, we set user text, image and graph to true
63+
is_found = "user_feature" in file_data
64+
if is_found:
65+
model["User Text"] = True
66+
model["User Image"] = True
67+
model["User Graph"] = True
68+
69+
# likewise for item feature
70+
is_found = "item_feature" in file_data
71+
if is_found:
72+
model["Item Text"] = True
73+
model["Item Image"] = True
74+
model["Item Graph"] = True
8475

85-
with open(f'../{model_dir}/__init__.py', 'r') as file:
86-
init_data = file.read()
76+
for header, modality_keyword in modalities_keywords.items():
77+
if header not in model:
78+
model[header] = False
79+
80+
81+
if __name__ == "__main__":
82+
# Read the content from README.md
83+
with open('../README.md', 'r') as file:
84+
content = file.read()
85+
86+
# Extract the relevant information from the content
87+
models = []
88+
lines = content.split('\n')
89+
lines = lines[lines.index('## Models') + 4: lines.index('## Resources') - 2]
90+
91+
headers = []
92+
headers = lines[0].split('|')[1:-1]
93+
headers = [header.strip() for header in headers]
94+
95+
for line in lines[2:]:
96+
parts = line.split('|')[1:-1]
97+
parts = [part.strip() for part in parts]
98+
model = dict(zip(headers, parts))
99+
models.append(model)
100+
101+
year = None
102+
103+
for model in models:
104+
# handle empty years
105+
if model["Year"] == "":
106+
model["Year"] = year
107+
else:
108+
year = model["Year"]
109+
110+
# handle model, docs and paper part
111+
name_paper_str = model["Model and Paper"]
112+
113+
for i, part in enumerate(name_paper_str.split(', ')):
114+
key, val = get_key_val(part)
115+
116+
if i == 0:
117+
model["Name"] = key
118+
model["Link"] = val
119+
else:
120+
model[key] = val
87121

88-
package_names = []
122+
# Check for PyTorch and TensorFlow in each requirements file
123+
add_pytorch_tensorflow(model)
89124

90-
for row in init_data.split('\n'):
91-
if "import" in row:
92-
package_name = row[row.index("import") + len("import "):]
93-
package_names.append(f"cornac.models.{package_name}")
125+
# Check for modalities keywords in files and add to model
126+
add_modalities_for_model(model)
127+
128+
# remove non required keys
129+
model.pop("Model and Paper")
130+
model.pop("Environment")
94131

95-
model["packages"] = package_names
132+
# Get package name
133+
model_dir = model["Link"]
134+
135+
with open(f'../{model_dir}/__init__.py', 'r') as file:
136+
init_data = file.read()
137+
138+
package_names = []
139+
140+
for row in init_data.split('\n'):
141+
if "import" in row:
142+
package_name = row[row.index("import") + len("import "):]
143+
package_names.append(f"cornac.models.{package_name}")
144+
145+
model["packages"] = package_names
96146

97-
json_str = json.dumps(models, indent=4)
147+
json_str = json.dumps(models, indent=4)
98148

99-
# Write the JSON object to a file
100-
with open('source/_static/models/data.js', 'w') as file:
101-
file.write(f"var data = {json_str};")
149+
# Write the JSON object to a file
150+
with open('source/_static/models/data.js', 'w') as file:
151+
file.write(f"var data = {json_str};")

0 commit comments

Comments
 (0)