-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsegment_nltk.py
36 lines (28 loc) · 1.11 KB
/
segment_nltk.py
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
import nltk
nltk.download('punkt')
# Load the text file as a string
with open('transcript.txt', 'r') as f:
text = f.read()
# Tokenize the text into sentences
sentences = nltk.sent_tokenize(text)
# Define a function to check if a sentence contains a mention of a new sample
def contains_new_sample(sentence):
# Here, you would need to define your own logic to check if a sentence mentions a new sample
# For example, you could look for keywords like "sample", "product", or specific sample names
# and return True if the sentence contains a mention of a new sample
if "new tree" in sentence.lower():
return True
return False
# Segment the text into parts where a new sample is being discussed
segments = []
current_segment = []
for sentence in sentences:
if contains_new_sample(sentence):
if current_segment:
segments.append(" ".join(current_segment))
current_segment = []
current_segment.append(sentence)
segments.append(" ".join(current_segment))
# Print the segments
for i, segment in enumerate(segments):
print(f"Sample {i+1} observations:\n{segment}")