scansion 0.1.6 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +29 -2
- data/lib/scansion/accents.rb +464 -0
- data/lib/scansion/version.rb +1 -1
- data/lib/scansion.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 18cc6de44c957906e6b877fcd2f606517aacc16893dbdb565476711a4967ccc5
|
|
4
|
+
data.tar.gz: c1d9c18006912e740abefb3a6ff14ddd6aae498ad90660c69731cfb9ed4942cf
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d95f9ac6f69ad9a3ba1ab1a6bc830abe94eecb38d607d08e622f547780b6418439a8100c6199cb53e73f3759ac4ac6a95eba239eb0f8d1c922e14cae8395ade9
|
|
7
|
+
data.tar.gz: 4d86d9979936c3133dda75cb4c7c39376a0cd229c28df08be5fefffb83c56babf105b9104462a6a19b95097000dcfff713ac33913b20881b72d82340c0020cfa
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# Journal des modifications
|
|
2
2
|
|
|
3
|
+
## 1.0.0 - 2026-11-23
|
|
4
|
+
|
|
5
|
+
- **Majeur** : `accents.rb` rejoint le gem (`lib/scansion/accents.rb`, `require "scansion/accents"`) — l'ancien outil externe `accent_de_phrase.rb` est renommé et déplacé. Le module `AccentDePhrase` devient **`Scansion::Accents`** (changement cassant : mettre à jour les `require` et les appels). Deux couches d'accent : accent de phrase ˈ par groupe rythmique (`Analysis#accents`, `#render`) et accent secondaire de mot ˌ (`Analysis#word_accents`) ; l'alignement mot -> syllabe consomme `Result#word_spans`
|
|
6
|
+
- `spec/accents_spec.rb` : 4 nouveaux exemples (espeak bouchonné, déterministe sans espeak-ng) — 142 exemples au total, 0 échec
|
|
7
|
+
- README : nouvelle section « Accents de phrase et accents secondaires » + structure mise à jour ; commentaires d'en-tête d'`accents.rb` traduits en français
|
|
8
|
+
|
|
3
9
|
## 0.1.6 - 2026-11-22
|
|
4
10
|
|
|
5
11
|
- nouveau paramètre `count_mute_e` : en mode contemporain (plain / `--no-dierese`), l'e muet n'est JAMAIS compté devant une consonne — « mes pères criaient dessous mes pieds » -> me-pɛʁ-kʁi-ɛ-dé-su-me-pje (8 syllabes, pɛʁ) ; « table ronde » -> tabl-ʁɔ̃d (2). En contexte classique (`dierese: true` / `:auto`), le comptage est inchangé (table ronde -> ta-blə-ʁɔ̃d, 3). La 1re passe de `:auto` garde le comptage classique (`count_mute_e: dierese != false`) pour ne pas tomber sur 10 et s'arrêter ; la passe de détection de contexte de `process_lines` aussi
|
data/README.md
CHANGED
|
@@ -69,7 +69,7 @@ Utilisation comme bibliothèque
|
|
|
69
69
|
```ruby
|
|
70
70
|
require "scansion"
|
|
71
71
|
|
|
72
|
-
Scansion::VERSION # => "
|
|
72
|
+
Scansion::VERSION # => "1.0.0"
|
|
73
73
|
res = Scansion.process_line("la belle table ronde")
|
|
74
74
|
res.phones # => "la-bɛl-ta-blə-ʁɔ̃d"
|
|
75
75
|
res.syllables # => 4
|
|
@@ -78,6 +78,32 @@ res.aligned # => true
|
|
|
78
78
|
Scansion.process_lines(lines, dierese: :auto)
|
|
79
79
|
```
|
|
80
80
|
|
|
81
|
+
Accents de phrase et accents secondaires (depuis la 1.0.0)
|
|
82
|
+
----------------------------------------------------------------
|
|
83
|
+
|
|
84
|
+
`require "scansion/accents"` ajoute `Scansion::Accents`, qui calcule deux
|
|
85
|
+
couches d'accentuation sur une scansion (règles de Delattre) :
|
|
86
|
+
|
|
87
|
+
- **accent de phrase** (ˈ) : un par groupe rythmique (syntagme), posé sur la
|
|
88
|
+
dernière syllabe accentuable du groupe — l'accent du dernier groupe est la rime ;
|
|
89
|
+
- **accent secondaire de mot** (ˌ) : un par mot plein, sur sa propre dernière
|
|
90
|
+
syllabe accentuable.
|
|
91
|
+
|
|
92
|
+
Une syllabe au noyau schwa (e caduc restauré) ne porte jamais l'accent.
|
|
93
|
+
L'alignement mot -> syllabe consomme `Scansion::Result#word_spans` (calculé
|
|
94
|
+
par le gem lui-même, donc sans dérive).
|
|
95
|
+
|
|
96
|
+
```ruby
|
|
97
|
+
require "scansion/accents"
|
|
98
|
+
|
|
99
|
+
a = Scansion::Accents.analyze_line("mes pères criaient dessous mes pieds")
|
|
100
|
+
a.render # => "me-ˌpɛʁ-kʁi-ˈɛ |-də-su-me-ˈpje (8 syll.)"
|
|
101
|
+
a.accents # => [3, 7] (indices 0-based : syllabes 4 et 8, accents de phrase)
|
|
102
|
+
a.word_accents # => [1, 3, 7] (accents secondaires des mots pères, criaient, pieds)
|
|
103
|
+
|
|
104
|
+
Scansion::Accents.analyze_lines(File.readlines("corpus.txt", chomp: true), dierese: :auto)
|
|
105
|
+
```
|
|
106
|
+
|
|
81
107
|
|
|
82
108
|
Développement
|
|
83
109
|
----------------------------------------------------------------
|
|
@@ -96,7 +122,8 @@ Structure
|
|
|
96
122
|
```
|
|
97
123
|
├── bin/scansion exécutable (scansion [options] [FILE|-])
|
|
98
124
|
├── lib/scansion.rb bibliothèque + point d'entrée CLI Scansion.main
|
|
125
|
+
├── lib/scansion/accents.rb Scansion::Accents — accents de phrase (ˈ) et de mot (ˌ)
|
|
99
126
|
├── lib/scansion/version.rb Scansion::VERSION
|
|
100
|
-
├── spec/ suite RSpec
|
|
127
|
+
├── spec/ suite RSpec (scansion_spec.rb + accents_spec.rb)
|
|
101
128
|
└── docs/scansion_rules.txt règles de scansion avec exemples travaillés
|
|
102
129
|
```
|
|
@@ -0,0 +1,464 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
#
|
|
3
|
+
# accents.rb — accents de phrase (groupes rythmiques, ˈ) et accents
|
|
4
|
+
# secondaires de mots (ˌ), par-dessus le gem scansion et phonemizer_ffi.
|
|
5
|
+
#
|
|
6
|
+
# Modèle (règles de Delattre, encodées) :
|
|
7
|
+
# 1. Deux couches d'accent : chaque GROUPE rythmique porte un accent (ˈ,
|
|
8
|
+
# « accent de phrase ») sur sa dernière syllabe accentuable ; et chaque
|
|
9
|
+
# MOT plein porte un accent interne (ˌ, « accent secondaire ») sur sa
|
|
10
|
+
# propre dernière syllabe accentuable.
|
|
11
|
+
# 2. Une syllabe au noyau schwa (e caduc restauré, /ə/) — ou un simple
|
|
12
|
+
# glide — ne porte jamais l'accent : il retombe sur la syllabe
|
|
13
|
+
# accentuable précédente.
|
|
14
|
+
# 3. Frontières de groupe (syntagmes) : avant une conjonction, un verbe
|
|
15
|
+
# auxiliaire, une préposition (sauf « de + nom nu » : « peau d'ange »),
|
|
16
|
+
# ou un déterminant ouvrant un nouveau syntagme (sauf si le mot
|
|
17
|
+
# précédent est fonctionnel ou une préposition : « à partir du col »,
|
|
18
|
+
# « que le fond ») ; plus après chaque ponctuation et en fin de vers.
|
|
19
|
+
# Un accent par groupe, sur sa dernière syllabe accentuable ; l'accent
|
|
20
|
+
# du dernier groupe est la rime. Si aucun groupe ne se termine avant la
|
|
21
|
+
# césure (6//6, 4//4), une frontière y est forcée.
|
|
22
|
+
#
|
|
23
|
+
# L'alignement mot -> syllabe consomme Scansion::Result#word_spans (calculé
|
|
24
|
+
# par le gem lui-même, donc sans dérive) ; le matching IPA sur mot isolé
|
|
25
|
+
# n'est gardé qu'en repli pour les résultats non alignés (~), sans spans.
|
|
26
|
+
#
|
|
27
|
+
# Usage :
|
|
28
|
+
# require "scansion/accents"
|
|
29
|
+
# a = Scansion::Accents.analyze_line("Au coin du feu, dans la langueur des rêveries!")
|
|
30
|
+
# puts a.render # syllabes, frontières de groupe (|) et accents (ˈ/ˌ)
|
|
31
|
+
# a.accents # => indices 0-based des syllabes accentuées (phrase)
|
|
32
|
+
# a.word_accents # => indices 0-based des accents secondaires (mots)
|
|
33
|
+
# a.warnings # problèmes d'alignement / validation
|
|
34
|
+
#
|
|
35
|
+
# Corpus :
|
|
36
|
+
# Scansion::Accents.analyze_lines(File.readlines("corpus.txt", chomp: true))
|
|
37
|
+
|
|
38
|
+
require_relative "../scansion"
|
|
39
|
+
require "phonemizer_ffi"
|
|
40
|
+
|
|
41
|
+
module Scansion
|
|
42
|
+
module Accents
|
|
43
|
+
SCHWA = "ə"
|
|
44
|
+
GLIDES = "yɥ"
|
|
45
|
+
|
|
46
|
+
# Characters treated as vocalic when looking for a syllable nucleus
|
|
47
|
+
# (incl. nasal vowels, which espeak writes as vowel + combining tilde U+0303).
|
|
48
|
+
VOWELS = "aeiouyɑɛɔøœəãẽĩõũ\u0303"
|
|
49
|
+
|
|
50
|
+
PUNCT_RE = /\A[,;:.!?…—–«»]\z/
|
|
51
|
+
|
|
52
|
+
# Classes de mots fonctionnels (sans accent propre). Un groupe rythmique se
|
|
53
|
+
# termine à la frontière devant certains d'entre eux (voir boundary_before?).
|
|
54
|
+
PREPS = %w[à au aux dans en par pour sur sous vers chez sans contre entre de d
|
|
55
|
+
dessous dessus dedans dehors avant après depuis jusqu jusque malgré selon].to_h { |w| [w, true] }.freeze
|
|
56
|
+
DETS = %w[le la les l un une du des au ce cet cette ces mon ton son ma ta mes tes ses
|
|
57
|
+
notre votre leur nos vos leurs quel quels quelle quelles
|
|
58
|
+
tout tous toute toutes].to_h { |w| [w, true] }.freeze
|
|
59
|
+
CONJS = %w[et ou mais donc or ni car que qu si].to_h { |w| [w, true] }.freeze
|
|
60
|
+
AUX = %w[suis es est sommes êtes sont étais était étaient été
|
|
61
|
+
ai as a avons avez ont avais avait avaient eu eut].to_h { |w| [w, true] }.freeze
|
|
62
|
+
PRONS = %w[je j tu t il elle on nous vous ils elles me m te se y en leur leurs ne pas plus jamais rien qui quoi dont où lequel laquelle lesquels lesquelles].to_h { |w| [w, true] }.freeze
|
|
63
|
+
# pronoms indéfinis seuls : ils ouvrent un groupe (« qu'amer » | « aucun s'efface »)
|
|
64
|
+
INDEF = %w[aucun aucune aucuns aucunes quelques plusieurs tout tous toute toutes
|
|
65
|
+
autre autres même mêmes chacun chacune chaque].to_h { |w| [w, true] }.freeze
|
|
66
|
+
FUNCTION_WORDS = PREPS.merge(DETS).merge(CONJS).merge(AUX).merge(PRONS).merge(INDEF).freeze
|
|
67
|
+
|
|
68
|
+
# Word token: Unicode word chars plus apostrophes (elisions: l'amour) and
|
|
69
|
+
# internal hyphens (porte-parapluie).
|
|
70
|
+
SCAN_RE = /[\p{Word}'’-]+|[,;:.!?…—–«»"]/
|
|
71
|
+
|
|
72
|
+
# keyword_init structs take keyword arguments only.
|
|
73
|
+
Token = Struct.new(:surface, :kind, :first, :last, keyword_init: true) # kind: :word | :punct
|
|
74
|
+
Group = Struct.new(:tokens, :accent)
|
|
75
|
+
|
|
76
|
+
Analysis = Struct.new(:text, :syllables, :tokens, :groups, :word_accents, :warnings, keyword_init: true) do
|
|
77
|
+
def accents # accents de phrase (fin de chaque groupe rythmique)
|
|
78
|
+
groups.filter_map(&:accent)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def render
|
|
82
|
+
syllables.each_with_index.map { |s, i|
|
|
83
|
+
if groups.any? { |g| g.accent == i }
|
|
84
|
+
marked = "ˈ#{s}" # accent de phrase (primaire)
|
|
85
|
+
elsif word_accents.to_a.include?(i)
|
|
86
|
+
marked = "ˌ#{s}" # accent interne du mot (secondaire)
|
|
87
|
+
else
|
|
88
|
+
marked = s
|
|
89
|
+
end
|
|
90
|
+
boundary = groups.any? { |g| g.tokens.last && g.tokens.last.last == i }
|
|
91
|
+
boundary ? "#{marked} |" : marked
|
|
92
|
+
}.join("-").sub(/ \|$/, "")
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
class << self
|
|
97
|
+
def analyze_lines(lines, dierese: :auto)
|
|
98
|
+
results = begin
|
|
99
|
+
Scansion.process_lines(lines, dierese: dierese)
|
|
100
|
+
rescue StandardError
|
|
101
|
+
lines.map { |l| Scansion.process_line(l, dierese: dierese) }
|
|
102
|
+
end
|
|
103
|
+
results.zip(lines).map { |res, text| analyze_result(res, text) }
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def analyze_line(text, dierese: :auto)
|
|
107
|
+
analyze_result(Scansion.process_line(text, dierese: dierese), text)
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
# un mot est fonctionnel si TOUTES ses parties le sont (« n\u2019est » -> n + est)
|
|
111
|
+
def head_of(tok)
|
|
112
|
+
tok.kind == :word ? pieces(tok).first.to_s.downcase : nil
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# Une frontière de groupe AVANT tokens[i] (mot) :
|
|
116
|
+
# - conjonction (que, et, ou, mais…) -> oui
|
|
117
|
+
# - verbe auxiliaire (n'est, est, a…) -> oui
|
|
118
|
+
# - préposition, sauf « de/d' + nom nu » (complément : « peau d'ange ») -> oui
|
|
119
|
+
# - déterminant qui OUVRE un nouveau syntagme, sauf si le mot
|
|
120
|
+
# précédent est fonctionnel ou une préposition (PP : « à partir du col »)
|
|
121
|
+
def boundary_before?(tokens, i)
|
|
122
|
+
tok = tokens[i]
|
|
123
|
+
return false unless tok.kind == :word
|
|
124
|
+
return false if i.zero?
|
|
125
|
+
pcs = pieces(tok)
|
|
126
|
+
head = pcs.first.to_s.downcase
|
|
127
|
+
last = pcs.last.to_s.downcase
|
|
128
|
+
nxt = tokens[i + 1]
|
|
129
|
+
nxt_head = nxt ? head_of(nxt) : nil
|
|
130
|
+
prev = tokens[i - 1]
|
|
131
|
+
prev_head = prev ? head_of(prev) : nil
|
|
132
|
+
prev2 = i >= 2 ? tokens[i - 2] : nil # pas d'index négatif !
|
|
133
|
+
prev2_head = prev2 ? head_of(prev2) : nil
|
|
134
|
+
|
|
135
|
+
return true if pcs.size == 1 && INDEF.key?(head) # aucun, tout, autre… seuls
|
|
136
|
+
return true if CONJS.key?(head) || CONJS.key?(last)
|
|
137
|
+
return true if AUX.key?(last) # n'est, est, a…
|
|
138
|
+
if PREPS.key?(head)
|
|
139
|
+
# « d'ange » (de + nom nu soudé) ou « de ange » : pas de frontière
|
|
140
|
+
return false if %w[de d].include?(head) && pcs.size > 1
|
|
141
|
+
if %w[de d].include?(head) && nxt_head && !DETS.key?(nxt_head)
|
|
142
|
+
return false
|
|
143
|
+
end
|
|
144
|
+
return true
|
|
145
|
+
end
|
|
146
|
+
if DETS.key?(head)
|
|
147
|
+
# le déterminant appartient au syntagme précédent (PP « à partir du col »,
|
|
148
|
+
# « que le fond », « sans l'accuser ») si le mot d'avant est fonctionnel
|
|
149
|
+
# ou une préposition
|
|
150
|
+
return false if prev_head && (FUNCTION_WORDS.key?(prev_head) || PREPS.key?(prev2_head.to_s))
|
|
151
|
+
return true
|
|
152
|
+
end
|
|
153
|
+
false
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
private
|
|
157
|
+
|
|
158
|
+
def analyze_result(res, text)
|
|
159
|
+
warns = []
|
|
160
|
+
if res.respond_to?(:aligned) && !res.aligned
|
|
161
|
+
warns << "scansion could not align: res.aligned is false"
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
# sanitize: un backend peut émettre des apostrophes ou autres signes
|
|
165
|
+
# de ponctuation dans l"IPA — on ne garde que lettres/marques IPA
|
|
166
|
+
syls = res.phones.split("-").map { |s| s.strip.gsub(/[[:punct:]]/u, "") }.reject(&:empty?)
|
|
167
|
+
# Sanity guard: phone segments should be 1:1 with the counted syllables
|
|
168
|
+
# (CLI example: "o-kwɛ̃-dy-fø-dɑ̃-la-lɑ̃-gœʁ-de-ʁɛ-və-ʁi" = 12/12).
|
|
169
|
+
if res.respond_to?(:syllables) && res.syllables != syls.size
|
|
170
|
+
warns << "syllable count (#{res.syllables}) != phone segments (#{syls.size}); alignment may drift"
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
# tokenize the NORMALIZED text: its word pieces then line up 1:1 with
|
|
174
|
+
# Scansion.words_of / Result#word_spans
|
|
175
|
+
tokens = tokenize(Scansion.normalize_input(text))
|
|
176
|
+
align_words(res, tokens, syls, warns)
|
|
177
|
+
groups = merge_function_groups(split_caesura(build_groups(tokens), syls))
|
|
178
|
+
groups.each { |g| g.accent = place_accent(g, syls) }
|
|
179
|
+
wacc = word_accents(tokens, syls)
|
|
180
|
+
|
|
181
|
+
analysis = Analysis.new(text: text, syllables: syls, tokens: tokens,
|
|
182
|
+
groups: groups, word_accents: wacc, warnings: warns)
|
|
183
|
+
analysis.warnings.concat(validate(analysis))
|
|
184
|
+
analysis
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
# ---- tokenization -------------------------------------------------------
|
|
188
|
+
|
|
189
|
+
def tokenize(text)
|
|
190
|
+
text.scan(SCAN_RE).map do |m|
|
|
191
|
+
kind = m.match?(PUNCT_RE) ? :punct : :word
|
|
192
|
+
Token.new(surface: m, kind: kind)
|
|
193
|
+
end
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
# pieces of a word token, split exactly like Scansion.words_of
|
|
197
|
+
# (apostrophes and hyphens are separators: "l'accuser" -> ["l", "accuser"])
|
|
198
|
+
def pieces(tok)
|
|
199
|
+
tok.surface.split(/['’-]/).reject(&:empty?)
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
# ---- token <-> syllable alignment -----------------------------------------
|
|
203
|
+
#
|
|
204
|
+
# Preferred path: Result#word_spans, computed by scansion itself while
|
|
205
|
+
# aligning letters to phones — exact by construction, no extra espeak call.
|
|
206
|
+
#
|
|
207
|
+
# Fallback (results without spans, e.g. espeak-raw fallbacks): phonemize
|
|
208
|
+
# each distinct word in isolation, then greedily match its IPA
|
|
209
|
+
# (schwa-stripped) against consecutive line syllables. Stripping ə absorbs
|
|
210
|
+
# the main source of noise between isolated and in-line phonemization: e
|
|
211
|
+
# caduc restored by scansion in context but absent in the isolated word
|
|
212
|
+
# (and occasionally the reverse). Words that cannot be matched fall back to
|
|
213
|
+
# a proportional split of the remaining syllables and are reported.
|
|
214
|
+
#
|
|
215
|
+
# Note: phonemizer_ffi is not thread-safe (global libespeak-ng state) —
|
|
216
|
+
# wrap corpus-wide calls in a Mutex if you parallelize elsewhere.
|
|
217
|
+
|
|
218
|
+
def align_words(res, tokens, syls, warns)
|
|
219
|
+
return if syls.empty?
|
|
220
|
+
words = tokens.select { |t| t.kind == :word }
|
|
221
|
+
return if words.empty?
|
|
222
|
+
|
|
223
|
+
spans = res.respond_to?(:word_spans) ? res.word_spans : nil
|
|
224
|
+
if spans && spans.size == words.sum { |t| pieces(t).size }
|
|
225
|
+
si = 0
|
|
226
|
+
words.each do |tok|
|
|
227
|
+
first = nil
|
|
228
|
+
last = nil
|
|
229
|
+
pieces(tok).each do
|
|
230
|
+
sp = spans[si]
|
|
231
|
+
si += 1
|
|
232
|
+
next unless sp && sp[:last] >= sp[:first]
|
|
233
|
+
first = sp[:first] if first.nil? || sp[:first] < first
|
|
234
|
+
last = sp[:last] if last.nil? || sp[:last] > last
|
|
235
|
+
end
|
|
236
|
+
tok.first = first
|
|
237
|
+
tok.last = last
|
|
238
|
+
end
|
|
239
|
+
return
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
align_words_ipa(words, syls, warns)
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
def align_words_ipa(words, syls, warns)
|
|
246
|
+
uniq = words.map { |t| t.surface.downcase }.uniq
|
|
247
|
+
ipa_list = Phonemizer.phonemize(uniq, language: "fr-fr")
|
|
248
|
+
ipa_list = [ipa_list] if ipa_list.is_a?(String) # single-word input
|
|
249
|
+
ipa = uniq.zip(ipa_list).to_h
|
|
250
|
+
|
|
251
|
+
pos = 0
|
|
252
|
+
unmatched = []
|
|
253
|
+
words.each_with_index do |tok, wi|
|
|
254
|
+
target = norm(ipa[tok.surface.downcase])
|
|
255
|
+
start = pos
|
|
256
|
+
rest_words = words.size - wi # this word + following words
|
|
257
|
+
acc = ""
|
|
258
|
+
|
|
259
|
+
if target.empty? # word that phonemizes to nothing (e.g. lone mute e)
|
|
260
|
+
pos += 1 unless syls.size - pos - 1 < rest_words - 1
|
|
261
|
+
else
|
|
262
|
+
while pos < syls.size && (syls.size - pos - 1) >= rest_words - 1
|
|
263
|
+
candidate = acc + norm(syls[pos])
|
|
264
|
+
break unless target.start_with?(candidate)
|
|
265
|
+
|
|
266
|
+
acc = candidate
|
|
267
|
+
pos += 1
|
|
268
|
+
break if acc == target
|
|
269
|
+
end
|
|
270
|
+
end
|
|
271
|
+
|
|
272
|
+
if pos == start || (!target.empty? && acc != target)
|
|
273
|
+
# no full match: share the remaining syllables proportionally
|
|
274
|
+
share = [[(syls.size - pos).fdiv(rest_words).ceil, 1].max, syls.size - pos].min
|
|
275
|
+
pos += share
|
|
276
|
+
unmatched << tok.surface
|
|
277
|
+
end
|
|
278
|
+
tok.first = start
|
|
279
|
+
tok.last = pos - 1
|
|
280
|
+
end
|
|
281
|
+
|
|
282
|
+
warns << "unaligned words (proportional fallback): #{unmatched.uniq.join(', ')}" if unmatched.any?
|
|
283
|
+
end
|
|
284
|
+
|
|
285
|
+
def norm(str)
|
|
286
|
+
str.to_s.tr(SCHWA, "").tr("-", "")
|
|
287
|
+
end
|
|
288
|
+
|
|
289
|
+
# ---- grouping ------------------------------------------------------------
|
|
290
|
+
|
|
291
|
+
# Accent INTERNE de chaque mot plein : sa dernière syllabe à noyau
|
|
292
|
+
# accentué (on remonte au-dessus d'un éventuel schwa final — « muette »
|
|
293
|
+
# mu-ɛ-tə porte donc son accent sur le « ɛ »). Les mots fonctionnels
|
|
294
|
+
# n'ont pas d'accent propre.
|
|
295
|
+
def word_accents(tokens, syls)
|
|
296
|
+
acc = []
|
|
297
|
+
tokens.each do |t|
|
|
298
|
+
next unless t.kind == :word && t.first && t.last && t.last >= t.first
|
|
299
|
+
next if pieces(t).all? { |p| FUNCTION_WORDS.key?(p.downcase) } # mot fonctionnel
|
|
300
|
+
i = t.last
|
|
301
|
+
i -= 1 while i > t.first && !accent_bearing?(syls[i])
|
|
302
|
+
acc << i if accent_bearing?(syls[i])
|
|
303
|
+
end
|
|
304
|
+
acc.uniq
|
|
305
|
+
end
|
|
306
|
+
|
|
307
|
+
# Un groupe rythmique (syntagme) se termine à la frontière suivante :
|
|
308
|
+
# avant une conjonction, un auxiliaire, une préposition (sauf complément
|
|
309
|
+
# « de + nom nu »), ou un déterminant ouvrant un nouveau syntagme ; et
|
|
310
|
+
# après chaque signe de ponctuation. L'accent ira sur la dernière syllabe
|
|
311
|
+
# portant un noyau du groupe.
|
|
312
|
+
def build_groups(tokens)
|
|
313
|
+
groups = []
|
|
314
|
+
current = []
|
|
315
|
+
tokens.each_with_index do |tok, i|
|
|
316
|
+
if tok.kind == :punct
|
|
317
|
+
unless current.empty?
|
|
318
|
+
groups << Group.new(current, nil)
|
|
319
|
+
current = []
|
|
320
|
+
end
|
|
321
|
+
else
|
|
322
|
+
if !current.empty? && boundary_before?(tokens, i)
|
|
323
|
+
groups << Group.new(current, nil)
|
|
324
|
+
current = []
|
|
325
|
+
end
|
|
326
|
+
current << tok
|
|
327
|
+
end
|
|
328
|
+
end
|
|
329
|
+
groups << Group.new(current, nil) unless current.empty?
|
|
330
|
+
groups
|
|
331
|
+
end
|
|
332
|
+
|
|
333
|
+
# Un groupe composé UNIQUEMENT de mots fonctionnels (et, qui, sans, le…)
|
|
334
|
+
# ne porte pas d'accent : on le fusionne dans le groupe suivant.
|
|
335
|
+
def merge_function_groups(groups)
|
|
336
|
+
out = groups.dup
|
|
337
|
+
i = 0
|
|
338
|
+
while i < out.size - 1
|
|
339
|
+
if all_function?(out[i])
|
|
340
|
+
out[i + 1] = Group.new(out[i].tokens + out[i + 1].tokens, nil)
|
|
341
|
+
out.delete_at(i)
|
|
342
|
+
else
|
|
343
|
+
i += 1
|
|
344
|
+
end
|
|
345
|
+
end
|
|
346
|
+
out
|
|
347
|
+
end
|
|
348
|
+
|
|
349
|
+
def all_function?(group)
|
|
350
|
+
group.tokens.all? do |t|
|
|
351
|
+
t.kind == :punct || pieces(t).all? { |p| FUNCTION_WORDS.key?(p.downcase) }
|
|
352
|
+
end
|
|
353
|
+
end
|
|
354
|
+
|
|
355
|
+
# Force a group boundary at the caesura (6//6 alexandrine, 4//4 octosyllable)
|
|
356
|
+
# when no punctuation already marks it: the boundary falls on the last
|
|
357
|
+
# accent-bearing syllable at or before the caesura index (0-based).
|
|
358
|
+
def split_caesura(groups, syls)
|
|
359
|
+
n = syls.size
|
|
360
|
+
c = n == 12 ? 5 : (n == 8 ? 3 : nil)
|
|
361
|
+
return groups if c.nil?
|
|
362
|
+
|
|
363
|
+
s = c
|
|
364
|
+
s -= 1 while s > 0 && (syls[s].nil? || !accent_bearing?(syls[s]))
|
|
365
|
+
# une frontière de groupe (mot plein ou ponctuation) avant la cesure suffit
|
|
366
|
+
return groups if groups.any? { |g| g.tokens.last && g.tokens.last.last && g.tokens.last.last <= c }
|
|
367
|
+
|
|
368
|
+
out = []
|
|
369
|
+
groups.each do |g|
|
|
370
|
+
cover = g.tokens.index { |t| t.first && t.last && t.first <= s && t.last >= s }
|
|
371
|
+
cover = nil if cover == g.tokens.size - 1 && g.tokens.last.last == s
|
|
372
|
+
if cover && cover < g.tokens.size - 1
|
|
373
|
+
out << Group.new(g.tokens[0..cover], nil)
|
|
374
|
+
out << Group.new(g.tokens[(cover + 1)..], nil)
|
|
375
|
+
else
|
|
376
|
+
out << g
|
|
377
|
+
end
|
|
378
|
+
end
|
|
379
|
+
out
|
|
380
|
+
end
|
|
381
|
+
|
|
382
|
+
# ---- accent placement ----------------------------------------------------
|
|
383
|
+
|
|
384
|
+
# Rule 2: walk back from the group's last syllable to the first
|
|
385
|
+
# accent-bearing one. Group boundaries come from punctuation + caesura
|
|
386
|
+
# (rule 3).
|
|
387
|
+
def place_accent(group, syls)
|
|
388
|
+
return nil if group.tokens.empty?
|
|
389
|
+
lo = group.tokens.map(&:first).compact.min
|
|
390
|
+
hi = group.tokens.map(&:last).compact.max
|
|
391
|
+
return nil if lo.nil? || hi.nil?
|
|
392
|
+
lo = [lo, 0].max
|
|
393
|
+
hi = [hi, syls.size - 1].min
|
|
394
|
+
return nil if lo > hi
|
|
395
|
+
i = hi
|
|
396
|
+
i -= 1 while i > lo && !accent_bearing?(syls[i])
|
|
397
|
+
i
|
|
398
|
+
end
|
|
399
|
+
|
|
400
|
+
# noyau effectif d'une syllabe : ses caractères voyelles, mais un y/ɥ
|
|
401
|
+
# initial suivi d'une autre voyelle est un glide, pas un noyau
|
|
402
|
+
# ("yi" -> i ; "ty" (tu) -> y reste un noyau)
|
|
403
|
+
def nucleus(syl)
|
|
404
|
+
return "" if syl.nil?
|
|
405
|
+
chars = syl.chars.select { |c| VOWELS.include?(c) }
|
|
406
|
+
chars.shift if chars.size > 1 && GLIDES.include?(chars.first.to_s)
|
|
407
|
+
chars.join
|
|
408
|
+
end
|
|
409
|
+
|
|
410
|
+
# une syllabe porte l'accent si son noyau est une vraie voyelle
|
|
411
|
+
# (ni schwa, ni glide seul)
|
|
412
|
+
def accent_bearing?(syl)
|
|
413
|
+
n = nucleus(syl)
|
|
414
|
+
!n.empty? && n != SCHWA
|
|
415
|
+
end
|
|
416
|
+
|
|
417
|
+
# ---- validation ----------------------------------------------------------
|
|
418
|
+
#
|
|
419
|
+
# Alexandrines and octosyllables give a free gold standard: the last
|
|
420
|
+
# syllable (rhyme) must be the accent of the last group, and a group
|
|
421
|
+
# boundary near the caesura (syllable 6 of 12, 4 of 8) must carry that
|
|
422
|
+
# group's accent.
|
|
423
|
+
|
|
424
|
+
def validate(a)
|
|
425
|
+
w = []
|
|
426
|
+
n = a.syllables.size
|
|
427
|
+
return w unless [8, 12].include?(n)
|
|
428
|
+
|
|
429
|
+
last = a.groups.last
|
|
430
|
+
if last.nil? || last.accent != n - 1
|
|
431
|
+
w << "verse #{n}: rhyme (syllable #{n}) is not the last group's accent"
|
|
432
|
+
elsif !accent_bearing?(a.syllables[n - 1])
|
|
433
|
+
w << "verse #{n}: rhyme syllable has no accent-bearing nucleus — suspicious elision at line end"
|
|
434
|
+
end
|
|
435
|
+
|
|
436
|
+
c = n == 12 ? 5 : 3 # index 0-based de la cesure
|
|
437
|
+
unless a.groups.any? { |g| g.tokens.last && g.tokens.last.last && g.tokens.last.last <= c }
|
|
438
|
+
w << "verse #{n}: no group boundary at the caesura (syllable #{c + 1})"
|
|
439
|
+
end
|
|
440
|
+
w
|
|
441
|
+
end
|
|
442
|
+
|
|
443
|
+
|
|
444
|
+
end
|
|
445
|
+
end
|
|
446
|
+
end
|
|
447
|
+
|
|
448
|
+
# ---- démo -------------------------------------------------------------------
|
|
449
|
+
|
|
450
|
+
if __FILE__ == $PROGRAM_NAME
|
|
451
|
+
corpus = [
|
|
452
|
+
"Au coin du feu, dans la langueur des rêveries!",
|
|
453
|
+
"Le jour n'est pas plus pur que le fond de ma nuit",
|
|
454
|
+
"Dites, sans l’accuser de chercher un esclave,",
|
|
455
|
+
]
|
|
456
|
+
Scansion::Accents.analyze_lines(corpus).each do |a|
|
|
457
|
+
puts a.text
|
|
458
|
+
puts " #{a.render} (#{a.syllables.size} syll.)"
|
|
459
|
+
puts " accents (phrase): #{a.accents.map { |i| i + 1 }.join(', ')}"
|
|
460
|
+
puts " accents (mot): #{a.word_accents.map { |i| i + 1 }.join(', ')}"
|
|
461
|
+
a.warnings.each { |w| puts " ⚠ #{w}" }
|
|
462
|
+
puts
|
|
463
|
+
end
|
|
464
|
+
end
|
data/lib/scansion/version.rb
CHANGED
data/lib/scansion.rb
CHANGED
|
@@ -1463,7 +1463,7 @@ module Scansion
|
|
|
1463
1463
|
end
|
|
1464
1464
|
|
|
1465
1465
|
# spans mot -> indices de syllabes (pour les outils en aval, ex.
|
|
1466
|
-
#
|
|
1466
|
+
# scansion/accents) : même règle de comptage que count_syllables
|
|
1467
1467
|
# (un glide y/ɥ suivi d'une voyelle n'est pas un noyau, sauf si la
|
|
1468
1468
|
# voyelle ouvre le mot suivant)
|
|
1469
1469
|
word_spans = []
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: scansion
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 1.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- anantone
|
|
@@ -70,6 +70,7 @@ files:
|
|
|
70
70
|
- bin/scansion
|
|
71
71
|
- docs/scansion_rules.txt
|
|
72
72
|
- lib/scansion.rb
|
|
73
|
+
- lib/scansion/accents.rb
|
|
73
74
|
- lib/scansion/version.rb
|
|
74
75
|
homepage: "https://github.com/anantone/scansion"
|
|
75
76
|
licenses:
|