Create README.md
Browse files
README.md
CHANGED
|
@@ -1,3 +1,60 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
|
| 3 |
+
## Cleaning
|
| 4 |
+
|
| 5 |
+
Unlike the original dataset, the `func_code_string` column was updated to remove any comments and keep just the code.
|
| 6 |
+
The original version can still be found in the `whole_func_string`.
|
| 7 |
+
|
| 8 |
+
```py
|
| 9 |
+
import re
|
| 10 |
+
|
| 11 |
+
def remove_comments_docstrings(code, language):
|
| 12 |
+
if language == 'python':
|
| 13 |
+
# Remove docstrings
|
| 14 |
+
code = re.sub(r'"""(.*?)"""', '', code, flags=re.DOTALL)
|
| 15 |
+
code = re.sub(r"'''(.*?)'''", '', code, flags=re.DOTALL)
|
| 16 |
+
# Remove comments
|
| 17 |
+
code = re.sub(r'#.*', '', code)
|
| 18 |
+
elif language == 'java' or language == 'javascript':
|
| 19 |
+
# Remove multiline comments
|
| 20 |
+
code = re.sub(r'/\*.*?\*/', '', code, flags=re.DOTALL)
|
| 21 |
+
# Remove single line comments
|
| 22 |
+
code = re.sub(r'//.*', '', code)
|
| 23 |
+
elif language == 'go':
|
| 24 |
+
# Similar to Java/Javascript
|
| 25 |
+
code = re.sub(r'/\*.*?\*/', '', code, flags=re.DOTALL)
|
| 26 |
+
code = re.sub(r'//.*', '', code)
|
| 27 |
+
elif language == 'ruby':
|
| 28 |
+
# Remove multiline comments
|
| 29 |
+
code = re.sub(r'=begin.*?=end', '', code, flags=re.DOTALL)
|
| 30 |
+
# Remove single line comments
|
| 31 |
+
code = re.sub(r'#.*', '', code)
|
| 32 |
+
elif language == 'php':
|
| 33 |
+
# Remove multiline comments
|
| 34 |
+
code = re.sub(r'/\*.*?\*/', '', code, flags=re.DOTALL)
|
| 35 |
+
# Remove single line and hash comments
|
| 36 |
+
code = re.sub(r'//.*', '', code)
|
| 37 |
+
code = re.sub(r'#.*', '', code)
|
| 38 |
+
return code.strip()
|
| 39 |
+
```
|
| 40 |
+
|
| 41 |
+
The validity of that snippet can be tested with the following example:
|
| 42 |
+
|
| 43 |
+
```py
|
| 44 |
+
# Example DataFrame
|
| 45 |
+
import pandas as pd
|
| 46 |
+
example = {
|
| 47 |
+
'language': ['python', 'java', 'javascript', 'go', 'ruby', 'php'],
|
| 48 |
+
'func_code_string': [
|
| 49 |
+
'"""Example docstring""" def foo(): # This is a comment\n return 1',
|
| 50 |
+
'/** Java doc */ public class Test { // Comment\n public void method() {} }',
|
| 51 |
+
'/* JS doc */ function test() { // Comment\n return true; }',
|
| 52 |
+
'/* Go doc */ package main // Import comment\nimport "fmt"',
|
| 53 |
+
'=begin Ruby doc =end def foo # Comment\n 1 + 1 end',
|
| 54 |
+
'<?php /* PHP doc */ // Comment\necho "Hello"; # Another comment ?>'
|
| 55 |
+
]}
|
| 56 |
+
example_df = pd.DataFrame(example)
|
| 57 |
+
|
| 58 |
+
example_df['cleaned_code'] = example_df.apply(lambda x: remove_comments_docstrings(x['func_code_string'], x['language']), axis=1)
|
| 59 |
+
print(example_df[['language', 'cleaned_code']])
|
| 60 |
+
```
|