Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
COM6001M Computer Science Major Project
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
benjamin.clough
COM6001M Computer Science Major Project
Commits
8a798d04
Commit
8a798d04
authored
May 17, 2023
by
benjamin.clough
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Upload New File
parent
0647a768
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
48 additions
and
0 deletions
+48
-0
preprocessing_functionality.py
preprocessing_functionality.py
+48
-0
No files found.
preprocessing_functionality.py
0 → 100644
View file @
8a798d04
import
nltk
import
pandas
from
nltk.stem
import
WordNetLemmatizer
,
PorterStemmer
from
bs4
import
BeautifulSoup
import
re
lemmatizer
=
nltk
.
stem
.
WordNetLemmatizer
()
stemmer
=
nltk
.
PorterStemmer
()
def
clean_text
(
text
:
str
)
->
str
:
# Strip HTML & XML
text
=
BeautifulSoup
(
text
,
"lxml"
)
.
text
text
=
re
.
sub
(
r'\|\|\|'
,
r' '
,
text
)
# Strip Hyperlinks & URLs
text
=
re
.
sub
(
r'http\S+'
,
r'<URL>'
,
text
)
# Send to lowercase
text
=
text
.
lower
()
text
=
text
.
replace
(
'x'
,
''
)
return
text
def
tokenize_text
(
text
):
untokenized_sentences
=
nltk
.
sent_tokenize
(
text
)
tokenized_sentences
=
[
tokenize_sentence
(
sentence
)
for
sentence
in
untokenized_sentences
]
tokens
=
[
token
for
tokenized_sentence
in
tokenized_sentences
for
token
in
tokenized_sentence
]
return
tokens
def
tokenize_sentence
(
sentence
):
tokenized_sentence
=
nltk
.
word_tokenize
(
sentence
)
tokenized_sentence
=
[
word
for
word
in
tokenized_sentence
if
len
(
word
)
>
2
]
return
tokenized_sentence
def
lemmatize_text
(
text
:
list
):
lemmatized
=
[
lemmatizer
.
lemmatize
(
word
)
for
word
in
text
]
return
' '
.
join
(
lemmatized
)
def
stem_text
(
text
:
list
):
stemmed
=
[
stemmer
.
stem
(
word
)
for
word
in
text
]
return
' '
.
join
(
stemmed
)
if
__name__
==
'__main__'
:
print
(
stemmer
.
stem
(
'connecting'
))
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment