FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
TensorFlow.NET/Tensorflow.CodeGen/DescriptionGenerator.cs at master · Solips1sm/TensorFlow.NET · GitHub
Solips1sm
/
TensorFlow.NET
Public
forked from
SciSharp/TensorFlow.NET
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
TensorFlow.NET
/
Tensorflow.CodeGen
/
DescriptionGenerator.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
263 lines (246 loc) · 9.42 KB
Breadcrumbs
TensorFlow.NET
/
Tensorflow.CodeGen
/
DescriptionGenerator.cs
Copy path
File metadata and controls
263 lines (246 loc) · 9.42 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
using
Microsoft
.
CodeAnalysis
.
CSharp
;
using
Protobuf
.
Text
;
using
System
;
using
System
.
Collections
.
Generic
;
using
System
.
Linq
;
using
System
.
Reflection
.
Metadata
.
Ecma335
;
using
System
.
Text
;
using
System
.
Text
.
RegularExpressions
;
using
System
.
Threading
.
Tasks
;
namespace
Tensorflow
.
CodeGen
{
public
class
DescriptionGenerator
{
private
static
readonly
string
replaceStrInner
=
"~~%~~"
;
private
static
readonly
string
replaceStrInnerQuotationMarks
=
"^%^"
;
Dictionary
<
string
,
Dictionary
<
string
,
string
>
>
_opDescriptions
=
new
Dictionary
<
string
,
Dictionary
<
string
,
string
>
>
(
)
;
Dictionary
<
string
,
OpDef
>
_opDescriptionDefs
=
new
Dictionary
<
string
,
OpDef
>
(
)
;
public
DescriptionGenerator
(
string
apiDefDirectory
)
{
DirectoryInfo
directory
=
new
DirectoryInfo
(
apiDefDirectory
)
;
int
errors
=
0
;
foreach
(
FileInfo
file
in
directory
.
GetFiles
(
)
)
{
string
target
=
file
.
Name
.
Split
(
'.'
)
[
0
]
.
Split
(
'_'
)
.
Last
(
)
;
OpDef
op
=
null
;
try
{
op
=
ReadOpDefs
(
file
.
FullName
)
.
Op
[
0
]
;
}
catch
{
errors
++
;
continue
;
}
_opDescriptionDefs
[
target
]
=
op
;
_opDescriptions
[
target
]
=
new
Dictionary
<
string
,
string
>
(
)
;
foreach
(
var
arg
in
op
.
InputArg
)
{
string
argName
=
arg
.
Name
;
var
token
=
SyntaxFactory
.
ParseToken
(
argName
)
;
if
(
token
.
IsKeyword
(
)
)
{
argName
=
$
"
{
argName
}
_"
;
}
_opDescriptions
[
target
]
[
argName
]
=
arg
.
Description
??
""
;
}
foreach
(
var
arg
in
op
.
Attr
)
{
var
token
=
SyntaxFactory
.
ParseToken
(
arg
.
Name
)
;
string
realKey
=
arg
.
Name
;
if
(
token
.
IsKeyword
(
)
)
{
realKey
+=
"_"
;
}
_opDescriptions
[
target
]
[
realKey
]
=
arg
.
Description
??
""
;
}
_opDescriptions
[
target
]
[
"SUMMARY"
]
=
op
.
Summary
??
""
;
_opDescriptions
[
target
]
[
"DESC"
]
=
op
.
Description
??
""
;
}
Console
.
WriteLine
(
$
"Warning:
{
errors
}
description files cannot be analyzed! Please revise it if "
+
$
"the failed files number is large, or ignore it."
)
;
}
/// <summary>
///
/// </summary>
/// <param name="op"></param>
/// <param name="sb"></param>
public
void
AppendDescription
(
OpDef
fullOp
,
StringBuilder
sb
)
{
var
opName
=
fullOp
.
Name
;
if
(
_opDescriptions
.
TryGetValue
(
opName
,
out
var
op
)
)
{
var
def
=
_opDescriptionDefs
[
opName
]
;
sb
.
AppendLine
(
"/// <summary>"
)
;
sb
.
AppendLine
(
$
"///
{
op
[
"SUMMARY"
]
}
"
)
;
sb
.
AppendLine
(
"/// </summary>"
)
;
string
totalDesc
=
op
[
"DESC"
]
;
if
(
!
string
.
IsNullOrEmpty
(
totalDesc
)
)
{
totalDesc
=
totalDesc
.
Replace
(
replaceStrInnerQuotationMarks
,
"
\"
"
)
;
sb
.
AppendLine
(
"/// <remarks>"
)
;
string
[
]
lines
=
totalDesc
.
Split
(
replaceStrInner
)
;
foreach
(
var
line
in
lines
)
{
sb
.
AppendLine
(
$
"///
{
line
}
"
)
;
}
sb
.
AppendLine
(
"/// </remarks>"
)
;
}
var
argNames
=
GetInputArgNames
(
fullOp
)
;
foreach
(
var
argName
in
argNames
)
{
if
(
op
.
TryGetValue
(
argName
,
out
var
desc
)
)
{
desc
=
desc
.
Replace
(
replaceStrInnerQuotationMarks
,
"
\"
"
)
;
string
[
]
lines
=
desc
.
Split
(
replaceStrInner
)
;
sb
.
AppendLine
(
$
"/// <param name=
\"
{
argName
}
\"
>"
)
;
foreach
(
var
line
in
lines
)
{
sb
.
AppendLine
(
$
"///
{
line
}
"
)
;
}
sb
.
AppendLine
(
"/// </param>"
)
;
}
else
{
sb
.
AppendLine
(
$
"/// <param name=
\"
{
argName
}
\"
></param>"
)
;
}
}
List
<
string
>
returnValueDescs
=
new
(
)
;
foreach
(
var
arg
in
def
.
OutputArg
)
{
if
(
!
string
.
IsNullOrEmpty
(
arg
.
Description
)
)
{
returnValueDescs
.
Add
(
$
"
{
arg
.
Name
}
:
{
arg
.
Description
}
"
)
;
}
}
string
returnValueDesc
=
""
;
if
(
returnValueDescs
.
Count
>
0
)
{
returnValueDesc
=
string
.
Join
(
" && "
,
returnValueDescs
)
;
}
sb
.
AppendLine
(
$
"/// <returns>
{
returnValueDesc
}
</returns>"
)
;
}
else
{
sb
.
AppendLine
(
"/// <summary>"
)
;
sb
.
AppendLine
(
$
"///"
)
;
sb
.
AppendLine
(
"/// </summary>"
)
;
var
argNames
=
GetInputArgNames
(
fullOp
)
;
foreach
(
var
argName
in
argNames
)
{
sb
.
AppendLine
(
$
"/// <param name=
\"
{
argName
}
\"
></param>"
)
;
}
sb
.
AppendLine
(
$
"/// <returns></returns>"
)
;
}
}
/// <summary>
///
/// </summary>
/// <param name="op">
/// </param>
/// <returns></returns>
/// <remarks></remarks>
public
List
<
string
>
GetInputArgNames
(
OpDef
op
)
{
List
<
string
>
names
=
new
(
)
;
foreach
(
var
arg
in
op
.
InputArg
)
{
string
argName
=
arg
.
Name
;
var
token
=
SyntaxFactory
.
ParseToken
(
argName
)
;
if
(
token
.
IsKeyword
(
)
)
{
argName
=
$
"
{
argName
}
_"
;
}
names
.
Add
(
argName
)
;
}
var
attrValueDic
=
Utils
.
GetAttrsDefaultValue
(
op
,
out
var
dynamicDefaultValues
)
;
foreach
(
var
(
key
,
typeStr
,
value
)
in
attrValueDic
)
{
var
token
=
SyntaxFactory
.
ParseToken
(
key
)
;
string
realKey
=
key
;
if
(
token
.
IsKeyword
(
)
)
{
realKey
+=
"_"
;
}
names
.
Add
(
realKey
)
;
}
return
names
;
}
private
static
OpList
ReadOpDefs
(
string
path
)
{
var
text
=
File
.
ReadAllText
(
path
)
;
text
=
RemoveLintTags
(
text
)
;
text
=
PreProcessText
(
text
)
;
string
pattern
=
@"<<END([\s\S]*?)END"
;
// 定义用于替换的字符串
string
replaceStrPrefix
=
"
\"
"
;
string
replaceStrSuffix
=
"
\"
"
;
// 将匹配到的文本段全部替换
string
replacedText
=
Regex
.
Replace
(
text
,
pattern
,
match
=>
{
string
matchedText
=
match
.
Value
;
string
innerText
=
match
.
Groups
[
1
]
.
Value
;
innerText
=
innerText
.
Replace
(
"
\"
"
,
replaceStrInnerQuotationMarks
)
.
Replace
(
"
\r
\n
"
,
replaceStrInner
)
.
Replace
(
"
\n
"
,
replaceStrInner
)
;
// 替换内部换行符
return
replaceStrPrefix
+
innerText
+
replaceStrSuffix
;
// 替换首尾
}
,
RegexOptions
.
Multiline
)
;
var
opDefs
=
new
TextParser
(
TextParser
.
Settings
.
Default
.
WithIgnoreUnknownFields
(
true
)
)
.
Parse
<
OpList
>
(
replacedText
)
;
return
opDefs
;
}
static
string
PreProcessText
(
string
input
)
{
int
depth
=
0
;
int
endBlockDepth
=
-
1
;
StringBuilder
sb
=
new
StringBuilder
(
)
;
for
(
int
i
=
0
;
i
<
input
.
Length
;
i
++
)
{
char
c
=
input
[
i
]
;
if
(
c
==
'{'
)
{
depth
++
;
sb
.
Append
(
c
)
;
}
else
if
(
c
==
'}'
)
{
if
(
depth
==
endBlockDepth
)
{
sb
.
Append
(
"END
\n
"
)
;
endBlockDepth
=
-
1
;
}
sb
.
Append
(
c
)
;
depth
--
;
}
else
if
(
c
==
'<'
&&
i
+
5
<
input
.
Length
&&
input
.
Substring
(
i
,
5
)
==
"<<END"
)
{
endBlockDepth
=
depth
;
sb
.
Append
(
"<<END"
)
;
i
+=
4
;
}
else
if
(
c
==
'E'
&&
i
+
3
<
input
.
Length
&&
input
.
Substring
(
i
,
3
)
==
"END"
)
{
endBlockDepth
=
-
1
;
sb
.
Append
(
"END"
)
;
i
+=
2
;
}
else
{
sb
.
Append
(
c
)
;
}
}
string
output
=
sb
.
ToString
(
)
;
return
output
;
}
static
string
RemoveLintTags
(
string
input
)
{
string
[
]
lines
=
input
.
Split
(
new
[
]
{
"
\r
\n
"
,
"
\r
"
,
"
\n
"
}
,
StringSplitOptions
.
None
)
;
StringBuilder
sb
=
new
StringBuilder
(
)
;
foreach
(
string
line
in
lines
)
{
if
(
!
line
.
TrimStart
(
)
.
StartsWith
(
"# LINT"
)
)
{
sb
.
AppendLine
(
line
)
;
}
}
return
sb
.
ToString
(
)
.
TrimEnd
(
)
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL