Implement: def preprocesstextsegments(df: pd.DataFrame, maxwords: int) -> pd.DataFrame:…
- User
Implement:
def preprocess_text_segments(df: pd.DataFrame, max_words: int) -> pd.DataFrame: """ Divide the text into segments of approx max_words words each. Make sure to not split sentences. Add a column "segment_id" to the dataframe to identify each segment. The ids should be in the format <testimony_id>_<segment_number>. 1. If a testimony has less than max_words, it should have a single segment with segment_number 0. 2. If a testimony has more than max_words, it should be split into multiple segments, each with segment_number starting from 0.
code:param df: :return: """ passYou can use this helper function that calls Hebrew spacy
def segment_hebrew_spacy(text): doc = nlp(text) return [sent.text for sent in doc.sents]
- Assistant
- User
I made some changes. Does it look correct?
- Assistant
- User
The model is loaded above the loop
- Assistant
- User
implement this:
def segment_hebrew_spacy(text, nlp): doc = nlp(text) return [sent.text for sent in doc.sents]
def test_segment_hebrew_spacy(text, nlp): """ just for testing, instead of returning the sentences, split randomly every 600 words :param text: :param nlp: :return: """ pass
- Assistant
Got it 👍 You want a mock version of segment_hebrew_spacy for testing that ignores spaCy and just splits the text every ~600 words. Here’s a simple implementation of test_segment_hebrew_spacy: pythonCopy codeimport random
def test_segment_hebrew_spacy(text, nlp=None): """ Mock segmentation: instead of using spaCy sentences, split the text into chunks of ~600 words. A small random jitter is added so chunks aren't all exactly 600. """ words = text.split() segments = [] i = 0
codewhile i < len(words): # Add some randomness to the split size (e.g., 580–620 words) chunk_size = 600 + random.randint(-20, 20) segment = words[i:i + chunk_size] segments.append(" ".join(segment)) i += chunk_size return segments🔑 Key points:
nlp param is kept for API compatibility, but unused here.
Splits by words, not characters, so word integrity is preserved.
Adds a ±20 word jitter so the splits don’t always fall at exactly 600.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.