11var fs = require ( 'fs' ) ;
22
3- var USER_1 = "Leon Erath"
4- var USER_2 = "Daniel Salomon "
5- var DATE_START = 0
6- var DATE_END = 15
7- var USER_START = 17
8- var TIME_DIFFERENCE = 2
9-
3+ // Constants
4+ let USER_1 = "Leon Erath "
5+ let USER_2 = "Daniel Salomon"
6+ let DATE_START = 0
7+ let DATE_END = 15
8+ let USER_START = 17
9+ let TIME_DIFFERENCE = 2
1010
1111
12+ // @parseText read .txt file and converts it to a String
1213function parseText ( input ) {
1314 var remaining = '' ;
1415
@@ -25,6 +26,10 @@ function parseText(input) {
2526
2627}
2728
29+
30+ // @paragraphText fixes \n so that one Text is in one line
31+ // additionally replaces user name with A and B
32+ // deletes texts with http Links or <Medien weggelassen> tags
2833function paragraphTexts ( data ) {
2934
3035 var text = ""
@@ -57,6 +62,8 @@ function paragraphTexts(data){
5762 groupTexts ( text )
5863}
5964
65+ // @groupTexts groups Texts from the same author
66+ // additionally add blank line if the conversation has a gap of TIME_DIFFERENCE
6067function groupTexts ( data ) {
6168
6269 var groupedText = ""
@@ -82,16 +89,16 @@ function groupTexts(data){
8289
8390 k = 0 ;
8491 while ( line . charAt ( 18 ) == line2 . charAt ( 18 ) ) {
85- k ++
86- line2 = line2 . substring ( 20 , line2 . length )
87-
88- if ( line . charAt ( line . length - 1 ) != "." && line . charAt ( line . length - 1 ) != "?" && line . charAt ( line . length - 1 ) != "!" ) {
89- line = line + "."
90- }
91-
92- line = line + line2
93- line2 = lines [ i + 1 + k ]
92+ k ++
93+ line2 = line2 . substring ( 20 , line2 . length )
9494
95+ if ( line . charAt ( line . length - 1 ) != "." && line . charAt ( line . length - 1 ) != "?" && line . charAt ( line . length - 1 ) != "!" ) {
96+ line = line + "."
97+ }
98+
99+ line = line + line2
100+ line2 = lines [ i + 1 + k ]
101+
95102 }
96103 i = i + k
97104 groupedText = groupedText + line + "\n" ;
@@ -101,39 +108,45 @@ function groupTexts(data){
101108
102109}
103110
111+ // @createFile creates .txt file for given input
104112function createFile ( data ) {
105113 fs . writeFile ( "export/chat.txt" , data , function ( err ) {
106114 if ( err ) {
107115 return console . log ( err ) ;
108116 }
109-
110117 console . log ( "The file was saved!" ) ;
111118 } ) ;
112119}
113120
114-
121+ // @parseDate create a Date of given input
122+ // returns false if input is invalid
123+ // neccessary to calculate time diffrence between texts
115124function parseDate ( input ) {
116125 input = input . substring ( DATE_START , DATE_END )
117126
127+ // replaces characters so that the string can be splitted
118128 input = input . replace ( "," , "." )
119129 input = input . replace ( ":" , "." )
120130 input = input . replace ( " " , "" )
121131 var parts = input . split ( '.' ) ;
122132
133+ // because of reasons you need to add 2 hours and subtract 1 month (dont ask me why)
123134 var date = new Date ( "20" + parts [ 2 ] , parts [ 1 ] - 1 , parts [ 0 ] , parts [ 3 ] , parts [ 4 ] ) . addHours ( 2 ) ;
124135 if ( date . toString ( ) == "Invalid Date" ) {
125136 return false
126137 }
127138 return date
128139}
129140
141+ // @addHours helper function for parseDate
130142Date . prototype . addHours = function ( h ) {
131143 this . setHours ( this . getHours ( ) + h ) ;
132144 return this ;
133145}
134146
135147
136- // reads chat.txt into input
148+ // reads chat_full.txt into input
149+ // .txt file must be in the root path
137150var input = fs . createReadStream ( 'chat_full.txt' ) ;
138151
139152// parses Text with given input
0 commit comments