forked from alimousavifar/lab3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HelperFunctions.cs
177 lines (146 loc) · 5.57 KB
/
HelperFunctions.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
173
174
175
176
using System;
using System.Collections.Generic;
using System.Threading;
using System.Linq;
using System.Globalization;
namespace Lab3Q1
{
public class HelperFunctions
{
/**
* Counts number of words, separated by spaces, in a line.
* @param line string in which to count words
* @param start_idx starting index to search for words
* @return number of words in the line
*/
public int WordCount(ref string line, int start_idx)
{
// YOUR IMPLEMENTATION HERE
int count = 0;
char space = ' ';
string temp = line.Substring(start_idx);
int i = 0;
if (temp.Length >= 1 && temp[0] != ' ')
{
count = 1;
}
else
count = 0;
for (i = 0; i < temp.Length; i++)
{
switch (temp[i] == ' ' && i != (temp.Length - 1) && i == 0)
{
case true:
count++;
break;
case false:
break;
}
}
return count;
}
/**
* Reads a file to count the number of words each actor speaks.
*
* @param filename file to open
* @param mutex mutex for protected access to the shared wcounts map
* @param wcounts a shared map from character -> word count
*/
// public static void CountCharacterWords(string filename,
// Mutex mutex,
// Dictionary<string, int> wcounts)
// {
// //===============================================
// // IMPLEMENT THIS METHOD INCLUDING THREAD SAFETY
// //===============================================
// string line; // for storing each line read from the file
// string character = ""; // empty character to start
// System.IO.StreamReader file = new System.IO.StreamReader(filename);
// while ((line = file.ReadLine()) != null)
// {
// //=================================================
// // YOUR JOB TO ADD WORD COUNT INFORMATION TO MAP
// //=================================================
// // Is the line a dialogueLine?
// // If yes, get the index and the character name.
// // if index > 0 and character not empty
// // get the word counts
// // if the key exists, update the word counts
// // else add a new key-value to the dictionary
// // reset the character
// }
// // Close the file
// }
// /**
// * Checks if the line specifies a character's dialogue, returning
// * the index of the start of the dialogue. If the
// * line specifies a new character is speaking, then extracts the
// * character's name.
// *
// * Assumptions: (doesn't have to be perfect)
// * Line that starts with exactly two spaces has
// * CHARACTER. <dialogue>
// * Line that starts with exactly four spaces
// * continues the dialogue of previous character
// *
// * @param line line to check
// * @param character extracted character name if new character,
// * otherwise leaves character unmodified
// * @return index of start of dialogue if a dialogue line,
// * -1 if not a dialogue line
// */
// static int IsDialogueLine(string line, ref string character)
// {
// // new character
// if (line.Length >= 3 && line[0] == ' '
// && line[1] == ' ' && line[2] != ' ')
// {
// // extract character name
// int start_idx = 2;
// int end_idx = 3;
// while (end_idx <= line.Length && line[end_idx - 1] != '.')
// {
// ++end_idx;
// }
// // no name found
// if (end_idx >= line.Length)
// {
// return 0;
// }
// // extract character's name
// character = line.Substring(start_idx, end_idx - start_idx - 1);
// return end_idx;
// }
// // previous character
// if (line.Length >= 5 && line[0] == ' '
// && line[1] == ' ' && line[2] == ' '
// && line[3] == ' ' && line[4] != ' ')
// {
// // continuation
// return 4;
// }
// return 0;
// }
// /**
// * Sorts characters in descending order by word count
// *
// * @param wcounts a map of character -> word count
// * @return sorted vector of {character, word count} pairs
// */
// public static List<Tuple<int, string>> SortCharactersByWordcount(Dictionary<string, int> wordcount)
// {
// // Implement sorting by word count here
// return sortedByValueList;
// }
// /**
// * Prints the List of Tuple<int, string>
// *
// * @param sortedList
// * @return Nothing
// */
// public static void PrintListofTuples(List<Tuple<int, string>> sortedList)
// {
// // Implement printing here
// }
}
}