-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathliwc_readDict.py
45 lines (36 loc) · 1.7 KB
/
liwc_readDict.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
37
38
39
40
41
42
43
44
45
# psyLex: an open-source implementation of the Linguistic Inquiry Word Count
# Created by Sean C. Rife, Ph.D.
# [email protected] // seanrife.com // @seanrife
# Licensed under the MIT License
# Function to read in an LIWC-style dictionary
import collections, sys, re
def readDict(dictionaryPath):
catList = collections.OrderedDict()
catLocation = []
wordList = {}
finalDict = []
# Check to make sure the dictionary is properly formatted
with open(dictionaryPath, "r") as dictionaryFile:
for idx, item in enumerate(dictionaryFile):
if "%" in item:
catLocation.append(idx)
if len(catLocation) > 2:
# There are apparently more than two category sections; throw error and die
sys.exit("Invalid dictionary format. Check the number/locations of the category delimiters (%).")
# Read dictionary as lines
with open(dictionaryPath, "r") as dictionaryFile:
lines = dictionaryFile.readlines()
# Within the category section of the dictionary file, grab the numbers associated with each category
for line in lines[catLocation[0] + 1:catLocation[1]]:
catList[re.split(r'\t+', line)[0]] = [re.split(r'\t+', line.rstrip())[1]]
# Now move on to the words
for idx, line in enumerate(lines[catLocation[1] + 1:]):
# Get each line (row), and split it by tabs (\t)
workingRow = re.split('\t', line.rstrip())
wordList[workingRow[0]] = list(workingRow[1:])
# Merge the category list and the word list
for key, values in wordList.items():
for catnum in values:
workingValue = catList[catnum][0]
finalDict.append([key, workingValue])
return finalDict