2023-03-27 17:14:07 -04:00
|
|
|
import spacy
|
|
|
|
from collections import Counter
|
2023-03-27 18:22:30 -04:00
|
|
|
import re as regex
|
2023-03-27 17:14:07 -04:00
|
|
|
import os
|
2023-03-31 13:41:41 -04:00
|
|
|
from saxonche import PySaxonProcessor
|
|
|
|
|
2023-03-29 14:22:05 -04:00
|
|
|
|
|
|
|
#### Loads all of the necessary variables and functions.
|
|
|
|
nlp = spacy.load("en_core_web_lg")
|
2023-03-27 17:14:07 -04:00
|
|
|
workingDir = os.getcwd()
|
|
|
|
CollPath = os.path.join(workingDir, '../regexConsp')
|
2023-03-29 14:22:05 -04:00
|
|
|
outputPath = os.path.join(workingDir, 'output/')
|
|
|
|
# Everything in original conspiracy directory.
|
2023-03-27 17:14:07 -04:00
|
|
|
insideDir = os.listdir(CollPath)
|
|
|
|
print(insideDir)
|
|
|
|
|
2023-03-29 14:22:05 -04:00
|
|
|
# Copies files in case they do not exist
|
|
|
|
def copyTextFiles(file):
|
|
|
|
content = []
|
|
|
|
# Reads the contents of file, and saves each line of file into the content array.
|
|
|
|
with open(CollPath + "/" + file, 'r', encoding='utf8') as inFile:
|
|
|
|
for line in inFile:
|
|
|
|
content.append(line)
|
2023-03-31 13:41:41 -04:00
|
|
|
print(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~ copying " + file + " ~~~~~~~~~~~~~~~~~~~~~~~~~~~ ")
|
2023-03-29 14:22:05 -04:00
|
|
|
inFile.close()
|
|
|
|
# With the contents copied, a loop will go through the array and write it all in a new file in output folder.
|
|
|
|
with open(outputPath + "/" + file, 'w', encoding='utf8') as f:
|
|
|
|
for line in content:
|
|
|
|
f.write(str(line))
|
2023-03-27 17:14:07 -04:00
|
|
|
|
2023-03-29 14:22:05 -04:00
|
|
|
# Function runs through the tokens of given file. Entities are stored in array, then returned. Called by regexFile().
|
2023-03-27 17:14:07 -04:00
|
|
|
def entitycollector(tokens):
|
2023-03-31 15:01:49 -04:00
|
|
|
# creates a new file that includes all of the found entities.
|
2023-03-31 13:41:41 -04:00
|
|
|
with open('output.txt', 'w') as f:
|
|
|
|
entities = {}
|
2023-03-31 15:01:49 -04:00
|
|
|
# goes through each entity in the token list.
|
2023-03-31 13:41:41 -04:00
|
|
|
for ent in sorted(tokens.ents):
|
|
|
|
entityInfo = [ent.text, ent.label_, spacy.explain(ent.label_)]
|
|
|
|
stringify = str(entityInfo)
|
|
|
|
f.write(stringify)
|
|
|
|
f.write('\n')
|
|
|
|
entities[ent.text] = ent.label_
|
2023-03-31 15:01:49 -04:00
|
|
|
# return all entities with its label and text.
|
2023-03-27 18:22:30 -04:00
|
|
|
return entities
|
2023-03-27 17:14:07 -04:00
|
|
|
|
2023-03-29 14:22:05 -04:00
|
|
|
# Function runs regex through given file.
|
|
|
|
def regexFile(file):
|
2023-03-31 13:41:41 -04:00
|
|
|
fileDir = os.path.join(outputPath, file)
|
|
|
|
with PySaxonProcessor(license=False) as proc:
|
2023-03-31 15:01:49 -04:00
|
|
|
# grabs the original xml file and stores it in a variable for later. this some xquery bs
|
2023-03-31 13:41:41 -04:00
|
|
|
xml = open(fileDir, encoding='utf-8').read()
|
|
|
|
xp = proc.new_xpath_processor()
|
|
|
|
node = proc.parse_xml(xml_text=xml)
|
|
|
|
xp.set_context(xdm_item=node)
|
2023-03-31 15:01:49 -04:00
|
|
|
|
|
|
|
# xquery goes through original text, and stores it all in a single string.
|
2023-03-31 13:41:41 -04:00
|
|
|
xpath = xp.evaluate('//p ! normalize-space() => string-join()')
|
|
|
|
string = xpath.__str__()
|
2023-03-31 15:01:49 -04:00
|
|
|
|
|
|
|
# regex goes through the text and deletes anything that is not a letter or space.
|
2023-03-31 13:41:41 -04:00
|
|
|
cleanedText = regex.sub('[^A-z]+', ' ', string)
|
2023-03-31 15:01:49 -04:00
|
|
|
|
|
|
|
# gets the tokens of the clean text.
|
2023-03-29 14:22:05 -04:00
|
|
|
tokens = nlp(cleanedText)
|
2023-03-31 15:01:49 -04:00
|
|
|
|
2023-03-31 13:41:41 -04:00
|
|
|
wrappedText = xml
|
2023-03-31 15:01:49 -04:00
|
|
|
# grabs all the entities in file and stores it in a list/array.
|
2023-03-29 14:22:05 -04:00
|
|
|
listEntities = entitycollector(tokens)
|
2023-03-31 15:01:49 -04:00
|
|
|
# if anything exists in the list, the following code will run.
|
2023-03-29 14:22:05 -04:00
|
|
|
if listEntities:
|
2023-03-31 15:01:49 -04:00
|
|
|
# it will check through each entity in the list and see its entity type. it is looking for "PERSON" tokens
|
|
|
|
# in this instance, which includes of nouns and names.
|
2023-03-31 13:41:41 -04:00
|
|
|
for entity in listEntities.keys():
|
|
|
|
if listEntities[entity] == "PERSON":
|
2023-03-31 15:01:49 -04:00
|
|
|
# key_template variable is the elements we wrap around found instances.
|
2023-03-31 13:41:41 -04:00
|
|
|
key_template = "<ent type = 'person'>" + entity + "</ent>"
|
2023-03-31 15:01:49 -04:00
|
|
|
# loops through wrappedText until all entities are wrapped.
|
2023-03-31 13:41:41 -04:00
|
|
|
wrappedText = wrappedText.replace(entity, key_template)
|
2023-03-31 15:01:49 -04:00
|
|
|
# Saves newly wrapped elements and then writes it into new file.
|
2023-03-31 13:41:41 -04:00
|
|
|
with open(fileDir, 'w', encoding='utf8') as f:
|
|
|
|
f.write(wrappedText)
|
|
|
|
print("WRAPPING " + entity)
|
2023-03-29 14:22:05 -04:00
|
|
|
|
2023-03-31 15:01:49 -04:00
|
|
|
# This part of the code does not run. It is a WIP.
|
|
|
|
## It tries to find weird or invalid elements/tags and fix them.
|
|
|
|
def checkTags(file):
|
|
|
|
content = []
|
|
|
|
fileDir = os.path.join(outputPath, file)
|
|
|
|
|
|
|
|
with open(fileDir, 'r', encoding='utf8') as inFile:
|
|
|
|
for line in inFile:
|
|
|
|
content.append(line)
|
|
|
|
# With the contents copied, a loop will go through the array and write it all in a new file in output folder.
|
|
|
|
with open(fileDir, 'w', encoding='utf8') as f:
|
|
|
|
for line in content:
|
|
|
|
match1 = regex.search("(<ent type = 'person'>){2,}(.+?)</ent>", line)
|
|
|
|
if match:
|
|
|
|
print("broken line found, fixing...")
|
|
|
|
newLine = regex.sub("(<ent type = 'person'>){2,}(.+?)</ent>",r"\1 \2",line)
|
|
|
|
print(line + "\n INTO.")
|
|
|
|
print(newLine)
|
|
|
|
|
|
|
|
|
2023-03-29 14:22:05 -04:00
|
|
|
for file in insideDir:
|
|
|
|
copyTextFiles(file)
|
2023-03-31 13:41:41 -04:00
|
|
|
regexFile(file)
|
2023-03-31 15:01:49 -04:00
|
|
|
#checkTags(file)
|
2023-03-31 13:41:41 -04:00
|
|
|
print("File checking finished.")
|