-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Subject and SubjectDirectory classes for representing subject cor…
…pus. Fixes #57
- Loading branch information
Showing
3 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
"""A directory of files as a subject corpus""" | ||
|
||
|
||
import glob | ||
import os.path | ||
import re | ||
|
||
|
||
class Subject: | ||
def __init__(self, uri, label, text): | ||
self.uri = uri | ||
self.label = label | ||
self.text = text | ||
|
||
|
||
class SubjectDirectory: | ||
def __init__(self, path): | ||
self.path = path | ||
|
||
def __iter__(self): | ||
"""Iterate through the directory, yielding Subject objects.""" | ||
|
||
for filename in glob.glob(os.path.join(self.path, '*.txt')): | ||
with open(filename) as subjfile: | ||
uri, label = subjfile.readline().strip().split(' ', 1) | ||
text = ' '.join(subjfile.readlines()) | ||
yield Subject(uri, label, text) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters