-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[egs] gale_arabic: add python script to process xml file (#3886)
- Loading branch information
Showing
8 changed files
with
166 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/bin/bash | ||
|
||
# check whether bs4 and lxml is installed | ||
if ! python3 -c "import bs4" 2>/dev/null; then | ||
echo "$0: BeautifulSoup4 not installed, you can install it by 'pip install beautifulsoup4' if you prefer to use python to process xml file" | ||
exit 1; | ||
fi | ||
|
||
if ! python3 -c "import lxml" 2>/dev/null; then | ||
echo "$0: lxml not installed, you can install it by 'pip install lxml' if you prefer to use python to process xml file" | ||
exit 1; | ||
fi | ||
|
||
echo "both BeatufileSoup4 and lxml are installed in python" | ||
exit 0 |
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,35 @@ | ||
#!/usr/bin/env python3 | ||
|
||
from bs4 import BeautifulSoup | ||
import sys | ||
import argparse | ||
|
||
def get_args(): | ||
parser = argparse.ArgumentParser(description="""This script process xml file.""") | ||
parser.add_argument("xml", type=str, help="""Input xml file""") | ||
parser.add_argument("output", type=str, help="""output text file""") | ||
args = parser.parse_args() | ||
return args | ||
|
||
def process_xml(xml_handle, output_handle): | ||
soup = BeautifulSoup(xml_handle, "xml") | ||
for segment in soup.find_all("segment"): | ||
who = segment["who"] | ||
starttime = segment["starttime"] | ||
endtime = segment["endtime"] | ||
WMER = segment["WMER"] | ||
text = " ".join([element.string for element in segment.find_all("element") if element.string != None]) | ||
output_handle.write("{} {} {} {} {}\n".format(who, starttime, endtime, WMER, text)) | ||
xml_handle.close() | ||
output_handle.close() | ||
|
||
def main(): | ||
args = get_args() | ||
|
||
xml_handle = open(args.xml, 'r') | ||
output_handle = sys.stdout if args.output == '-' else open(args.output, 'w') | ||
|
||
process_xml(xml_handle, output_handle) | ||
|
||
if __name__ == "__main__": | ||
main() |
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,15 @@ | ||
#!/bin/bash | ||
|
||
# check whether bs4 and lxml is installed | ||
if ! python3 -c "import bs4" 2>/dev/null; then | ||
echo "$0: BeautifulSoup4 not installed, you can install it by 'pip install beautifulsoup4' if you prefer to use python to process xml file" | ||
exit 1; | ||
fi | ||
|
||
if ! python3 -c "import lxml" 2>/dev/null; then | ||
echo "$0: lxml not installed, you can install it by 'pip install lxml' if you prefer to use python to process xml file" | ||
exit 1; | ||
fi | ||
|
||
echo "both BeatufileSoup4 and lxml are installed in python" | ||
exit 0 |
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,35 @@ | ||
#!/usr/bin/env python3 | ||
|
||
from bs4 import BeautifulSoup | ||
import sys | ||
import argparse | ||
|
||
def get_args(): | ||
parser = argparse.ArgumentParser(description="""This script process xml file.""") | ||
parser.add_argument("xml", type=str, help="""Input xml file""") | ||
parser.add_argument("output", type=str, help="""output text file""") | ||
args = parser.parse_args() | ||
return args | ||
|
||
def process_xml(xml_handle, output_handle): | ||
soup = BeautifulSoup(xml_handle, "xml") | ||
for segment in soup.find_all("segment"): | ||
who = segment["who"] | ||
starttime = segment["starttime"] | ||
endtime = segment["endtime"] | ||
WMER = segment["WMER"] | ||
text = " ".join([element.string for element in segment.find_all("element") if element.string != None]) | ||
output_handle.write("{} {} {} {} {}\n".format(who, starttime, endtime, WMER, text)) | ||
xml_handle.close() | ||
output_handle.close() | ||
|
||
def main(): | ||
args = get_args() | ||
|
||
xml_handle = open(args.xml, 'r') | ||
output_handle = sys.stdout if args.output == '-' else open(args.output, 'w') | ||
|
||
process_xml(xml_handle, output_handle) | ||
|
||
if __name__ == "__main__": | ||
main() |
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