<commit_before># Copyright 2016 <NAME>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import requests
import xml.etree.ElementTree as et
from . import countries

_url = None

def init(url):
	global _url
	_url = url

def grobid(affiliation):
	cmd = 'affiliations=' + affiliation
	r = requests.post(_url + '/processAffiliations', data=cmd)
	xml = r.content.decode('UTF-8')
	result = dict.fromkeys(['institute', 'city', 'alpha2', 'country',])
	
	# return if the returned string is not parseable
	try:
		root = et.fromstring('<results>' + xml + '</results>')
	except et.ParseError:
		return result
	
	# try to find an organisation (consider only the first one)
	organisations = root.findall('./affiliation/orgName')
	for org in organisations:
		if org.get('type') == 'institution':
			result['institute'] = org.text
			break
	# return immediately if none was found
	else:
		return result
	<commit_msg>add docstrings to the parser module<commit_after># Copyright 2016 <NAME>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

'''Module to parse an affiliation string using grobid.'''

import requests
import xml.etree.ElementTree as et
from . import countries

_url = None

def init(url):
	'''
	Initialise the grobid url
	
	:param url: the URL to the grobid service
	'''
	global _url
	_url = url

def grobid(affiliation):
	'''
	Parse the given affiliation string using grobid.
	
	:param affiliation: the affiliation string to be parsed
	'''
	# let grobid process the given affiliation string
	cmd = 'affiliations=' + affiliation
	r = requests.post(_url + '/processAffiliations', data=cmd)
	xml = r.content.decode('UTF-8')
	result = dict.fromkeys(['institute', 'city', 'alpha2', 'country',])
	
	# return if the returned string is not parseable
	try:
		root = et.fromstring('<results>' + xml + '</results>')
	except et.ParseError:
		return result
	
	# try to find an organisation (consider only the first one)
	organisations = root.findall('./affiliation/orgName')
	for org in organisations:
		if org.get('type') == 'institution':
			result['institute'] = org.text
			break
	# return immediately if none was found
	else:
		return result
	
{'fraction_non_alphanumeric': 0.10009593859929646, 'fraction_numerical': 0.0063959066197633516, 'mean_word_length': 4.239530988274707, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5591733', 'n_tokens_mistral': 931, 'n_tokens_neox': 880, 'n_words': 373}