Skip to content

Commit

Permalink
Add WritingGoal class (#19)
Browse files Browse the repository at this point in the history
* Add WritingGoal class

 - add WG<=>TT mapping to constants;
 - add test to make sure we can get the WGs correctly from the TTs;
 - add WG column to master CSV.

* Update readme with writing goal info
  • Loading branch information
p-goulart authored Jul 20, 2023
1 parent 4ac47f5 commit 4d6ea13
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 6 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ ABSAGE_SUBST
This script is meant to provide an all-encompassing view of **all** rules created by the lingu team in **one single file** – this means *all locales* are present in the same file. For example:

```csv
row,id,locale,source_repo,type,source_file,tone_tags,is_goal_specific
0,IN_SHANGHAI,en,os,grammar,grammar.xml,,false
1,IN_SHANGHAI,en,os,grammar,grammar.xml,,false
2,LOWERCASE_NAMES,fr,premium,grammar,grammar.xml,,false
3,SOME_EXAMPLE,es,os,style,grammar.xml,professional,true
row,id,subId,locale,source_repo,type,source_file,tone_tags,writing_goals,is_goal_specific
0,IN_SHANGHAI,1,en,os,grammar,grammar.xml,,,false
1,IN_SHANGHAI,2,en,os,grammar,grammar.xml,,,false
2,LOWERCASE_NAMES,1,fr,premium,grammar,grammar.xml,,,false
3,SOME_EXAMPLE,1,es,os,style,grammar.xml,professional,serious,true
```

#### Table headers
Expand All @@ -118,6 +118,7 @@ row,id,locale,source_repo,type,source_file,tone_tags,is_goal_specific
|type|'grammar', 'style', or 'unknown'|
|source_file|the path to the file where this rule comes from|
|tone_tags|comma-separated list of `tone_tags` applied to the the rule, including those inherited from rulegroups and categories|
|writing_goals|comma-separate list of writing goals served by the tone tags in the previous column|
|is_goal_specific|boolean value of `is_goal_specific` rule attribute|


Expand Down
8 changes: 8 additions & 0 deletions scripts/lib/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,11 @@
LOCALES = {'de', 'es', 'en', 'fr', 'nl', 'pt'}
REPOS = {'os': 'languagetool',
'premium': 'languagetool-premium-modules'}

WRITING_GOAL_MAPPING = {
"serious": ['clarity', 'confident', 'formal', 'general', 'positive', 'professional'],
"objective": ['academic', 'clarity', 'formal', 'general', 'objective', 'povrem', 'scientific'],
"confident": ['clarity', 'confident', 'general', 'persuasive', 'positive'],
"personal": ['clarity', 'general', 'informal', 'positive', 'povadd'],
"expressive": ['clarity', 'general']
}
20 changes: 20 additions & 0 deletions scripts/lib/writing_goals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from typing import List
from elements import ToneTag
from constants import *


class WritingGoal:
# This constructor isn't needed for now tbh
def __init__(self, name: str):
self.name = name
self.tags = WRITING_GOAL_MAPPING[name]

@staticmethod
# From a list of tone tags, return all the writing goals serviced by that tag
def list_from_tags(tags: List[ToneTag]):
goals = []
for tag in tags:
for goal, tag_list in WRITING_GOAL_MAPPING.items():
if tag.tag in tag_list and goal not in goals:
goals.append(goal)
return goals
6 changes: 5 additions & 1 deletion scripts/master_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from lib.logger import logger_wrapper
import os
from pandas import DataFrame
from lib.writing_goals import WritingGoal


class CLI:
Expand All @@ -26,13 +27,15 @@ def __init__(self):
# type (grammar, style, unknown)
# source file
# tone_tags
# writing goals
# is_goal_specific
def __main__():
cli = CLI()
logger = logger_wrapper(cli.parser.prog, cli.args.verbosity)
logger.debug(f"Starting script...\nInvoked with options: {cli.args}")
out_path = path.join(cli.args.out_dir, 'all_rules.csv')
headers = ['id', 'subId', 'locale', 'source_repo', 'type', 'source_file', 'tone_tags', 'is_goal_specific']
headers = ['id', 'subId', 'locale', 'source_repo', 'type', 'source_file', 'tone_tags', 'writing_goals',
'is_goal_specific']
rows = []
for locale in LOCALES:
for repo_name, repo_dir in REPOS.items():
Expand All @@ -43,6 +46,7 @@ def __main__():
rows.append([
rule.id, rule.sub_id, locale, repo_name, file.type, file.rel_path,
','.join([tt.tag for tt in rule.tone_tags]),
','.join(WritingGoal.list_from_tags(rule.tone_tags)),
rule.is_goal_specific
])
df = DataFrame(rows, columns=headers)
Expand Down
5 changes: 5 additions & 0 deletions tests/test_tone_tags.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import unittest
from tests.mock import mock_rule
from scripts.lib.elements import ToneTag
from scripts.lib.writing_goals import WritingGoal


class TestToneTags(unittest.TestCase):
Expand All @@ -26,6 +27,10 @@ def test_tone_tag_inheritance_repeated(self):
{'tone_tags': 'formal'})
self.assertEquals(len(rule.tone_tags), 1)

def test_writing_goal(self):
self.assertEquals(len(WritingGoal.list_from_tags([ToneTag('clarity')])), 5)
self.assertEquals(len(WritingGoal.list_from_tags([ToneTag('formal'), ToneTag('informal')])), 3)


if __name__ == '__main__':
unittest.main()

0 comments on commit 4d6ea13

Please sign in to comment.