-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utilities.cs
172 lines (141 loc) · 5.84 KB
/
Utilities.cs
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
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using DDW;
using DDW.Collections;
namespace Prototype
{
class Utilities
{
public static bool[] CreateSkipList(string[] document)
{
bool[] skips = new bool[document.Length];
for (int i = 0; i < document.Length; i++)
{
bool hasLetterOrDigit = false;
char[] chars = document[i].ToCharArray();
foreach (char c in chars)
{
if (!Char.IsWhiteSpace(c))
//if (Char.IsLetterOrDigit(c))
{
hasLetterOrDigit = true;
break;
}
}
skips[i] = !hasLetterOrDigit;
}
return skips;
}
public static CompilationUnitNode ParseDocument(string fileName)
{
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs, true);
Lexer l = new Lexer(sr);
TokenCollection toks = l.Lex();
Parser p = new Parser(fileName);
CompilationUnitNode cu = p.Parse(toks, l.StringLiterals);
return cu;
}
public static TokenCollection LexDocument(string fileName)
{
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs, true);
Lexer l = new Lexer(sr);
TokenCollection toks = l.Lex();
return toks;
}
public static string[] FileToStringArray(string filename)
{
StreamReader sr = File.OpenText(filename);
List<string> lines = new List<string>();
string line;
while ((line = sr.ReadLine()) != null)
{
lines.Add(line.Replace("\t", " "));
}
sr.Close();
return lines.ToArray();
}
private static void AddToNameTable(Dictionary<string, List<int>> nameTable, MemberNode idn)
{
string name = idn.Names[0].GenericIdentifier;
int line = idn.RelatedToken.Line - 1;
if (nameTable.ContainsKey(name))
nameTable[name].Add(line);
else
{
List<int> lines = new List<int>();
lines.Add(line);
nameTable.Add(name, lines);
}
}
public static float[][] CreateSemanticWeights(int gain)
{
// definition of array
float[][] _semanticWeights = new float[Model.Default.Length][];
// initialization of array
for (int i = 0; i < _semanticWeights.Length; i++)
{
_semanticWeights[i] = new float[Model.Default.Length];
for (int j = 0; j < _semanticWeights[i].Length; j++)
{
_semanticWeights[i][j] = 0;
}
}
// define nametable, name as key, list of ints as value
Dictionary<string, List<int>> nameTable = new Dictionary<string, List<int>>();
// fill nametable dictionary with method, field and property definitions
foreach (NamespaceNode nsn in Model.Default.ParsedDocument.Namespaces)
{
foreach (ClassNode cn in nsn.Classes)
{
foreach (MethodNode mn in cn.Methods)
AddToNameTable(nameTable, mn);
foreach (FieldNode fn in cn.Fields)
AddToNameTable(nameTable, fn);
foreach (PropertyNode pn in cn.Properties)
AddToNameTable(nameTable, pn);
}
}
Dictionary<string, List<int>> referenceTable = new Dictionary<string, List<int>>();
foreach (KeyValuePair<string, List<int>> d in nameTable)
{
referenceTable.Add(d.Key, new List<int>());
}
LinkedListNode<DDW.Token> token = Model.Default.LexedDocument.First;
List<string> par = new List<string>(); //For debugging
while (token != null)
{
if (token.Value.ID == TokenID.Ident)
{
int count;
if (token.Next == null || token.Next.Value.Line != token.Value.Line)
count = Model.Default.Document[token.Value.Line - 1].Length - (token.Value.Col);
else
count = token.Next.Value.Col - token.Value.Col;
string part = Model.Default.Document[token.Value.Line - 1].Substring(token.Value.Col, count).Trim();
par.Add(part);
if (referenceTable.ContainsKey(part) && !referenceTable[part].Contains(token.Value.Line - 1))
referenceTable[part].Add(token.Value.Line - 1);
}
token = token.Next;
}
foreach (KeyValuePair<string, List<int>> kv in referenceTable)
{
foreach (int j in kv.Value)
{
for (int f = -Model.Default.FocusAreaRadius; f <= Model.Default.FocusAreaRadius; f++)
{
if (j + f < 0 || j + f >= Model.Default.Length)
continue;
foreach (int i in nameTable[kv.Key])
_semanticWeights[j + f][i] = gain;
}
}
}
return _semanticWeights;
}
}
}