ContentCoder is a Python-based text analysis tool that enables users to process and analyze text using custom linguistic dictionaries. It is inspired by tools like LIWC (Linguistic Inquiry and Word Count) and provides robust methods for tokenization, text analysis, and frequency calculations.
Note: Approximately 98% of this README was generated by ChatGPT — it may not be entirely accurate, but at a quick glance, it looks pretty spot-on.
- Custom Dictionary-Based Analysis
- Support for LIWC-style dictionaries (2007 & 2022 formats)
- Efficient text tokenization
- Wildcard and abbreviation handling
- Punctuation and big word analysis
- Dictionary export in multiple formats (JSON, CSV, Poster format, etc.)
- High-performance wildcard matching with memory optimization
Ensure you have Python 3.9+ installed. ContentCoder is all native Python and does not require dependencies for installation.
pip install contentcoder
src/contentcoder/
│── __init__.py
│─ ContentCoder.py
│─ ContentCodingDictionary.py
│─ happiestfuntokenizing.py
│─ create_export_dir.py
from contentcoder.ContentCoder import ContentCoder
cc = ContentCoder(dicFilename='path/to/dictionary.dic', fileEncoding='utf-8-sig')
text = "An abrupt sound startled him. Off to the right he heard it, and his ears, expert in such matters, could not be mistaken. Again he heard the sound, and again. Somewhere, off in the blackness, someone had fired a gun three times."
results = cc.Analyze(text, relativeFreq=True, dropPunct=True, retainCaptures=False, returnTokens=True, wildcardMem=True)
print(results)
Expected output:
{
"WC": 23,
"Dic": 5.4,
"BigWords": 6.0,
"Numbers": 3.0,
"AllPunct": 0.0,
"Period": 3.0,
"Comma": 0.0,
"QMark": 0.0,
"Exclam": 0.0,
"Apostro": 0.0
}
Analyzes a given text and returns a dictionary of results.
inputText
(str): The text to analyze.relativeFreq
(bool): IfTrue
, returns relative frequencies. Otherwise, raw frequencies.dropPunct
(bool): IfTrue
, punctuation is removed before processing.retainCaptures
(bool): IfTrue
, captures and stores wildcard-matched words.returnTokens
(bool): IfTrue
, returns tokenized text.wildcardMem
(bool): IfTrue
, speeds up wildcard processing by storing past matches.
result = cc.Analyze("Hello world! This is a test sentence.", returnTokens=True)
Returns a list of all available output categories.
print(cc.GetResultsHeader())
Expected output:
["WC", "Dic", "BigWords", "Numbers", "AllPunct", "Period", "Comma", "QMark", "Exclam", "Apostro"]
Formats the results of Analyze()
into a CSV-friendly list.
text = "The government plays an important role."
result = cc.Analyze(text)
csv_row = cc.GetResultsArray(result)
print(csv_row)
Expected output:
[6, 4.3, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
Exports wildcard-captured words and their frequencies to a CSV file.
cc.ExportCaptures("captured_words.csv")
Exports the loaded dictionary in LIWC-22 format.
cc.dict.ExportDict2022Format("dictionary_2022.dicx")
Updates the categories associated with a dictionary term.
cc.dict.UpdateCategories(dicTerm="happiness", newCategories={"positive_emotion": 1.0, "joy": 0.5})
This script reads a large CSV file and processes each text in the "body"
column.
import csv
from tqdm import tqdm
from contentcoder.ContentCoder import ContentCoder
cc = ContentCoder(dicFilename='dictionary.dic', fileEncoding='utf-8-sig')
with open("Comments.csv", "r", encoding="utf-8-sig") as csvfile, \
open("Output.csv", "w", encoding="utf-8-sig", newline="") as csvfile_out:
reader = csv.DictReader(csvfile)
writer = csv.writer(csvfile_out)
writer.writerow(["id"] + cc.GetResultsHeader())
for row in tqdm(reader, desc="Processing", unit=" comments"):
row_id = row["id"]
text = row["comment_text"]
result = cc.Analyze(text)
csv_row = cc.GetResultsArray(result)
writer.writerow([row_id] + csv_row)
print("Finished!")
MIT License © 2021